Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI
Python agentagentopsagents-sdkaianthropicautogencost-estimationcrewaievalsevaluation-metricsgroqlangchainllmmistralollamaopenaiopenai-agents
[Feature] suppress logging unless init is called (#159)
Shawn Qiu7353a8cd3eee
diff --git a/agentops/__init__.py b/agentops/__init__.pyindex 232b3e90..0de3019d 100755--- a/agentops/__init__.py+++ b/agentops/__init__.py@@ -8,6 +8,7 @@ from .event import Event, ActionEvent, LLMEvent, ToolEvent, ErrorEventfrom .enums import Modelsfrom .decorators import record_functionfrom .agent import track_agent+from .log_config import set_logging_level_info, set_logging_level_critialdef init(api_key: Optional[str] = None,@@ -41,7 +42,7 @@ def init(api_key: Optional[str] = None,inherited_session_id (optional, str): Init Agentops with an existing SessionAttributes:"""-+ set_logging_level_info()c = Client(api_key=api_key,parent_key=parent_key,endpoint=endpoint,@@ -52,7 +53,7 @@ def init(api_key: Optional[str] = None,auto_start_session=auto_start_session,inherited_session_id=inherited_session_id)-+return inherited_session_id or c.current_session_iddiff --git a/agentops/agent.py b/agentops/agent.pyindex 2a0a005c..fd3c8df4 100644--- a/agentops/agent.py+++ b/agentops/agent.py@@ -1,4 +1,4 @@-import logging+from .log_config import loggerfrom uuid import uuid4from agentops import Clientfrom inspect import isclass, isfunction@@ -18,7 +18,7 @@ def track_agent(name: str | None = None):self.agent_ops_agent_id = uuid4()Client().create_agent(self.agent_ops_agent_id, self.agent_ops_agent_name)except AttributeError as e:- logging.error("AgentOps failed to track an agent. This often happens if agentops.init() was not "+ logger.warning("AgentOps failed to track an agent. This often happens if agentops.init() was not ""called before initializing an agent with the @track_agent decorator.")raise ediff --git a/agentops/client.py b/agentops/client.pyindex b97d0c55..fe64799a 100644--- a/agentops/client.py+++ b/agentops/client.py@@ -14,7 +14,7 @@ from .host_env import get_host_envfrom uuid import uuid4from typing import Optional, Listimport traceback-import logging+from .log_config import logger, set_logging_level_infoimport inspectimport atexitimport signal@@ -115,7 +115,7 @@ class Client(metaclass=MetaClient):if self._session is not None and not self._session.has_ended:self._worker.add_event(event.__dict__)else:- logging.warning(+ logger.warning("🖇 AgentOps: Cannot record event - no current session")def _record_event_sync(self, func, event_name, *args, **kwargs):@@ -206,20 +206,22 @@ class Client(metaclass=MetaClient):config: (Configuration, optional): Client configuration objectinherited_session_id (optional, str): assign session id to match existing Session"""+ set_logging_level_info()+if self._session is not None:- return logging.warning("🖇 AgentOps: Cannot start session - session already started")+ return logger.warning("🖇 AgentOps: Cannot start session - session already started")if not config and not self.config:- return logging.warning("🖇 AgentOps: Cannot start session - missing configuration")+ return logger.warning("🖇 AgentOps: Cannot start session - missing configuration")self._session = Session(inherited_session_id or uuid4(), tags or self._tags, host_env=get_host_env())self._worker = Worker(config or self.config)start_session_result = self._worker.start_session(self._session)if not start_session_result:self._session = None- return logging.warning("🖇 AgentOps: Cannot start session")+ return logger.warning("🖇 AgentOps: Cannot start session")- logging.info('View info on this session at https://app.agentops.ai/drilldown?session_id={}'+ logger.info('View info on this session at https://app.agentops.ai/drilldown?session_id={}'.format(self._session.session_id))return self._session.session_id@@ -237,10 +239,10 @@ class Client(metaclass=MetaClient):video (str, optional): The video screen recording of the session"""if self._session is None or self._session.has_ended:- return logging.warning("🖇 AgentOps: Cannot end session - no current session")+ return logger.warning("🖇 AgentOps: Cannot end session - no current session")if not any(end_state == state.value for state in EndState):- return logging.warning("🖇 AgentOps: Invalid end_state. Please use one of the EndState enums")+ return logger.warning("🖇 AgentOps: Invalid end_state. Please use one of the EndState enums")self._session.video = videoself._session.end_session(end_state, end_state_reason)@@ -272,7 +274,7 @@ class Client(metaclass=MetaClient):frame: The current stack frame."""signal_name = 'SIGINT' if signum == signal.SIGINT else 'SIGTERM'- logging.info(+ logger.info(f'🖇 AgentOps: {signal_name} detected. Ending session...')self.end_session(end_state='Fail',end_state_reason=f'Signal {signal_name} detected')diff --git a/agentops/config.py b/agentops/config.pyindex f5751b24..6ea2f7db 100644--- a/agentops/config.py+++ b/agentops/config.py@@ -7,7 +7,7 @@ Classes:from typing import Optionalfrom os import environ-import logging+from .log_config import loggerclass Configuration:@@ -143,4 +143,4 @@ class ConfigurationError(Exception):def __init__(self, message: str):super().__init__(message)- logging.warning(message)+ logger.warning(message)diff --git a/agentops/helpers.py b/agentops/helpers.pyindex 409889f2..71990c4b 100644--- a/agentops/helpers.py+++ b/agentops/helpers.py@@ -4,7 +4,7 @@ import timefrom datetime import datetimeimport jsonimport inspect-import logging+from .log_config import loggerfrom uuid import UUIDimport osfrom importlib.metadata import version@@ -85,7 +85,7 @@ def check_call_stack_for_agent_id() -> str | None:if var == "__main__":returnif hasattr(var, 'agent_ops_agent_id') and getattr(var, 'agent_ops_agent_id'):- logging.debug('LLM call from agent named: ' + getattr(var, 'agent_ops_agent_name'))+ logger.debug('LLM call from agent named: ' + getattr(var, 'agent_ops_agent_name'))return getattr(var, 'agent_ops_agent_id')return None@@ -95,7 +95,7 @@ def get_agentops_version():pkg_version = version("agentops")return pkg_versionexcept Exception as e:- logging.warning(f"Error reading package version: {e}")+ logger.warning(f"Error reading package version: {e}")return Nonediff --git a/agentops/http_client.py b/agentops/http_client.pyindex f1d205f0..2c743e22 100644--- a/agentops/http_client.py+++ b/agentops/http_client.py@@ -1,6 +1,6 @@from enum import Enumfrom typing import Optional-import logging+from .log_config import loggerimport requestsfrom requests.adapters import Retry, HTTPAdapter@@ -82,7 +82,7 @@ class HttpClient:except requests.exceptions.Timeout:result.code = 408result.status = HttpStatus.TIMEOUT- logging.warning(+ logger.warning('🖇 AgentOps: Could not post data - connection timed out')except requests.exceptions.HTTPError as e:try:@@ -96,12 +96,12 @@ class HttpClient:result.body = {'error': str(e)}if result.code == 401:- logging.warning(+ logger.warning(f'🖇 AgentOps: Could not post data - API server rejected your API key: {api_key}')if result.code == 400:- logging.warning(f'🖇 AgentOps: Could not post data - {result.body}')+ logger.warning(f'🖇 AgentOps: Could not post data - {result.body}')if result.code == 500:- logging.warning(+ logger.warning(f'🖇 AgentOps: Could not post data - internal server error')return resultdiff --git a/agentops/llm_tracker.py b/agentops/llm_tracker.pyindex 6bae7446..3602f0bc 100644--- a/agentops/llm_tracker.py+++ b/agentops/llm_tracker.py@@ -2,7 +2,7 @@ import functoolsimport sysfrom importlib import import_modulefrom packaging.version import parse-import logging+from .log_config import loggerfrom .event import LLMEvent, ErrorEventfrom .helpers import get_ISO_time, check_call_stack_for_agent_idimport inspect@@ -59,8 +59,8 @@ class LlmTracker:self.client.record(self.llm_event)except Exception as e:self.client.record(ErrorEvent(trigger_event=self.llm_event, exception=e))- # TODO: This error is specific to only one path of failure. Should be more generic or have different logging for different paths- logging.warning(+ # TODO: This error is specific to only one path of failure. Should be more generic or have different logger for different paths+ logger.warning(f"🖇 AgentOps: Unable to parse a chunk for LLM call {kwargs} - skipping upload to AgentOps")# if the response is a generator, decorate the generator@@ -98,8 +98,8 @@ class LlmTracker:self.client.record(self.llm_event)except Exception as e:self.client.record(ErrorEvent(trigger_event=self.llm_event, exception=e))- # TODO: This error is specific to only one path of failure. Should be more generic or have different logging for different paths- logging.warning(+ # TODO: This error is specific to only one path of failure. Should be more generic or have different logger for different paths+ logger.warning(f"🖇 AgentOps: Unable to parse a chunk for LLM call {kwargs} - skipping upload to AgentOps")return response@@ -144,8 +144,8 @@ class LlmTracker:self.client.record(self.llm_event)except Exception as e:self.client.record(ErrorEvent(trigger_event=self.llm_event, exception=e))- # TODO: This error is specific to only one path of failure. Should be more generic or have different logging for different paths- logging.warning(+ # TODO: This error is specific to only one path of failure. Should be more generic or have different logger for different paths+ logger.warning(f"🖇 AgentOps: Unable to parse a chunk for LLM call {kwargs} - skipping upload to AgentOps")# if the response is a generator, decorate the generator@@ -189,8 +189,8 @@ class LlmTracker:self.client.record(self.llm_event)except Exception as e:self.client.record(ErrorEvent(trigger_event=self.llm_event, exception=e))- # TODO: This error is specific to only one path of failure. Should be more generic or have different logging for different paths- logging.warning(+ # TODO: This error is specific to only one path of failure. Should be more generic or have different logger for different paths+ logger.warning(f"🖇 AgentOps: Unable to parse a chunk for LLM call {kwargs} - skipping upload to AgentOps")return responsediff --git a/agentops/log_config.py b/agentops/log_config.pynew file mode 100644index 00000000..86cdecaf--- /dev/null+++ b/agentops/log_config.py@@ -0,0 +1,10 @@+import logging++logger = logging.getLogger("agentops")+logger.setLevel(logging.CRITICAL)++def set_logging_level_critial():+ logger.setLevel(logging.CRITICAL)++def set_logging_level_info():+ logger.setLevel(logging.INFO)\ No newline at end of filediff --git a/agentops/meta_client.py b/agentops/meta_client.pyindex d90d8035..b95973fa 100644--- a/agentops/meta_client.py+++ b/agentops/meta_client.py@@ -1,4 +1,4 @@-import logging+from .log_config import loggerimport tracebackfrom .host_env import get_host_env@@ -45,7 +45,7 @@ def handle_exceptions(method):try:return method(self, *args, **kwargs)except Exception as e:- logging.warning(f"🖇 AgentOps: Error: {e}")+ logger.warning(f"🖇 AgentOps: Error: {e}")config = getattr(self, 'config', None)if config is not None:type(self).send_exception_to_server(e, self.config._api_key)diff --git a/pyproject.toml b/pyproject.tomlindex 119e80c8..db81f3b6 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "agentops"-version = "0.1.5"+version = "0.1.6"authors = [{ name="Alex Reibman", email="[email protected]" },{ name="Shawn Qiu", email="[email protected]" },