diff --git a/agentops/decorators.py b/agentops/decorators.py
index 0ea961e84..ce0d40506 100644
--- a/agentops/decorators.py
+++ b/agentops/decorators.py
@@ -156,6 +156,11 @@ def decorator(obj):
def new_init(self, *args, **kwargs):
try:
+ kwarg_name = kwargs.get("agentops_name", None)
+ if kwarg_name is not None:
+ self.agent_ops_agent_name = kwarg_name
+ del kwargs["agentops_name"]
+
original_init(self, *args, **kwargs)
if not Client().is_initialized:
diff --git a/docs/v1/usage/tracking-agents.mdx b/docs/v1/usage/tracking-agents.mdx
index 04c4ec417..a261951d5 100644
--- a/docs/v1/usage/tracking-agents.mdx
+++ b/docs/v1/usage/tracking-agents.mdx
@@ -19,6 +19,11 @@ class MyAgent:
...
```
+Alternatively, to name an agent in runtime, the `@track_agent` decorator creates an additional keyword argument in the class constructor. Using the above example, you can create an agent with a dynamic name with:
+```python
+research_agent = MyAgent(agentops_name='ResearchAgent')
+```
+
`trackagent.name` is optional.
If omitted, agent name defaults to the name of the class (e.g. MyAgent).
If an event does not originate from a tracked agent, agent name defaults to "Default Agent".
diff --git a/tests/test_agent.py b/tests/test_agent.py
new file mode 100644
index 000000000..abf7b12a0
--- /dev/null
+++ b/tests/test_agent.py
@@ -0,0 +1,34 @@
+from unittest import TestCase
+from unittest.mock import patch, MagicMock
+from uuid import uuid4
+
+from agentops import track_agent
+import agentops
+
+
+class TrackAgentTests(TestCase):
+ def test_track_agent_with_class(self):
+ agentops.init()
+
+ @track_agent(name="agent_name")
+ class TestAgentClass:
+ t = "a"
+ pass
+
+ obj = TestAgentClass()
+ self.assertTrue(isinstance(obj, TestAgentClass))
+ self.assertEqual(getattr(obj, "agent_ops_agent_name"), "agent_name")
+ self.assertIsNotNone(getattr(obj, "agent_ops_agent_id"))
+
+ def test_track_agent_with_class_name(self):
+ agentops.init()
+
+ @track_agent(name="agent_name")
+ class TestAgentClass:
+ t = "a"
+ pass
+
+ obj = TestAgentClass(agentops_name="agent1")
+ self.assertTrue(isinstance(obj, TestAgentClass))
+ self.assertEqual(getattr(obj, "agent_ops_agent_name"), "agent1")
+ self.assertIsNotNone(getattr(obj, "agent_ops_agent_id"))