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

non-agent LLM calls are not assigned to agents

Braelyn Boynton67025d7a2174
diff --git a/agentops/llm_tracker.py b/agentops/llm_tracker.py
index 01f7c522..d6bd1f70 100644
--- a/agentops/llm_tracker.py
+++ b/agentops/llm_tracker.py
@@ -8,13 +8,15 @@ from .event import Event
from .helpers import get_ISO_time
-def check_call_stack_for_agent_id(max_depth=5) -> str | None:
+def check_call_stack_for_agent_id() -> str | None:
# only looks at the last 2 frames otherwise it will retrieve
# whatever agent class was used last
- for frame_info in inspect.stack()[1:max_depth]:
+ for frame_info in inspect.stack():
# Get the locals from the current frame
local_vars = frame_info.frame.f_locals
for var in local_vars.values():
+ if var == "__main__":
+ return
# Instead of checking if var is a class, check if it's an instance with the _is_ao_agent attribute
if hasattr(var, '_is_ao_agent') and getattr(var, '_is_ao_agent'):
logging.info('LLM call from agent named: ' + getattr(var, '_ao_agent_name'))
diff --git a/examples/multi_agent_example.ipynb b/examples/multi_agent_example.ipynb
index 9b651f46..d7ef41b4 100644
--- a/examples/multi_agent_example.ipynb
+++ b/examples/multi_agent_example.ipynb
@@ -28,8 +28,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:28.818420Z",
- "start_time": "2024-03-08T01:29:28.583716Z"
+ "end_time": "2024-03-08T20:18:22.647026Z",
+ "start_time": "2024-03-08T20:18:22.400023Z"
}
},
"id": "7c566fac57d3b6ce",
@@ -47,8 +47,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:28.824542Z",
- "start_time": "2024-03-08T01:29:28.818894Z"
+ "end_time": "2024-03-08T20:18:22.647457Z",
+ "start_time": "2024-03-08T20:18:22.642757Z"
}
},
"id": "9f8c52496c04693",
@@ -61,7 +61,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
- "INFO:root:View info on this session at https://agentops.ai/dashboard?session_id=d31c041f-925a-41d9-ad0f-5131fb4eb49d\n"
+ "INFO:root:View info on this session at https://agentops.ai/dashboard?session_id=5193a9a7-aa7f-485d-843b-36518566e983\n"
]
}
],
@@ -72,8 +72,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:28.909156Z",
- "start_time": "2024-03-08T01:29:28.822059Z"
+ "end_time": "2024-03-08T20:18:22.714693Z",
+ "start_time": "2024-03-08T20:18:22.646071Z"
}
},
"id": "af062552554d60ce",
@@ -110,7 +110,7 @@
"data": {
"text/plain": "'Hello! How can I assist you today?'"
},
- "execution_count": 4,
+ "execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -128,12 +128,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:29.368056Z",
- "start_time": "2024-03-08T01:29:28.913164Z"
+ "end_time": "2024-03-08T20:18:31.386272Z",
+ "start_time": "2024-03-08T20:18:31.060728Z"
}
},
"id": "bf2875d35c8458ae",
- "execution_count": 4
+ "execution_count": 12
},
{
"cell_type": "code",
@@ -156,8 +156,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:29.368364Z",
- "start_time": "2024-03-08T01:29:29.365258Z"
+ "end_time": "2024-03-08T20:18:23.471733Z",
+ "start_time": "2024-03-08T20:18:23.466899Z"
}
},
"id": "727e3cc26ce3ec3",
@@ -174,8 +174,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:29.462582Z",
- "start_time": "2024-03-08T01:29:29.367402Z"
+ "end_time": "2024-03-08T20:18:23.565195Z",
+ "start_time": "2024-03-08T20:18:23.468586Z"
}
},
"id": "79b75d65de738522",
@@ -189,8 +189,8 @@
"output_type": "stream",
"text": [
"engineer\n",
- "6f7ee430-8f59-499b-ad2a-5fbf18d6ab65\n",
- "1197abd8-b9e7-4180-869a-929305f5b16b\n"
+ "fc947d29-c909-4470-bd33-f0da4687f81a\n",
+ "3ecfe8d2-df1a-4189-85b1-0736d4314843\n"
]
}
],
@@ -202,8 +202,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:29.468754Z",
- "start_time": "2024-03-08T01:29:29.463138Z"
+ "end_time": "2024-03-08T20:18:23.566003Z",
+ "start_time": "2024-03-08T20:18:23.563967Z"
}
},
"id": "513819204dde0e0a",
@@ -234,8 +234,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:30.330283Z",
- "start_time": "2024-03-08T01:29:29.465798Z"
+ "end_time": "2024-03-08T20:18:24.427146Z",
+ "start_time": "2024-03-08T20:18:23.566776Z"
}
},
"id": "69e76061a626549",
@@ -259,8 +259,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:30.330752Z",
- "start_time": "2024-03-08T01:29:30.326284Z"
+ "end_time": "2024-03-08T20:18:24.427763Z",
+ "start_time": "2024-03-08T20:18:24.424968Z"
}
},
"id": "830b86dac47dceb3",
@@ -291,8 +291,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:33.158991Z",
- "start_time": "2024-03-08T01:29:30.329965Z"
+ "end_time": "2024-03-08T20:18:27.148493Z",
+ "start_time": "2024-03-08T20:18:24.429637Z"
}
},
"id": "63c9d0d457aee91a",
@@ -304,7 +304,7 @@
{
"data": {
"text/plain": "<IPython.core.display.Markdown object>",
- "text/markdown": "```python\nimport unittest\n\ndef multiply_and_divide(num1, num2):\n result = (num1 * num2) / 2\n return result\n\nclass TestMultiplyAndDivide(unittest.TestCase):\n \n def test_multiply_and_divide(self):\n self.assertEqual(multiply_and_divide(4, 2), 4)\n self.assertEqual(multiply_and_divide(5, 3), 7.5)\n self.assertEqual(multiply_and_divide(0, 10), 0)\n self.assertEqual(multiply_and_divide(-4, 2), -4)\n \nif __name__ == '__main__':\n unittest.main()\n```"
+ "text/markdown": "```python\nimport unittest\n\ndef multiply_and_divide(num1, num2):\n result = (num1 * num2) / 2\n return result\n\nclass TestMultiplyAndDivide(unittest.TestCase):\n\n def test_multiply_and_divide(self):\n self.assertEqual(multiply_and_divide(4, 2), 4.0)\n self.assertEqual(multiply_and_divide(5, 3), 7.5)\n self.assertEqual(multiply_and_divide(0, 10), 0.0)\n self.assertEqual(multiply_and_divide(-6, 3), -9.0)\n\nif __name__ == '__main__':\n unittest.main()\n```"
},
"metadata": {},
"output_type": "display_data"
@@ -316,8 +316,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:33.159484Z",
- "start_time": "2024-03-08T01:29:33.155654Z"
+ "end_time": "2024-03-08T20:18:27.151987Z",
+ "start_time": "2024-03-08T20:18:27.148793Z"
}
},
"id": "a88ffcbd2015d422",
@@ -330,8 +330,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:29:33.160422Z",
- "start_time": "2024-03-08T01:29:33.158613Z"
+ "end_time": "2024-03-08T20:18:27.155679Z",
+ "start_time": "2024-03-08T20:18:27.151979Z"
}
},
"id": "e5003033f93eb21b",