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
Moved default values down to Configuration which is lowest level, not Config (#126)
Howard Gilb4fdc1c345e5
diff --git a/agentops/client.py b/agentops/client.pyindex 92ba1844..7580b8ca 100644--- a/agentops/client.py+++ b/agentops/client.py@@ -13,7 +13,6 @@ from .worker import Workerfrom .host_env import get_host_envfrom uuid import uuid4from typing import Optional, List-from os import environimport tracebackimport loggingimport inspect@@ -22,7 +21,7 @@ import signalimport sysfrom .meta_client import MetaClient-from .config import Configuration+from .config import Configuration, ConfigurationErrorfrom .llm_tracker import LlmTracker@@ -32,25 +31,31 @@ class Client(metaclass=MetaClient):Client for AgentOps service.Args:+api_key (str, optional): API Key for AgentOps services. If none is provided, key willbe read from the AGENTOPS_API_KEY environment variable.- tags (List[str], optional): Tags for the sessions that can be used for grouping or- sorting later (e.g. ["GPT-4"]).- endpoint (str, optional): The endpoint for the AgentOps service. Defaults to 'https://api.agentops.ai'.+ parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. If none is provided, key will+ be read from the AGENTOPS_PARENT_KEY environment variable.+ endpoint (str, optional): The endpoint for the AgentOps service. If none is provided, key will+ be read from the AGENTOPS_API_ENDPOINT environment variable. Defaults to 'https://api.agentops.ai'.max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue.- Defaults to 1000.+ Defaults to 30,000 (30 seconds)max_queue_size (int, optional): The maximum size of the event queue. Defaults to 100.+ tags (List[str], optional): Tags for the sessions that can be used for grouping or+ sorting later (e.g. ["GPT-4"]).override (bool): Whether to override and LLM calls to emit as events.+ auto_start_session (bool): Whether to start a session automatically when the client is created.Attributes:_session (Session, optional): A Session is a grouping of events (e.g. a run of your agent)."""- def __init__(self, api_key: Optional[str] = None,+ def __init__(self,+ api_key: Optional[str] = None,parent_key: Optional[str] = None,- tags: Optional[List[str]] = None,endpoint: Optional[str] = None,max_wait_time: Optional[int] = None,max_queue_size: Optional[int] = None,+ tags: Optional[List[str]] = None,override=True,auto_start_session=True):@@ -58,27 +63,15 @@ class Client(metaclass=MetaClient):self._session = Noneself._worker = Noneself._tags = tags- self.config = None-- # These handle the case where .init() is used with optionals, and one of these- # params are None, which is does not trigger the Optional default in the constructor- if not api_key:- api_key = environ.get("AGENTOPS_API_KEY")- if not api_key:- logging.warning("AgentOps: No API key provided - no data will be recorded.")- return-- if not parent_key:- parent_key = environ.get('AGENTOPS_PARENT_KEY', None)-- if not endpoint:- endpoint = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai')-- self.config = Configuration(api_key,- parent_key,- endpoint,- max_wait_time,- max_queue_size)++ try:+ self.config = Configuration(api_key,+ parent_key,+ endpoint,+ max_wait_time,+ max_queue_size)+ except ConfigurationError:+ returnself._handle_unclean_exits()diff --git a/agentops/config.py b/agentops/config.pyindex 9779cfbb..2bd56262 100644--- a/agentops/config.py+++ b/agentops/config.py@@ -7,6 +7,7 @@ Classes:from typing import Optionalfrom os import environ+import loggingclass Configuration:@@ -14,17 +15,34 @@ class Configuration:Stores the configuration settings for AgentOps clients.Args:- api_key (str): API Key for AgentOps services- endpoint (str, optional): The endpoint for the AgentOps service. Defaults to 'https://api.agentops.ai'.+ api_key (str, optional): API Key for AgentOps services. If none is provided, key will+ be read from the AGENTOPS_API_KEY environment variable.+ parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. If none is provided, key will+ be read from the AGENTOPS_PARENT_KEY environment variable.+ endpoint (str, optional): The endpoint for the AgentOps service. If none is provided, key will+ be read from the AGENTOPS_API_ENDPOINT environment variable. Defaults to 'https://api.agentops.ai'.max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue. Defaults to 30000.max_queue_size (int, optional): The maximum size of the event queue. Defaults to 100."""- def __init__(self, api_key: str,- parent_key: Optional[str],- endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'),+ def __init__(self,+ api_key: Optional[str] = None,+ parent_key: Optional[str] = None,+ endpoint: Optional[str] = None,max_wait_time: Optional[int] = None,max_queue_size: Optional[int] = None):++ if not api_key:+ api_key = environ.get('AGENTOPS_API_KEY', None)+ if not api_key:+ raise ConfigurationError("AgentOps: No API key provided - no data will be recorded.")++ if not parent_key:+ parent_key = environ.get('AGENTOPS_PARENT_KEY', None)++ if not endpoint:+ endpoint = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai')+self._api_key: str = api_keyself._endpoint = endpointself._max_wait_time = max_wait_time or 30000@@ -118,3 +136,11 @@ class Configuration:@parent_key.setterdef parent_key(self, value: str):self._parent_key = value+++class ConfigurationError(Exception):+ """Exception raised for errors related to Configuration"""++ def __init__(self, message: str):+ super().__init__(message)+ logging.warning(message)diff --git a/agentops/meta_client.py b/agentops/meta_client.pyindex 17de1b56..29f78588 100644--- a/agentops/meta_client.py+++ b/agentops/meta_client.py@@ -45,8 +45,10 @@ def handle_exceptions(method):try:return method(self, *args, **kwargs)except Exception as e:- type(self).send_exception_to_server(e, self.config._api_key)logging.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)raise e- return wrapper\ No newline at end of file+ return wrapperdiff --git a/agentops/worker.py b/agentops/worker.pyindex d4410c24..d409ee10 100644--- a/agentops/worker.py+++ b/agentops/worker.py@@ -2,7 +2,7 @@ import jsonimport threadingimport timefrom .http_client import HttpClient-from .config import Configuration+from .config import Configuration, ConfigurationErrorfrom .session import Sessionfrom .helpers import safe_serialize, filter_unjsonablefrom typing import Dictdiff --git a/tests/openai_handlers/_test_handler_openai_v1.py b/tests/openai_handlers/_test_handler_openai_v1.pyindex 97ea1795..e5843e77 100644--- a/tests/openai_handlers/_test_handler_openai_v1.py+++ b/tests/openai_handlers/_test_handler_openai_v1.py@@ -1,4 +1,5 @@import time+import openaifrom openai.resources.chat import completionsfrom openai import OpenAI, AsyncOpenAIimport agentops