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

agent instances have unique ids

Braelyn Boynton10e97f70708b
diff --git a/agentops/agent.py b/agentops/agent.py
index a7542880..c296960b 100644
--- a/agentops/agent.py
+++ b/agentops/agent.py
@@ -5,12 +5,17 @@ from agentops import Client
def track_agent(name: str | None):
def class_decorator(cls):
cls._is_ao_agent = True
- cls._ao_agent_id = str(uuid.uuid4())
cls._ao_agent_name = name or cls.__name__
- # create agent record
- ao_client = Client()
- ao_client.create_agent(cls._ao_agent_id, cls._ao_agent_name)
+ original_init = cls.__init__
+
+ def new_init(self, *args, **kwargs):
+ self._ao_agent_id = str(uuid.uuid4())
+ ao_client = Client()
+ ao_client.create_agent(self._ao_agent_id, self._ao_agent_name)
+ original_init(self, *args, **kwargs)
+
+ cls.__init__ = new_init
return cls
return class_decorator
diff --git a/examples/multi_agent_example.ipynb b/examples/multi_agent_example.ipynb
index 9818fd19..9b651f46 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:10:43.313773Z",
- "start_time": "2024-03-08T01:10:43.072138Z"
+ "end_time": "2024-03-08T01:29:28.818420Z",
+ "start_time": "2024-03-08T01:29:28.583716Z"
}
},
"id": "7c566fac57d3b6ce",
@@ -47,8 +47,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:10:43.320611Z",
- "start_time": "2024-03-08T01:10:43.315050Z"
+ "end_time": "2024-03-08T01:29:28.824542Z",
+ "start_time": "2024-03-08T01:29:28.818894Z"
}
},
"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=47aef9e1-94d3-4e69-8d75-d40c453b5b74\n"
+ "INFO:root:View info on this session at https://agentops.ai/dashboard?session_id=d31c041f-925a-41d9-ad0f-5131fb4eb49d\n"
]
}
],
@@ -72,8 +72,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:10:43.398524Z",
- "start_time": "2024-03-08T01:10:43.318539Z"
+ "end_time": "2024-03-08T01:29:28.909156Z",
+ "start_time": "2024-03-08T01:29:28.822059Z"
}
},
"id": "af062552554d60ce",
@@ -91,7 +91,30 @@
},
{
"cell_type": "code",
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Unable to parse a chunk for LLM call {'model': 'gpt-3.5-turbo', 'messages': [{'role': 'system', 'content': 'You are not a tracked agent'}, {'role': 'user', 'content': 'Say hello'}]} - skipping upload to AgentOps\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": "'Hello! How can I assist you today?'"
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"res = openai_client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
@@ -104,13 +127,13 @@
],
"metadata": {
"collapsed": false,
- "is_executing": true,
"ExecuteTime": {
- "start_time": "2024-03-08T01:11:26.561763Z"
+ "end_time": "2024-03-08T01:29:29.368056Z",
+ "start_time": "2024-03-08T01:29:28.913164Z"
}
},
"id": "bf2875d35c8458ae",
- "execution_count": null
+ "execution_count": 4
},
{
"cell_type": "code",
@@ -133,54 +156,58 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:10:55.986322Z",
- "start_time": "2024-03-08T01:10:55.905312Z"
+ "end_time": "2024-03-08T01:29:29.368364Z",
+ "start_time": "2024-03-08T01:29:29.365258Z"
}
},
"id": "727e3cc26ce3ec3",
- "execution_count": 6
+ "execution_count": 5
},
{
"cell_type": "code",
"outputs": [],
"source": [
"qa = QaAgent()\n",
+ "qa2 = QaAgent()\n",
"engineer = EngineerAgent()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:10:56.511730Z",
- "start_time": "2024-03-08T01:10:56.506571Z"
+ "end_time": "2024-03-08T01:29:29.462582Z",
+ "start_time": "2024-03-08T01:29:29.367402Z"
}
},
"id": "79b75d65de738522",
- "execution_count": 7
+ "execution_count": 6
},
{
"cell_type": "code",
"outputs": [
{
- "data": {
- "text/plain": "'engineer'"
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "engineer\n",
+ "6f7ee430-8f59-499b-ad2a-5fbf18d6ab65\n",
+ "1197abd8-b9e7-4180-869a-929305f5b16b\n"
+ ]
}
],
"source": [
- "engineer._ao_agent_name"
+ "print(engineer._ao_agent_name)\n",
+ "print(qa._ao_agent_id)\n",
+ "print(qa2._ao_agent_id)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:10:56.705542Z",
- "start_time": "2024-03-08T01:10:56.701470Z"
+ "end_time": "2024-03-08T01:29:29.468754Z",
+ "start_time": "2024-03-08T01:29:29.463138Z"
}
},
"id": "513819204dde0e0a",
- "execution_count": 8
+ "execution_count": 7
},
{
"cell_type": "code",
@@ -197,22 +224,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "<agentops.llm_tracker.LlmTracker object at 0x1074d8250>\n",
- "ChatCompletion(id='chatcmpl-90J3B0QxoJxMWjCGnqOKjXLAWwk3T', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='def multiply_and_divide(num1, num2):\\n result = (num1 * num2) / 2\\n return result', role='assistant', function_call=None, tool_calls=None))], created=1709860257, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint='fp_2b778c6b35', usage=CompletionUsage(completion_tokens=28, prompt_tokens=48, total_tokens=76))\n",
- "{'model': 'gpt-3.5-turbo', 'messages': [{'role': 'system', 'content': 'You are a software engineer and only output python code, no markdown tags.'}, {'role': 'user', 'content': 'Write a python function that accepts two numbers and multiplies them together, then divides by two. No example.'}], 'temperature': 0.5}\n",
- "2024-03-08T01:10:56.869Z\n",
- "<class 'openai.Stream'>\n",
- "<class 'openai.AsyncStream'>\n",
- "<class 'openai.types.chat.chat_completion_chunk.ChatCompletionChunk'>\n",
- "<class 'openai.resources.completions.AsyncCompletions'>\n",
- "<function LlmTracker._handle_response_v1_openai.<locals>.handle_stream_chunk at 0x10948fb00>\n",
- "(<openai.resources.chat.completions.Completions object at 0x108a1e2d0>,)\n",
- "{'model': 'gpt-3.5-turbo', 'messages': [{'role': 'system', 'content': 'You are a software engineer and only output python code, no markdown tags.'}, {'role': 'user', 'content': 'Write a python function that accepts two numbers and multiplies them together, then divides by two. No example.'}], 'temperature': 0.5}\n",
- "2024-03-08T01:10:56.869Z\n",
- "ChatCompletion(id='chatcmpl-90J3B0QxoJxMWjCGnqOKjXLAWwk3T', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='def multiply_and_divide(num1, num2):\\n result = (num1 * num2) / 2\\n return result', role='assistant', function_call=None, tool_calls=None))], created=1709860257, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint='fp_2b778c6b35', usage=CompletionUsage(completion_tokens=28, prompt_tokens=48, total_tokens=76))\n",
- "<function Completions.create at 0x108901800>\n",
- "<agentops.llm_tracker.LlmTracker object at 0x1074d8250>\n",
- "<__main__.EngineerAgent object at 0x1094e2090>\n",
"Unable to parse a chunk for LLM call {'model': 'gpt-3.5-turbo', 'messages': [{'role': 'system', 'content': 'You are a software engineer and only output python code, no markdown tags.'}, {'role': 'user', 'content': 'Write a python function that accepts two numbers and multiplies them together, then divides by two. No example.'}], 'temperature': 0.5} - skipping upload to AgentOps\n"
]
}
@@ -223,12 +234,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:10:58.082806Z",
- "start_time": "2024-03-08T01:10:56.872314Z"
+ "end_time": "2024-03-08T01:29:30.330283Z",
+ "start_time": "2024-03-08T01:29:29.465798Z"
}
},
"id": "69e76061a626549",
- "execution_count": 9
+ "execution_count": 8
},
{
"cell_type": "code",
@@ -248,12 +259,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:10:58.083272Z",
- "start_time": "2024-03-08T01:10:58.076792Z"
+ "end_time": "2024-03-08T01:29:30.330752Z",
+ "start_time": "2024-03-08T01:29:30.326284Z"
}
},
"id": "830b86dac47dceb3",
- "execution_count": 10
+ "execution_count": 9
},
{
"cell_type": "code",
@@ -270,22 +281,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "<agentops.llm_tracker.LlmTracker object at 0x1074d8250>\n",
- "ChatCompletion(id='chatcmpl-90J3COBfcX26PVyuihVOXdOvG3jJW', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=\"import 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(0, 5), 0)\\n self.assertEqual(multiply_and_divide(-3, 6), -9)\\n\\nif __name__ == '__main__':\\n unittest.main()\", role='assistant', function_call=None, tool_calls=None))], created=1709860258, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint='fp_2b778c6b35', usage=CompletionUsage(completion_tokens=113, prompt_tokens=66, total_tokens=179))\n",
- "{'model': 'gpt-3.5-turbo', 'messages': [{'role': 'system', 'content': 'You are a qa engineer and only output python code, no markdown tags.'}, {'role': 'user', 'content': 'Write a python unit test that test the following function: \\n def multiply_and_divide(num1, num2):\\n result = (num1 * num2) / 2\\n return result'}], 'temperature': 0.5}\n",
- "2024-03-08T01:10:58.078Z\n",
- "<class 'openai.Stream'>\n",
- "<class 'openai.AsyncStream'>\n",
- "<class 'openai.types.chat.chat_completion_chunk.ChatCompletionChunk'>\n",
- "<class 'openai.resources.completions.AsyncCompletions'>\n",
- "<function LlmTracker._handle_response_v1_openai.<locals>.handle_stream_chunk at 0x1094b7420>\n",
- "(<openai.resources.chat.completions.Completions object at 0x108a1e2d0>,)\n",
- "{'model': 'gpt-3.5-turbo', 'messages': [{'role': 'system', 'content': 'You are a qa engineer and only output python code, no markdown tags.'}, {'role': 'user', 'content': 'Write a python unit test that test the following function: \\n def multiply_and_divide(num1, num2):\\n result = (num1 * num2) / 2\\n return result'}], 'temperature': 0.5}\n",
- "2024-03-08T01:10:58.078Z\n",
- "ChatCompletion(id='chatcmpl-90J3COBfcX26PVyuihVOXdOvG3jJW', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=\"import 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(0, 5), 0)\\n self.assertEqual(multiply_and_divide(-3, 6), -9)\\n\\nif __name__ == '__main__':\\n unittest.main()\", role='assistant', function_call=None, tool_calls=None))], created=1709860258, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint='fp_2b778c6b35', usage=CompletionUsage(completion_tokens=113, prompt_tokens=66, total_tokens=179))\n",
- "<function Completions.create at 0x108901800>\n",
- "<agentops.llm_tracker.LlmTracker object at 0x1074d8250>\n",
- "<__main__.QaAgent object at 0x1094e3050>\n",
"Unable to parse a chunk for LLM call {'model': 'gpt-3.5-turbo', 'messages': [{'role': 'system', 'content': 'You are a qa engineer and only output python code, no markdown tags.'}, {'role': 'user', 'content': 'Write a python unit test that test the following function: \\n def multiply_and_divide(num1, num2):\\n result = (num1 * num2) / 2\\n return result'}], 'temperature': 0.5} - skipping upload to AgentOps\n"
]
}
@@ -296,12 +291,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:11:00.069430Z",
- "start_time": "2024-03-08T01:10:58.080774Z"
+ "end_time": "2024-03-08T01:29:33.158991Z",
+ "start_time": "2024-03-08T01:29:30.329965Z"
}
},
"id": "63c9d0d457aee91a",
- "execution_count": 11
+ "execution_count": 10
},
{
"cell_type": "code",
@@ -309,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(0, 5), 0)\n self.assertEqual(multiply_and_divide(-3, 6), -9)\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)\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```"
},
"metadata": {},
"output_type": "display_data"
@@ -321,12 +316,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "end_time": "2024-03-08T01:11:00.070307Z",
- "start_time": "2024-03-08T01:11:00.067841Z"
+ "end_time": "2024-03-08T01:29:33.159484Z",
+ "start_time": "2024-03-08T01:29:33.155654Z"
}
},
"id": "a88ffcbd2015d422",
- "execution_count": 12
+ "execution_count": 11
},
{
"cell_type": "code",
@@ -335,11 +330,12 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
- "start_time": "2024-03-08T01:10:44.447430Z"
+ "end_time": "2024-03-08T01:29:33.160422Z",
+ "start_time": "2024-03-08T01:29:33.158613Z"
}
},
"id": "e5003033f93eb21b",
- "execution_count": null
+ "execution_count": 11
}
],
"metadata": {