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
set end timestamp if not set before record (#179)
Braelyn Boynton2a0ee739b991
diff --git a/agentops/client.py b/agentops/client.pyindex 544285ef..3ed8f1cb 100644--- a/agentops/client.py+++ b/agentops/client.py@@ -75,6 +75,7 @@ class Client(metaclass=MetaClient):self._session = Noneself._worker = None+ self._tags_for_future_session = Noneself._env_data_opt_out = os.getenv('AGENTOPS_ENV_DATA_OPT_OUT') and os.getenv('AGENTOPS_ENV_DATA_OPT_OUT').lower() == 'true'@@ -135,7 +136,8 @@ class Client(metaclass=MetaClient):Args:event (Event): The event to record."""-+ if not event.end_timestamp or event.init_timestamp == event.end_timestamp:+ event.end_timestamp = get_ISO_time()if self._session is not None and not self._session.has_ended:if isinstance(event, ErrorEvent):if event.trigger_event:@@ -270,13 +272,13 @@ class Client(metaclass=MetaClient):self._session.video = videoself._session.end_session(end_state, end_state_reason)- token_cost = Decimal(self._worker.end_session(self._session))+ token_cost = self._worker.end_session(self._session)if token_cost == 'unknown':print('🖇 AgentOps: Could not determine cost of run.')else:-+ token_cost_d = Decimal(token_cost)print('🖇 AgentOps: This run cost ${}'.format('{:.2f}'.format(- token_cost) if token_cost == 0 else '{:.6f}'.format(token_cost)))+ token_cost_d) if token_cost_d == 0 else '{:.6f}'.format(token_cost_d)))self._session = Noneself._worker = Nonediff --git a/tests/test_events.py b/tests/test_events.pynew file mode 100644index 00000000..4e156bdf--- /dev/null+++ b/tests/test_events.py@@ -0,0 +1,29 @@+import time+import requests_mock+import pytest+import agentops+from agentops import ActionEvent+++def mock_req():+ with requests_mock.Mocker() as m:+ url = 'https://api.agentops.ai'+ m.post(url + '/events', text='ok')+ m.post(url + '/sessions', json={'status': 'success', 'token_cost': 5})+ yield m++class TestEvents:+ def setup_method(self):+ self.api_key = "random_api_key"+ self.event_type = 'test_event_type'+ self.config = agentops.Configuration(api_key=self.api_key, max_wait_time=50, max_queue_size=1)++ def test_record_timestamp(self, mock_req):+ agentops.init(api_key=self.api_key)++ event = ActionEvent()+ time.sleep(0.15)+ agentops.record(event)++ assert event.init_timestamp != event.end_timestamp\ No newline at end of file