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

added cleanup

reibsc64ccd629c4d
diff --git a/agentops/agentops.py b/agentops/agentops.py
index cc194aed..f7410c07 100644
--- a/agentops/agentops.py
+++ b/agentops/agentops.py
@@ -12,6 +12,7 @@ from uuid import uuid4
from typing import Optional, Dict
import functools
import inspect
+import atexit
class AgentOps:
@@ -33,6 +34,10 @@ class AgentOps:
self.config.api_key = api_key
self.start_session(tags)
+ # Store a reference to the instance
+ AgentOps._instance = self
+ atexit.register(self.cleanup)
+
def record(self, event: Event):
"""
Record an event with the AgentOps service.
@@ -115,3 +120,8 @@ class AgentOps:
self.session.end_session(end_state, rating)
self.worker.end_session(self.session)
+
+ def cleanup(self):
+ # Only run cleanup function if session is created
+ if hasattr(self, "session"):
+ self.end_session(end_state=EventState.FAIL)