AgentOps
AgentOps is the developer favorite platform for testing, debugging, and deploying AI agents and LLM apps. Monitor, analyze, and optimize your agent workflows with comprehensive observability and analytics.
Repository Overview
Observability and DevTool platform for AI Agents
AgentOps helps developers build, evaluate, and monitor AI agents. From prototype to production.
Key Integrations
Quick Start
pip install agentops#### Session replays in 2 lines of code
Initialize the AgentOps client and automatically get analytics on all your LLM calls.
import agentopsBeginning of your program (i.e. main.py, __init__.py)
agentops.init( )...
End of program
agentops.end_session('Success')All your sessions can be viewed on the AgentOps dashboard
Agent Debugging
Session Replays
Summary Analytics
First class Developer Experience
Add powerful observability to your agents, tools, and functions with as little code as possible: one line at a time.
Refer to our documentation
Create a session span (root for all other spans)
from agentops.sdk.decorators import session@session
def my_workflow():
# Your session code here
return result
Create an agent span for tracking agent operations
from agentops.sdk.decorators import agent@agent
class MyAgent:
def __init__(self, name):
self.name = name
# Agent methods here
Create operation/task spans for tracking specific operations
from agentops.sdk.decorators import operation, task@operation # or @task
def process_data(data):
# Process the data
return result
Create workflow spans for tracking multi-operation workflows
from agentops.sdk.decorators import workflow@workflow
def my_workflow(data):
# Workflow implementation
return result
Nest decorators for proper span hierarchy
from agentops.sdk.decorators import session, agent, operation@agent
class MyAgent:
@operation
def nested_operation(self, message):
return f"Processed: {message}"
@operation
def main_operation(self):
result = self.nested_operation("test message")
return result
@session
def my_session():
agent = MyAgent()
return agent.main_operation()
All decorators support:
- Input/Output Recording
- Exception Handling
- Async/await functions
- Generator functions
- Custom attributes and names
Integrations
OpenAI Agents SDK
Build multi-agent systems with tools, handoffs, and guardrails. AgentOps natively integrates with the OpenAI Agents SDKs for both Python and TypeScript.
#### Python
pip install openai-agents- Python integration guide
- OpenAI Agents Python documentation
#### TypeScript
npm install agentops @openai/agents- TypeScript integration guide
- OpenAI Agents JS documentation
CrewAI
Build Crew agents with observability in just 2 lines of code. Simply set an AGENTOPS_API_KEY in your environment, and your crews will get automatic monitoring on the AgentOps dashboard.
pip install 'crewai[agentops]'- AgentOps integration example
- Official CrewAI documentation
AG2
With only two lines of code, add full observability and monitoring to AG2 (formerly AutoGen) agents. Set an
AGENTOPS_API_KEY in your environment and call agentops.init()- AG2 Observability Example
- AG2 - AgentOps Documentation
Camel AI
Track and analyze CAMEL agents with full observability. Set an AGENTOPS_API_KEY in your environment and initialize AgentOps to get started.
- Camel AI - Advanced agent communication framework
- AgentOps integration example
- Official Camel AI documentation
Installation
pip install "camel-ai[all]==0.2.11"
pip install agentopsimport os
import agentops
from camel.agents import ChatAgent
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelTypeInitialize AgentOps
agentops.init(os.getenv("AGENTOPS_API_KEY"), tags=["CAMEL Example"])Import toolkits after AgentOps init for tracking
from camel.toolkits import SearchToolkitSet up the agent with search tools
sys_msg = BaseMessage.make_assistant_message(
role_name='Tools calling operator',
content='You are a helpful assistant'
)Configure tools and model
tools = [*SearchToolkit().get_tools()]
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
)Create and run the agent
camel_agent = ChatAgent(
system_message=sys_msg,
model=model,
tools=tools,
)response = camel_agent.step("What is AgentOps?")
print(response)
agentops.end_session("Success")
Check out our Camel integration guide for more examples including multi-agent scenarios.
Langchain
AgentOps works seamlessly with applications built using Langchain. To use the handler, install Langchain as an optional dependency:
Installation
pip install agentops[langchain]To use the handler, import and set
import os
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from agentops.integration.callbacks.langchain import LangchainCallbackHandlerAGENTOPS_API_KEY = os.environ['AGENTOPS_API_KEY']
handler = LangchainCallbackHandler(api_key=AGENTOPS_API_KEY, tags=['Langchain Example'])
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY,
callbacks=[handler],
model='gpt-3.5-turbo')
agent = initialize_agent(tools,
llm,
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
callbacks=[handler], # You must pass in a callback handler to record your agent
handle_parsing_errors=True)
Check out the Langchain Examples Notebook for more details including Async handlers.
Cohere
First class support for Cohere(>=5.4.0). This is a living integration, should you need any added functionality please message us on Discord!
- AgentOps integration example
- Official Cohere documentation
Installation
pip install cohere``python python
import cohere
import agentops
Beginning of program's code (i.e. main.py, __init__.py)
agentops.init()
co = cohere.Client()
chat = co.chat(
message="Is it pronounced ceaux-hear or co-hehray?"
)
print(chat)
agentops.end_session('Success')
import cohere
import agentops
Beginning of program's code (i.e. main.py, __init__.py)
agentops.init()
co = cohere.Client()
stream = co.chat_stream(
message="Write me a haiku about the synergies between Cohere and AgentOps"
)
for event in stream:
if event.event_type == "text-generation":
print(event.text, end='')
agentops.end_session('Success')
Anthropic
Track agents built with the Anthropic Python SDK (>=0.32.0).
- AgentOps integration guide
- Official Anthropic documentation
Installation
pip install anthropic
import anthropic
import agentops
Beginning of program's code (i.e. main.py, __init__.py)
agentops.init()
client = anthropic.Anthropic(
# This is the default and can be omitted
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Tell me a cool fact about AgentOps",
}
],
model="claude-3-opus-20240229",
)
print(message.content)
agentops.end_session('Success')
Streamingimport anthropic
import agentops
Beginning of program's code (i.e. main.py, __init__.py)
agentops.init()
client = anthropic.Anthropic(
# This is the default and can be omitted
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
stream = client.messages.create(
max_tokens=1024,
model="claude-3-opus-20240229",
messages=[
{
"role": "user",
"content": "Tell me something cool about streaming agents",
}
],
stream=True,
)
response = ""
for event in stream:
if event.type == "content_block_delta":
response += event.delta.text
elif event.type == "message_stop":
print("\n")
print(response)
print("\n")
Asyncimport asyncio
from anthropic import AsyncAnthropic
client = AsyncAnthropic(
# This is the default and can be omitted
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
async def main() -> None:
message = await client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Tell me something interesting about async agents",
}
],
model="claude-3-opus-20240229",
)
print(message.content)
await main()
Mistral
Track agents built with the Mistral Python SDK (>=0.32.0).
- AgentOps integration example
- Official Mistral documentation
Installation
pip install mistralai
Syncfrom mistralai import Mistral
import agentops
Beginning of program's code (i.e. main.py, __init__.py)
agentops.init()
client = Mistral(
# This is the default and can be omitted
api_key=os.environ.get("MISTRAL_API_KEY"),
)
message = client.chat.complete(
messages=[
{
"role": "user",
"content": "Tell me a cool fact about AgentOps",
}
],
model="open-mistral-nemo",
)
print(message.choices[0].message.content)
agentops.end_session('Success')
Streamingfrom mistralai import Mistral
import agentops
Beginning of program's code (i.e. main.py, __init__.py)
agentops.init()
client = Mistral(
# This is the default and can be omitted
api_key=os.environ.get("MISTRAL_API_KEY"),
)
message = client.chat.stream(
messages=[
{
"role": "user",
"content": "Tell me something cool about streaming agents",
}
],
model="open-mistral-nemo",
)
response = ""
for event in message:
if event.data.choices[0].finish_reason == "stop":
print("\n")
print(response)
print("\n")
else:
response += event.text
agentops.end_session('Success')
Asyncimport asyncio
from mistralai import Mistral
client = Mistral(
# This is the default and can be omitted
api_key=os.environ.get("MISTRAL_API_KEY"),
)
async def main() -> None:
message = await client.chat.complete_async(
messages=[
{
"role": "user",
"content": "Tell me something interesting about async agents",
}
],
model="open-mistral-nemo",
)
print(message.choices[0].message.content)
await main()
Async Streamingimport asyncio
from mistralai import Mistral
client = Mistral(
# This is the default and can be omitted
api_key=os.environ.get("MISTRAL_API_KEY"),
)
async def main() -> None:
message = await client.chat.stream_async(
messages=[
{
"role": "user",
"content": "Tell me something interesting about async streaming agents",
}
],
model="open-mistral-nemo",
)
response = ""
async for event in message:
if event.data.choices[0].finish_reason == "stop":
print("\n")
print(response)
print("\n")
else:
response += event.text
await main()
CamelAI
Track agents built with the CamelAI Python SDK (>=0.32.0).
- CamelAI integration guide
- Official CamelAI documentation
Installation
pip install camel-ai[all]
pip install agentops
#Import Dependencies
import agentops
import os
from getpass import getpass
from dotenv import load_dotenv
#Set Keys
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY") or ""
agentops_api_key = os.getenv("AGENTOPS_API_KEY") or ""
You can find usage examples here!.LiteLLM
AgentOps provides support for LiteLLM(>=1.3.1), allowing you to call 100+ LLMs using the same Input/Output Format.
- AgentOps integration example
- Official LiteLLM documentation
Installation
pip install litellm
Do not use LiteLLM like this
from litellm import completion
...
response = completion(model="claude-3", messages=messages)
Use LiteLLM like this
import litellm
...
response = litellm.completion(model="claude-3", messages=messages)
or
response = await litellm.acompletion(model="claude-3", messages=messages)
LlamaIndex
AgentOps works seamlessly with applications built using LlamaIndex, a framework for building context-augmented generative AI applications with LLMs.
Installation
pip install llama-index-instrumentation-agentops
To use the handler, import and setfrom llama_index.core import set_global_handler
NOTE: Feel free to set your AgentOps environment variables (e.g., 'AGENTOPS_API_KEY')
as outlined in the AgentOps documentation, or pass the equivalent keyword arguments
anticipated by AgentOps' AOClient as eval_params in set_global_handler.
set_global_handler("agentops")
Check out the LlamaIndex docs for more details.AGENTOPS_API_KEYLlama Stack
AgentOps provides support for Llama Stack Python Client(>=0.0.53), allowing you to monitor your Agentic applications.
- AgentOps integration example 1
- AgentOps integration example 2
- Official Llama Stack Python ClientSwarmZero AI
Track and analyze SwarmZero agents with full observability. Set an
in your environment and initialize AgentOps to get started.- SwarmZero - Advanced multi-agent framework
- AgentOps integration example
- SwarmZero AI integration example
- SwarmZero AI - AgentOps documentation
- Official SwarmZero Python SDKInstallation
pip install swarmzero
pip install agentops
from dotenv import load_dotenv
load_dotenv()
import agentops
agentops.init()
from swarmzero import Agent, Swarm
...
Evaluations Roadmap
Debugging Roadmap
Why AgentOps?
Without the right tools, AI agents are slow, expensive, and unreliable. Our mission is to bring your agent from prototype to production. Here's why AgentOps stands out:
- Comprehensive Observability: Track your AI agents' performance, user interactions, and API usage.
- Real-Time Monitoring: Get instant insights with session replays, metrics, and live monitoring tools.
- Cost Control: Monitor and manage your spend on LLM and API calls.
- Failure Detection: Quickly identify and respond to agent failures and multi-agent interaction issues.
- Tool Usage Statistics: Understand how your agents utilize external tools with detailed analytics.
- Session-Wide Metrics: Gain a holistic view of your agents' sessions with comprehensive statistics.
AgentOps is designed to make agent observability, testing, and monitoring easy.
Star History
Check out our growth in the community:
Popular projects using AgentOps
_Generated using github-dependents-info, by Nicolas Vuillamy_
Contributing Guide
Contributing to AgentOps
Thanks for checking out AgentOps. We're building tools to help developers like you make AI agents that actually work reliably. If you've ever tried to build an agent system, you know the pain - they're a nightmare to debug, impossible to monitor, and when something goes wrong... good luck figuring out why.
We created AgentOps to solve these headaches, and we'd love your help making it even better. Our SDK hooks into all the major Python frameworks (AG2, CrewAI, LangChain) and LLM providers (OpenAI, Anthropic, Cohere, etc.) to give you visibility into what your agents are actually doing.
How You Can Help
There are tons of ways to contribute, and we genuinely appreciate all of them:
1. Add More Providers: Help us support new LLM providers. Each one helps more developers monitor their agents.
2. Improve Framework Support: Using a framework we don't support yet? Help us add it!
3. Make Docs Better: Found our docs confusing? Help us fix them! Clear documentation makes everyone's life easier.
4. Share Your Experience: Using AgentOps? Let us know what's working and what isn't. Your feedback shapes our roadmap.
Even if you're not ready to contribute code, we'd love to hear your thoughts. Drop into our Discord, open an issue, or start a discussion. We're building this for developers like you, so your input matters.
Table of Contents
- Getting Started
- Development Environment
- Testing
- Adding LLM Providers
- Code Style
- Pull Request Process
- DocumentationGetting Started
1. Fork and Clone:
First, fork the repository by clicking the 'Fork' button in the top right of the AgentOps repository. This creates your own copy of the repository where you can make changes.
Then clone your fork:
git clone https://github.com/YOUR_USERNAME/agentops.git
cd agentops
Add the upstream repository to stay in sync:git remote add upstream https://github.com/AgentOps-AI/agentops.git
git fetch upstream
Before starting work on a new feature:git checkout main
git pull upstream main
git checkout -b feature/your-feature-name
2. Install Dependencies:pip install -e .
3. Set Up Pre-commit Hooks:pre-commit install
.envDevelopment Environment
1. Environment Variables:
Create afile:
AGENTOPS_API_KEY=your_api_key
OPENAI_API_KEY=your_openai_key # For testing
ANTHROPIC_API_KEY=your_anthropic_key # For testing
# Other keys...
2. Virtual Environment:poetry
We recommend usingorvenv:
python -m venv venv
source venv/bin/activate # Unix
.\venv\Scripts\activate # Windows
3. Pre-commit Setup:
We use pre-commit hooks to automatically format and lint code. Set them up with:pip install pre-commit
pre-commit install
That's it! The hooks will run automatically when you commit. To manually check all files:pre-commit run --all-files
Testing
We use a comprehensive testing stack to ensure code quality and reliability. Our testing framework includes pytest and several specialized testing tools.
Testing Dependencies
Install all testing dependencies:
pip install -e ".[dev]"
We use the following testing packages:pytest==7.4.0
-: Core testing frameworkpytest-depends
-: Manage test dependenciespytest-asyncio
-: Test async codepytest-vcr
-: Record and replay HTTP interactionspytest-mock
-: Mocking functionalitypyfakefs
-: Mock filesystem operationsrequests_mock==1.11.0
-: Mock HTTP requestsUsing Tox
We use tox to automate and standardize testing. Tox:
- Creates isolated virtual environments for testing
- Tests against multiple Python versions (3.7-3.12)
- Runs all test suites consistently
- Ensures dependencies are correctly specified
- Verifies the package installs correctlyRun tox:
tox
This will:
1. Create fresh virtual environments
2. Install dependencies
3. Run pytest with our test suite
4. Generate coverage reportsRunning Tests
1. Run All Tests:
tox
2. Run Specific Test File:pytest tests/llms/test_anthropic.py -v
3. Run with Coverage:coverage run -m pytest
coverage report
Writing Tests
1. Test Structure:
import pytest
from pytest_mock import MockerFixture
from unittest.mock import Mock, patch
@pytest.mark.asyncio # For async tests
async def test_async_function():
# Test implementation
@pytest.mark.depends(on=['test_prerequisite']) # Declare test dependencies
def test_dependent_function():
# Test implementation
2. Recording HTTP Interactions:@pytest.mark.vcr() # Records HTTP interactions
def test_api_call():
response = client.make_request()
assert response.status_code == 200
3. Mocking Filesystem:def test_file_operations(fs): # fs fixture provided by pyfakefs
fs.create_file('/fake/file.txt', contents='test')
assert os.path.exists('/fake/file.txt')
4. Mocking HTTP Requests:def test_http_client(requests_mock):
requests_mock.get('http://api.example.com', json={'key': 'value'})
response = make_request()
assert response.json()['key'] == 'value'
conftest.pyTesting Best Practices
1. Test Categories:
- Unit tests: Test individual components
- Integration tests: Test component interactions
- End-to-end tests: Test complete workflows
- Performance tests: Test response times and resource usage2. Fixtures:
Create reusable test fixtures in:
@pytest.fixture
def mock_llm_client():
client = Mock()
client.chat.completions.create.return_value = Mock()
return client
3. Test Data:tests/data/
- Store test data intests/cassettes/
- Use meaningful test data names
- Document data format and purpose4. VCR Cassettes:
- Store inexamples/
- Sanitize sensitive information
- Update cassettes when API changesCI Testing Strategy
We use Jupyter notebooks as integration tests for LLM providers. This approach:
- Tests real-world usage patterns
- Verifies end-to-end functionality
- Ensures examples stay up-to-date
- Tests against actual LLM APIs1. Notebook Tests:
- Located indirectorytest-notebooks.yml
- Each LLM provider has example notebooks
- CI runs notebooks on PR merges to main
- Tests run against multiple Python versions2. Test Workflow:
Theworkflow:
name: Test Notebooks
on:
pull_request:
paths:
- "agentops/"
- "examples/"
- "tests/"
- Runs on PR merges and manual triggersexamples/provider_name/
- Sets up environment with provider API keys
- Installs AgentOps from main branch
- Executes each notebook
- Excludes specific notebooks that require manual testing3. Provider Coverage:
Each provider should have notebooks demonstrating:
- Basic completion calls
- Streaming responses
- Async operations (if supported)
- Error handling
- Tool usage (if applicable)4. Adding Provider Tests:
- Create notebook inexclude_notebooks
- Include all provider functionality
- Add necessary secrets to GitHub Actions
- Updatein workflow if manual testing neededagentops/llms/Adding LLM Providers
The
directory contains provider implementations. Each provider must:1. Inherit from BaseProvider:
@singleton
class NewProvider(BaseProvider):
def __init__(self, client):
super().__init__(client)
self._provider_name = "ProviderName"
2. Implement Required Methods:handle_response()
-: Process LLM responsesoverride()
-: Patch the provider's methodsundo_override()
-: Restore original methods3. Handle Events:
Track:
- Prompts and completions
- Token usage
- Timestamps
- Errors
- Tool usage (if applicable)4. Example Implementation Structure:
def handle_response(self, response, kwargs, init_timestamp, session=None):
llm_event = LLMEvent(init_timestamp=init_timestamp, params=kwargs)
try:
# Process response
llm_event.returns = response.model_dump()
llm_event.prompt = kwargs["messages"]
# ... additional processing
self._safe_record(session, llm_event)
except Exception as e:
self._safe_record(session, ErrorEvent(trigger_event=llm_event, exception=e))
feature/descriptionCode Style
1. Formatting:
- Use Black for Python code formatting
- Maximum line length: 88 characters
- Use type hints2. Documentation:
- Docstrings for all public methods
- Clear inline comments
- Update relevant documentation3. Error Handling:
- Use specific exception types
- Log errors with meaningful messages
- Include context in error messagesPull Request Process
1. Branch Naming:
-fix/description
-docs/description
-docs/2. Commit Messages:
- Clear and descriptive
- Reference issues when applicable3. PR Requirements:
- Pass all tests
- Maintain or improve code coverage
- Include relevant documentation
- Update CHANGELOG.md if applicable4. Review Process:
- At least one approval required
- Address all review comments
- Maintain PR scopeDocumentation
1. Types of Documentation:
- API reference
- Integration guides
- Examples
- Troubleshooting guides2. Documentation Location:
- Code documentation in docstrings
- User guides inexamples/
- Examples in3. Documentation Style:
- Clear and concise
- Include code examples
- Explain the why, not just the whatGetting Help & Community
We encourage active community participation and are here to help!
Preferred Communication Channels
1. GitHub Issues & Discussions:
- Open an issue for:
- Bug reports
- Feature requests
- Documentation improvements
- Start a discussion for:
- Questions about usage
- Ideas for new features
- Community showcase
- General feedback2. Discord Community:
- Join our Discord server for:
- Real-time help
- Community discussions
- Feature announcements
- Sharing your projects3. Contact Form:
- For private inquiries, use our contact form
- Please note that public channels are preferred for technical discussionsLicense
By contributing to AgentOps, you agree that your contributions will be licensed under the MIT License.
Core SDK Implementation
agentops/__init__.py
/ Detailed source-code truncated for AI context efficiency. /
agentops/client/client.py
/ Detailed source-code truncated for AI context efficiency. /
agentops/sdk/decorators/__init__.py
/ Detailed source-code truncated for AI context efficiency. /
npx mint-mcp add agentopsDocumentation
v2/introduction.mdx
---
title: "Introduction"
description: "AgentOps is the developer favorite platform for testing, debugging, and deploying AI agents and LLM apps."
---Prefer asking your IDE? Install the Mintlify MCP Docs Server for AgentOps to chat with the docs while you code:
Integrate with developer favorite LLM providers and agent frameworks
Agent Frameworks
} iconType="image" href="/v2/integrations/ag2" />
} iconType="image" href="/v2/integrations/agno" />
} iconType="image" href="/v2/integrations/autogen" />
} iconType="image" href="/v2/integrations/crewai" />
} iconType="image" href="/v2/integrations/google_adk" />
} iconType="image" href="/v2/integrations/langchain" />
} iconType="image" href="/v2/integrations/openai_agents_python" />
} iconType="image" href="/v2/integrations/openai_agents_js" />
} iconType="image" href="/v2/integrations/smolagents" />LLM Providers
} iconType="image" href="/v2/integrations/anthropic" />
} iconType="image" href="/v2/integrations/google_generative_ai" />
} iconType="image" href="/v2/integrations/openai" />
} iconType="image" href="/v2/integrations/litellm" />
} iconType="image" href="/v2/integrations/ibm_watsonx_ai" />
} iconType="image" href="/v2/integrations/xai" />
} iconType="image" href="/v2/integrations/mem0" />Observability and monitoring for your AI agents and LLM apps. And we do it all in just two lines of code...
import agentops
agentops.init()
... that logs everything back to your AgentOps Dashboard.@traceAgentOps is also available for TypeScript/JavaScript applications. Check out our TypeScript SDK guide for Node.js projects.
That's it! AgentOps will automatically instrument your code and start tracking traces.
Need more control? You can create custom traces using the
decorator (recommended) or manage traces manually for advanced use cases:
import agentops
from agentops.sdk.decorators import trace
agentops.init(, auto_start_session=False)
@trace(name="my-workflow", tags=["production"])
def my_workflow():
# Your code here
return "Workflow completed"
You can also set a custom trace name during initialization:import agentops
agentops.init(, trace_name="custom-trace-name")
npx mint-mcp add agentopsThe AgentOps Dashboard
Give us a star to bookmark on GitHub, save for later )
With just two lines of code, you can free yourself from the chains of the terminal and, instead, visualize your agents' behavior
in your AgentOps Dashboard. After setting up AgentOps, each execution of your program is recorded as a session and the above
data is automatically recorded for you.The examples below were captured with two lines of code.
Session Drilldown
Here you will find a list of all of your previously recorded sessions and useful data about each such as total execution time.
You also get helpful debugging info such as any SDK versions you were on if you're building on a supported agent framework like Crew or AutoGen.
LLM calls are presented as a familiar chat history view, and charts give you a breakdown of the types of events that were called and how long they took.Find any past sessions from your Session Drawer.
Most powerful of all is the Session Waterfall. On the left, a time visualization of all your LLM calls, Action events, Tool calls, and Errors.
On the right, specific details about the event you've selected on the waterfall. For instance the exact prompt and completion for a given LLM call.
Most of which has been automatically recorded for you.Session Overview
View a meta-analysis of all of your sessions in a single view.
v2/quickstart.mdx
---
title: "Quickstart"
description: "Get started with AgentOps in minutes with just 2 lines of code for basic monitoring, and explore powerful decorators for custom tracing."
---AgentOps is designed for easy integration into your AI agent projects, providing powerful observability with minimal setup. This guide will get you started quickly.
Give us a star on GitHub! Your support helps us grow.
Prefer asking your IDE? Install the Mintlify MCP Docs Server for AgentOps to chat with the docs while you code:
python-dotenvInstallation
First, install the AgentOps SDK. We recommend includingfor easy API key management.
pip install agentops python-dotenv
poetry add agentops python-dotenv
uv add agentops python-dotenv
import agentopsInitial Setup (2 Lines of Code)
At its simplest, AgentOps can start monitoring your supported LLM and agent framework calls with just two lines of Python code.
1. Import AgentOps: Add
to your script.agentops.init()
2. Initialize AgentOps: Callwith your API key.
import agentops
import os
from dotenv import load_dotenv
Load environment variables (recommended for API keys)
load_dotenv()
Initialize AgentOps
The API key can be passed directly or set as an environment variable AGENTOPS_API_KEY
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
agentops.init(AGENTOPS_API_KEY)
That's it for basic auto-instrumentation!
If you're using a supported library (like OpenAI, LangChain, CrewAI, etc.),
AgentOps will now automatically track LLM calls and agent actions.
Setting Your AgentOps API Key
You need an AgentOps API key to send data to your dashboard.
- Get your API key from the AgentOps Dashboard.It's best practice to set your API key as an environment variable.
export AGENTOPS_API_KEY="your_agentops_api_key_here"
AGENTOPS_API_KEY="your_agentops_api_key_here"
If you use a.envfile, make sureload_dotenv()is called beforeagentops.init().@operationRunning Your Agent & Viewing Traces
After adding the two lines and ensuring your API key is set up:
1. Run your agent application as you normally would.
2. AgentOps will automatically instrument supported libraries and send trace data.
3. Visit your AgentOps Dashboard to observe your agent's operations!Beyond Automatic Instrumentation: Decorators
While AgentOps automatically instruments many popular libraries, you can gain finer-grained control and track custom parts of your code using our powerful decorators. This allows you to define specific operations, group logic under named agents, track tool usage with costs, and create custom traces.
Tracking Custom Operations with
Instrument any function in your code to create spans that track its execution, parameters, and return values. These operations will appear in your session visualization alongside LLM calls.
from agentops.sdk.decorators import operation
@operation
def process_data(data):
# Your function logic here
processed_result = data.upper()
# agentops.record(Events("Processed Data", result=processed_result)) # Optional: record specific events
return processed_result
Example usage:
my_data = "example input"
output = process_data(my_data)
@agentTracking Agent Logic with
@agent
If you structure your system with specific named agents (e.g., classes), use thedecorator on the class and@operationon its methods to group all downstream operations under that agent's context.
from agentops.sdk.decorators import agent, operation
@agent(name="MyCustomAgent") # You can provide a name for the agent
class MyAgent:
def __init__(self, agent_id):
self.agent_id = agent_id # agent_id is a reserved parameter for AgentOps
@operation
def perform_task(self, task_description):
# Agent task logic here
# This could include LLM calls or calls to other @operation decorated functions
return f"Agent {self.agent_id} completed: {task_description}"
Example usage:
research_agent = MyAgent(agent_id="researcher-001")
result = research_agent.perform_task("Analyze market trends")
@toolTracking Tools with
Track the usage of specific tools or functions, and optionally associate costs with them. This data will be aggregated in your dashboard.
from agentops.sdk.decorators import tool
@tool(name="WebSearchTool", cost=0.05) # Cost is optional
def web_search(query: str) -> str:
# Tool logic here
return f"Search results for: {query}"
@tool # No cost specified
def calculator(expression: str) -> str:
try:
return str(eval(expression))
except Exception as e:
return f"Error: {e}"
Example usage:
search_result = web_search("AgentOps features")
calculation = calculator("2 + 2")
@traceGrouping with Traces (
or manual)@trace
Create custom traces to group a sequence of operations or define logical units of work. You can use thedecorator or manage traces manually for more complex scenarios.auto_start_session=False
Ifinagentops.init(), you must use@traceoragentops.start_trace()for any data to be recorded.
from agentops.sdk.decorators import trace
Assuming MyAgent and web_search are defined as above
Option 1: Using the @trace decorator
@trace(name="MyMainWorkflow", tags=["main-flow"])
def my_workflow_decorated(task_to_perform):
# Your workflow code here
main_agent = MyAgent(agent_id="workflow-agent") # Assuming MyAgent is defined
result = main_agent.perform_task(task_to_perform)
# Example of using a tool within the trace
tool_result = web_search(f"details for {task_to_perform}") # Assuming web_search is defined
return result, tool_result
result_decorated = my_workflow_decorated("complex data processing")
Option 2: Managing traces manually
import agentops # Already imported
custom_trace = agentops.start_trace(name="MyManualWorkflow", tags=["manual-flow"])
try:
# Your code here
main_agent = MyAgent(agent_id="manual-workflow-agent") # Assuming MyAgent is defined
result = main_agent.perform_task("another complex task")
tool_result = web_search(f"info for {result}") # Assuming web_search is defined
agentops.end_trace(custom_trace, end_state="Success", end_prompt=f"Completed: {result}")
except Exception as e:
if custom_trace: # Ensure trace was started before trying to end it
agentops.end_trace(custom_trace, end_state="Fail", error_message=str(e))
raise
Updating Trace Metadata
You can also update metadata on running traces to add context or track progress:
from agentops import update_trace_metadata
Update metadata during trace execution
update_trace_metadata({
"operation_name": "AI Agent Processing",
"processing_stage": "data_validation",
"records_processed": 1500,
"user_id": "user_123",
"tags": ["validation", "production"]
})
Complete Example with Decorators
Here's a consolidated example showcasing how these decorators can work together:
/ Detailed source-code truncated for AI context efficiency. /
agentops.init()Next Steps
You've seen how to get started with AgentOps! Explore further to leverage its full potential:
See how AgentOps automatically instruments popular LLM and agent frameworks.
Explore detailed examples for various use cases and integrations.
Dive deeper into the AgentOps SDK capabilities and API.
Learn how to group operations and create custom traces using the @trace decorator.
v2/concepts/core-concepts.mdx
---
title: 'Core Concepts'
description: 'Understanding the fundamental concepts of AgentOps'
---The AgentOps SDK Architecture
AgentOps is designed to provide comprehensive monitoring and analytics for AI agent workflows with minimal implementation effort. The SDK follows these key design principles:
Automated Instrumentation
After calling
, the SDK automatically identifies installed LLM providers and instruments their API calls. This allows AgentOps to capture interactions between your code and the LLM providers to collect data for your dashboard without requiring manual instrumentation for every call.initDeclarative Tracing with Decorators
The decorators system allows you to add tracing to your existing functions and classes with minimal code changes. Decorators create hierarchical spans that provide a structured view of your agent's operations for monitoring and analysis.
OpenTelemetry Foundation
AgentOps is built on OpenTelemetry, a widely-adopted standard for observability instrumentation. This provides a robust and standardized approach to collecting, processing, and exporting telemetry data.
Sessions
A Session represents a single user interaction with your agent. When you initialize AgentOps using the
function, a session is automatically created for you:
import agentops
Initialize AgentOps with automatic session creation
agentops.init(api_key="YOUR_API_KEY")
By default, all events and API calls will be associated with this session. For more advanced use cases, you can control session creation manually:Initialize without auto-starting a session
agentops.init(api_key="YOUR_API_KEY", auto_start_session=False)
Later, manually start a session when needed
agentops.start_session(tags=["customer-query"])
Span Hierarchy
In AgentOps, activities are organized into a hierarchical structure of spans:
- SESSION: The root container for all activities in a single execution of your workflow
- AGENT: Represents an autonomous entity with specialized capabilities
- WORKFLOW: A logical grouping of related operations
- OPERATION/TASK: A specific task or function performed by an agent
- LLM: An interaction with a language model
- TOOL: The use of a tool or API by an agent
This hierarchy creates a complete trace of your agent's execution:
SESSION
AGENT
OPERATION/TASK
LLM
TOOL
WORKFLOW
OPERATION/TASK
LLM (unattributed to a specific agent)
@agentAgents
An Agent represents a component in your application that performs tasks. You can create and track agents using the
decorator:
from agentops.sdk.decorators import agent, operation
@agent(name="customer_service")
class CustomerServiceAgent:
@operation
def answer_query(self, query):
# Agent logic here
pass
LLM Events
AgentOps automatically tracks LLM API calls from supported providers, collecting valuable information like:
- Model: The specific model used (e.g., "gpt-4", "claude-3-opus")
- Provider: The LLM provider (e.g., "OpenAI", "Anthropic")
- Prompt Tokens: Number of tokens in the input
- Completion Tokens: Number of tokens in the output
- Cost: The estimated cost of the interaction
- Messages: The prompt and completion content
import agentops
from openai import OpenAI
Initialize AgentOps
agentops.init(api_key="YOUR_API_KEY")
Initialize the OpenAI client
client = OpenAI()
This LLM call is automatically tracked
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What's the capital of France?"}]
)
Tags
Tags help you organize and filter your sessions. You can add tags when initializing AgentOps or when starting a session:
Add tags when initializing
agentops.init(api_key="YOUR_API_KEY", tags=["production", "web-app"])
Or when manually starting a session
agentops.start_session(tags=["customer-service", "tier-1"])
Host Environment
AgentOps automatically collects basic information about the environment where your agent is running:
- Operating System: The OS type and version
- Python Version: The version of Python being used
- Hostname: The name of the host machine (anonymized)
- SDK Version: The version of the AgentOps SDK being used
Dashboard Views
The AgentOps dashboard provides several ways to visualize and analyze your agent's performance:
- Session List: Overview of all sessions with filtering options
- Timeline View: Chronological display of spans showing duration and relationships
- Tree View: Hierarchical representation of spans showing parent-child relationships
- Message View: Detailed view of LLM interactions with prompt and completion content
- Analytics: Aggregated metrics across sessions and operations
Putting It All Together
A typical implementation looks like this:
import agentops
from openai import OpenAI
from agentops.sdk.decorators import agent, operation
Initialize AgentOps
agentops.init(api_key="YOUR_API_KEY", tags=["production"])
Define an agent
@agent(name="assistant")
class AssistantAgent:
def __init__(self):
self.client = OpenAI()
@operation
def answer_question(self, question):
# This LLM call will be automatically tracked and associated with this agent
response = self.client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": question}]
)
return response.choices[0].message.content
def workflow():
# Use the agent
assistant = AssistantAgent()
answer = assistant.answer_question("What's the capital of France?")
print(answer)
workflow()
Session is automatically tracked until application terminates
v1/quickstart.mdx
---
title: "Quickstart"
description: "Start using AgentOps with just 2 lines of code"
---
import CodeTooltip from '/snippets/add-code-tooltip.mdx'
import EnvTooltip from '/snippets/add-env-tooltip.mdx'
pip install agentops
poetry add agentops
Get an AgentOps API key hereimport agentops
agentops.init()
Execute your program and visit app.agentops.ai/drilldown to observe your Agent!@operationAfter your run, AgentOps prints a clickable URL to console linking directly to your session in the Dashboard
{/ Intentionally blank div for newline /}Give us a star if you liked AgentOps! (you may be our 3,000th )
More basic functionality
You can instrument functions inside your code with the
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.
# Instrument a function as an operation
from agentops.sdk.decorators import operation
@operation
def process_data(data):
# Your function logic here
result = data.upper()
return result
If you use specific named agents within your system, you can create agent spans that contain all downstream operations using the@agentdecorator.
# Create an agent class
from agentops.sdk.decorators import agent, operation
@agent
class 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 to group all your agent operations by using the@sessiondecorator. Sessions serve as the root span for all operations.
# Create a session
from agentops.sdk.decorators import session
@session
def my_workflow():
# Your session code here
agent = MyAgent("research-agent")
result = agent.perform_task("data analysis")
return result
# Run the session
my_workflow()
Example Code
Here is the complete code from the sections above
import agentops
from agentops.sdk.decorators import session, agent, operation
Initialize AgentOps
agentops.init()
Create an agent class
@agent
class 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
@session
def my_workflow():
# Your session code here
agent = MyAgent("research-agent")
result = agent.perform_task("data analysis")
return result
Run the session
my_workflow()
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!
Explore our more advanced functionality!
Record all of your operations the way AgentOps intends.
Associate operations with specific named agents.
Instrumentation Architecture
agentops/instrumentation/__init__.py
/ Detailed source-code truncated for AI context efficiency. /
v0.27.0+agentops/instrumentation/README.md
AgentOps Instrumentation
This package provides OpenTelemetry instrumentation for various LLM providers and related services.
Available Instrumentors
- OpenAI (
andv1.0.0+)v0.7.0+
- Anthropic ()v0.1.0+
- Google GenAI ()v0.1.0+
- IBM WatsonX AI ()v0.56.0+
- CrewAI ()v0.3.2+
- AG2/AutoGen ()v0.1.0+
- Google ADK ()v0.0.1+
- Agno ()v0.1.0+
- Mem0 ()v0.1.0+
- smolagents ()agentops.instrumentation.commonCommon Module Usage
The
module provides shared utilities for creating instrumentations:CommonInstrumentorBase Instrumentor
Use
for creating new instrumentations:
from agentops.instrumentation.common import CommonInstrumentor, InstrumentorConfig, WrapConfig
class MyInstrumentor(CommonInstrumentor):
def __init__(self):
config = InstrumentorConfig(
library_name="my-library",
library_version="1.0.0",
wrapped_methods=[
WrapConfig(
trace_name="my.method",
package="my_library.module",
class_name="MyClass",
method_name="my_method",
handler=my_attribute_handler
)
],
dependencies=["my-library >= 1.0.0"]
)
super().__init__(config)
Attribute Handlers
Create attribute handlers to extract data from method calls:
from agentops.instrumentation.common import AttributeMap
def my_attribute_handler(args=None, kwargs=None, return_value=None) -> AttributeMap:
attributes = {}
if kwargs and "model" in kwargs:
attributes["llm.request.model"] = kwargs["model"]
if return_value and hasattr(return_value, "usage"):
attributes["llm.usage.total_tokens"] = return_value.usage.total_tokens
return attributes
Span Management
Use the span management utilities for consistent span creation:
from agentops.instrumentation.common import create_span, SpanAttributeManager
Create an attribute manager
attr_manager = SpanAttributeManager(service_name="my-service")
Use the create_span context manager
with create_span(
tracer,
"my.operation",
attributes={"my.attribute": "value"},
attribute_manager=attr_manager
) as span:
# Your operation code here
pass
Token Counting
Use the token counting utilities for consistent token usage extraction:
from agentops.instrumentation.common import TokenUsageExtractor, set_token_usage_attributes
Extract token usage from a response
usage = TokenUsageExtractor.extract_from_response(response)
Set token usage attributes on a span
set_token_usage_attributes(span, response)
Streaming Support
Use streaming utilities for handling streaming responses:
from agentops.instrumentation.common import create_stream_wrapper_factory, StreamingResponseHandler
Create a stream wrapper factory
wrapper = create_stream_wrapper_factory(
tracer,
"my.stream",
extract_chunk_content=StreamingResponseHandler.extract_generic_chunk_content,
initial_attributes={"stream.type": "text"}
)
Apply to streaming methods
wrap_function_wrapper("my_module", "stream_method", wrapper)
Metrics
Use standard metrics for consistency across instrumentations:
from agentops.instrumentation.common import StandardMetrics, MetricsRecorder
Create standard metrics
metrics = StandardMetrics.create_standard_metrics(meter)
Use the metrics recorder
recorder = MetricsRecorder(metrics)
recorder.record_token_usage(prompt_tokens=100, completion_tokens=50)
recorder.record_duration(1.5)
agentops/instrumentation/Creating a New Instrumentor
1. Create a new directory under
for your provider__init__.py
2. Create anfile with version informationinstrumentor.py
3. Create anfile extendingCommonInstrumentorattributes/
4. Create attribute handlers in ansubdirectory__init__.py
5. Add your instrumentor to the mainconfigurationExample structure:
agentops/instrumentation/
my_provider/
__init__.py
instrumentor.py
attributes/
__init__.py
handlers.py
agentops.semconvBest Practices
1. Use Common Utilities: Leverage the common module for consistency
2. Follow Semantic Conventions: Use attributes fromexamples/
3. Handle Errors Gracefully: Wrap operations in try-except blocks
4. Support Async: Provide both sync and async method wrapping
5. Document Attributes: Comment on what attributes are captured
6. Test Thoroughly: Write unit tests for your instrumentorExamples
See the
directory for usage examples of each instrumentor.
agentops/instrumentation/providers/openai/instrumentor.py
/ Detailed source-code truncated for AI context efficiency. /
Examples
examples/openai/openai_example_sync.py
/ Detailed source-code truncated for AI context efficiency. /
examples/crewai/job_posting.py
/ Detailed source-code truncated for AI context efficiency. /
examples/langchain/langchain_examples.py
/ Detailed source-code truncated for AI context efficiency. /
ag2/examples/README.md
AgentOps Examples
This directory contains comprehensive examples demonstrating how to integrate AgentOps with various AI/ML frameworks, libraries, and providers. Each example is provided as a Jupyter notebook and a Python script with detailed explanations and code samples.
Directory Structure
- Examples for AG2 (AutoGen 2.0) multi-agent conversationsagentchat_with_memory
-- Agent chat with persistent memoryasync_human_input
-- Asynchronous human input handlingtools_wikipedia_search
-- Wikipedia search tool integrationanthropic/- Anthropic Claude API integration examplesagentops-anthropic-understanding-tools
-- Deep dive into tool usageanthropic-example-async
-- Asynchronous API callsanthropic-example-sync
-- Synchronous API callsantrophic-example-tool
-- Tool calling examplesREADME.md
-- Detailed Anthropic integration guideautogen/- Microsoft AutoGen framework examplesAgentChat
-- Basic agent chat functionalityMathAgent
-- Mathematical problem-solving agentcrewai/- CrewAI multi-agent framework examplesjob_posting
-- Job posting automation workflowmarkdown_validator
-- Markdown validation agentgemini/- Google Gemini API integrationgemini_example
-- Basic Gemini API usage with AgentOpsgoogle_adk/- Google AI Development Kit exampleshuman_approval
-- Human-in-the-loop approval workflowslangchain/- LangChain framework integrationlangchain_examples
-- Comprehensive LangChain usage exampleslitellm/- LiteLLM proxy integrationlitellm_example
-- Multi-provider LLM access through LiteLLMopenai/- OpenAI API integration examplesmulti_tool_orchestration
-- Complex tool orchestrationopenai_example_async
-- Asynchronous OpenAI API callsopenai_example_sync
-- Synchronous OpenAI API callsweb_search
-- Web search functionalityopenai_agents/- OpenAI Agents SDK examplesagent_patterns
-- Common agent design patternsagents_tools
-- Agent tool integrationcustomer_service_agent
-- Customer service automationsmolagents/- SmolAgents framework examplesmulti_smolagents_system
-- Multi-agent system coordinationtext_to_sql
-- Natural language to SQL conversionwatsonx/- IBM Watsonx AI integrationwatsonx-streaming
-- Streaming text generationwatsonx-text-chat
-- Text generation and chat completionwatsonx-tokeniation-model
-- Tokenization and model detailsREADME.md
-- Detailed Watsonx integration guidexai/- xAI (Grok) API integrationgrok_examples
-- Basic Grok API usagegrok_vision_examples
-- Vision capabilities with Grokgenerate_documentation.pyUtility Scripts
- Script to convert Jupyter notebooks to MDX documentation filesexamples/
- Converts notebooks fromtodocs/v2/examples/%pip install
- Handles frontmatter, GitHub links, and installation sections
- Transformscommands to CodeGroup formatgenerate_documentation.pyPrerequisites
1. AgentOps Account: Sign up at agentops.ai
2. Python Environment: Python 3.10+ recommended
3. API Keys: Obtain API keys for the services you want to useDocumentation Generation
The
script automatically converts these Jupyter notebook examples into documentation for the AgentOps website. It:docs/v2/examples/- Extracts notebook content and converts to Markdown
- Adds proper frontmatter and metadata
- Transforms installation commands into user-friendly format
- Generates GitHub links for source notebooks
- Creates MDX files inUsage
python examples/generate_documentation.py examples/langchain/langchain_examples.ipynb
`
Contributing
When adding new examples:
1. Create a new subdirectory for the framework/provider
2. Include comprehensive Jupyter notebooks with explanations
3. Add a README.md if the integration is complex
4. Ensure examples are self-contained and runnable
5. Follow the existing naming conventions
6. Use the
generate_documentation.py script to create documentation files
7. Add the example notebook to the main README.md for visibility
8. Add the generated documentation to the docs/v2/examples/` directory for website visibility9. Submit a pull request with a clear description of your changes
Additional Resources
- AgentOps Documentation
- AgentOps Dashboard
- GitHub Repository
- Community Discord
License
These examples are provided under the same license as the AgentOps project. See the main repository for license details.