You can instrument functions inside your code with the @operation decorator, which will create spans that track function execution, parameters, and return values. These operations will be displayed in your session visualization alongside LLM calls.
python
# Instrument a function as an operationfrom agentops.sdk.decorators import operation@operationdef process_data(data): # Your function logic here result = data.upper() return result
Track Agents
If you use specific named agents within your system, you can create agent spans that contain all downstream operations using the @agent decorator.
python
# Create an agent classfrom agentops.sdk.decorators import agent, operation@agentclass MyAgent: def __init__(self, name): self.name = name @operation def perform_task(self, task): # Agent task logic here return f"Completed {task}"
Creating Sessions
Create a session to group all your agent operations by using the @session decorator. Sessions serve as the root span for all operations.
python
# Create a sessionfrom agentops.sdk.decorators import session@sessiondef my_workflow(): # Your session code here agent = MyAgent("research-agent") result = agent.perform_task("data analysis") return result# Run the sessionmy_workflow()
import agentopsfrom agentops.sdk.decorators import session, agent, operation# Initialize AgentOpsagentops.init(<INSERT YOUR API KEY HERE>)# Create an agent class@agentclass MyAgent: def __init__(self, name): self.name = name @operation def perform_task(self, task): # Agent task logic here return f"Completed {task}"# Create a session@sessiondef my_workflow(): # Your session code here agent = MyAgent("research-agent") result = agent.perform_task("data analysis") return result# Run the sessionmy_workflow()
Simple Code Example
Jupyter Notebook with sample code that you can run!
That’s all you need to get started! Check out the documentation below to see how you can record other operations. AgentOps is a lot more powerful this way!