Skip to content

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

Moved default values down to Configuration which is lowest level, not Config (#126)

Howard Gilb4fdc1c345e5
diff --git a/agentops/client.py b/agentops/client.py
index 92ba1844..7580b8ca 100644
--- a/agentops/client.py
+++ b/agentops/client.py
@@ -13,7 +13,6 @@ from .worker import Worker
from .host_env import get_host_env
from uuid import uuid4
from typing import Optional, List
-from os import environ
import traceback
import logging
import inspect
@@ -22,7 +21,7 @@ import signal
import sys
from .meta_client import MetaClient
-from .config import Configuration
+from .config import Configuration, ConfigurationError
from .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 will
be 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 = None
self._worker = None
self._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:
+ return
self._handle_unclean_exits()
diff --git a/agentops/config.py b/agentops/config.py
index 9779cfbb..2bd56262 100644
--- a/agentops/config.py
+++ b/agentops/config.py
@@ -7,6 +7,7 @@ Classes:
from typing import Optional
from os import environ
+import logging
class 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_key
self._endpoint = endpoint
self._max_wait_time = max_wait_time or 30000
@@ -118,3 +136,11 @@ class Configuration:
@parent_key.setter
def 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.py
index 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 wrapper
diff --git a/agentops/worker.py b/agentops/worker.py
index d4410c24..d409ee10 100644
--- a/agentops/worker.py
+++ b/agentops/worker.py
@@ -2,7 +2,7 @@ import json
import threading
import time
from .http_client import HttpClient
-from .config import Configuration
+from .config import Configuration, ConfigurationError
from .session import Session
from .helpers import safe_serialize, filter_unjsonable
from typing import Dict
diff --git a/tests/openai_handlers/_test_handler_openai_v1.py b/tests/openai_handlers/_test_handler_openai_v1.py
index 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 openai
from openai.resources.chat import completions
from openai import OpenAI, AsyncOpenAI
import agentops