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

parent key support

Braelyn Boynton1f56ff0c2324
diff --git a/agentops/__init__.py b/agentops/__init__.py
index e99bdea1..50e6818e 100755
--- a/agentops/__init__.py
+++ b/agentops/__init__.py
@@ -1,4 +1,5 @@
# agentops/__init__.py
+from os import environ
from typing import Optional, List
from .client import Client
@@ -12,13 +13,21 @@ from os import environ
def init(api_key: Optional[str] = None,
+ parent_key: Optional[str] = None,
tags: Optional[List[str]] = None,
endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'),
max_wait_time: Optional[int] = 1000,
max_queue_size: Optional[int] = 100,
override=True,
auto_start_session=True):
- Client(api_key, tags, endpoint, max_wait_time, max_queue_size, override, auto_start_session)
+
+ 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')
+
+ Client(api_key, parent_key, tags, endpoint, max_wait_time, max_queue_size, override, auto_start_session)
def end_session(end_state: str = Field("Indeterminate",
diff --git a/agentops/client.py b/agentops/client.py
index abde272d..7bded911 100644
--- a/agentops/client.py
+++ b/agentops/client.py
@@ -46,6 +46,7 @@ class Client(metaclass=MetaClient):
"""
def __init__(self, api_key: Optional[str] = None,
+ parent_key: Optional[str] = environ.get('AGENTOPS_PARENT_KEY', None),
tags: Optional[List[str]] = None,
endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'),
max_wait_time: Optional[int] = 1000,
@@ -64,6 +65,7 @@ class Client(metaclass=MetaClient):
return
self.config = Configuration(api_key or environ.get('AGENTOPS_API_KEY'),
+ parent_key,
endpoint,
max_wait_time,
max_queue_size)
@@ -71,7 +73,7 @@ class Client(metaclass=MetaClient):
self._handle_unclean_exits()
if auto_start_session:
- self.start_session(tags)
+ self.start_session(tags, self.config)
if override:
if 'openai' in sys.modules:
diff --git a/agentops/config.py b/agentops/config.py
index 2755916c..19cc2aa4 100644
--- a/agentops/config.py
+++ b/agentops/config.py
@@ -21,12 +21,14 @@ class Configuration:
"""
def __init__(self, api_key: str,
+ parent_key: Optional[str],
endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'),
max_wait_time: Optional[int] = 1000, max_queue_size: Optional[int] = 100):
self._api_key: str = api_key
self._endpoint = endpoint
self._max_wait_time = max_wait_time
self._max_queue_size = max_queue_size
+ self._parent_key: Optional[str] = parent_key
@property
def api_key(self) -> str:
@@ -107,3 +109,11 @@ class Configuration:
value (int): The new maximum size of the event queue.
"""
self._max_queue_size = value
+
+ @property
+ def parent_key(self):
+ return self._parent_key
+
+ @parent_key.setter
+ def parent_key(self, value: str):
+ self._parent_key = value
diff --git a/agentops/http_client.py b/agentops/http_client.py
index 323e0d47..d538fc93 100644
--- a/agentops/http_client.py
+++ b/agentops/http_client.py
@@ -61,16 +61,20 @@ class Response:
class HttpClient:
@staticmethod
- def post(url: str, payload: bytes, api_key: Optional[str] = None, header=None) -> Response:
+ def post(url: str, payload: bytes, api_key: Optional[str] = None, parent_key: Optional[str] = None,
+ header=None) -> Response:
result = Response()
try:
# Create request session with retries configured
request_session = requests.Session()
request_session.mount(url, HTTPAdapter(max_retries=retry_config))
- if api_key != None:
+ if api_key is not None:
JSON_HEADER["X-Agentops-Auth"] = api_key
+ if parent_key is not None:
+ JSON_HEADER["X-Agentops-Parent-Key"] = parent_key
+
res = request_session.post(url, data=payload,
headers=JSON_HEADER, timeout=20)
diff --git a/agentops/worker.py b/agentops/worker.py
index bc930d69..127a502b 100644
--- a/agentops/worker.py
+++ b/agentops/worker.py
@@ -39,7 +39,8 @@ class Worker:
serialized_payload = safe_serialize(payload).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/events',
serialized_payload,
- self.config.api_key)
+ self.config.api_key,
+ self.config.parent_key)
def start_session(self, session: Session) -> None:
self._session = session
@@ -50,7 +51,8 @@ class Worker:
serialized_payload = json.dumps(filter_unjsonable(payload)).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/sessions',
serialized_payload,
- self.config.api_key)
+ self.config.api_key,
+ self.config.parent_key)
def end_session(self, session: Session) -> None:
self.stop_flag.set()
@@ -66,7 +68,8 @@ class Worker:
HttpClient.post(f'{self.config.endpoint}/sessions',
json.dumps(filter_unjsonable(
payload)).encode("utf-8"),
- self.config.api_key)
+ self.config.api_key,
+ self.config.parent_key)
def update_session(self, session: Session) -> None:
with self.lock:
@@ -78,7 +81,7 @@ class Worker:
json.dumps(filter_unjsonable(
payload)).encode("utf-8"),
self.config.api_key,
- self.config.org_key)
+ self.config.parent_key)
def create_agent(self, agent_id, name):
payload = {
@@ -91,7 +94,8 @@ class Worker:
safe_serialize(payload).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/agents',
serialized_payload,
- self.config.api_key)
+ self.config.api_key,
+ self.config.parent_key)
def run(self) -> None:
while not self.stop_flag.is_set():