agentops

GitHub

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

AI Prompts & Endpoints
CodeWiki Knowledge Base

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

bash
pip install agentops

#### Session replays in 2 lines of code

Initialize the AgentOps client and automatically get analytics on all your LLM calls.

Get an API key

python
import agentops

Beginning 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

python

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

python

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

python

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

python

Create workflow spans for tracking multi-operation workflows


from agentops.sdk.decorators import workflow

@workflow
def my_workflow(data):
# Workflow implementation
return result

python

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

bash
pip install openai-agents

- Python integration guide
- OpenAI Agents Python documentation

#### TypeScript

bash
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.

bash
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

bash
pip install "camel-ai[all]==0.2.11"
pip install agentops

python
import os
import agentops
from camel.agents import ChatAgent
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType

Initialize AgentOps


agentops.init(os.getenv("AGENTOPS_API_KEY"), tags=["CAMEL Example"])

Import toolkits after AgentOps init for tracking


from camel.toolkits import SearchToolkit

Set 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

shell
pip install agentops[langchain]

To use the handler, import and set

python
import os
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from agentops.integration.callbacks.langchain import LangchainCallbackHandler

AGENTOPS_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

bash
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')

text
python python
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')

text

Anthropic

Track agents built with the Anthropic Python SDK (>=0.32.0).

- AgentOps integration guide
- Official Anthropic documentation

Installation

bash
pip install anthropic
text
python python
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')

text
Streaming
python python
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"),
)

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")

text
Async
python python
import 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()

text

Mistral

Track agents built with the Mistral Python SDK (>=0.32.0).

- AgentOps integration example
- Official Mistral documentation

Installation

bash
pip install mistralai
text
Sync
python python
from 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')

text
Streaming
python python
from 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')

text
Async
python python
import 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()

text
Async Streaming
python python
import 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()

text

CamelAI

Track agents built with the CamelAI Python SDK (>=0.32.0).

- CamelAI integration guide
- Official CamelAI documentation

Installation

bash
pip install camel-ai[all]
pip install agentops
text
python python
#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 ""

text
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

bash
pip install litellm
text
python python

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)
text

LlamaIndex

AgentOps works seamlessly with applications built using LlamaIndex, a framework for building context-augmented generative AI applications with LLMs.

Installation

shell
pip install llama-index-instrumentation-agentops
text
To use the handler, import and set
python
from 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")

text
Check out the LlamaIndex docs for more details.

Llama 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 Client

SwarmZero AI

Track and analyze SwarmZero agents with full observability. Set an AGENTOPS_API_KEY 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 SDK

Installation

bash
pip install swarmzero
pip install agentops
text
python
from dotenv import load_dotenv
load_dotenv()

import agentops
agentops.init()

from swarmzero import Agent, Swarm

...


text

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:

_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
- Documentation

Getting 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:

bash
git clone https://github.com/YOUR_USERNAME/agentops.git
cd agentops
text
Add the upstream repository to stay in sync:
bash
git remote add upstream https://github.com/AgentOps-AI/agentops.git
git fetch upstream
text
Before starting work on a new feature:
bash
git checkout main
git pull upstream main
git checkout -b feature/your-feature-name
text
2. Install Dependencies:
bash
pip install -e .
text
3. Set Up Pre-commit Hooks:
bash
pre-commit install
text

Development Environment

1. Environment Variables:
Create a
.env file:


AGENTOPS_API_KEY=your_api_key
OPENAI_API_KEY=your_openai_key # For testing
ANTHROPIC_API_KEY=your_anthropic_key # For testing
# Other keys...
text
2. Virtual Environment:
We recommend using
poetry or venv:
bash
python -m venv venv
source venv/bin/activate # Unix
.\venv\Scripts\activate # Windows
text
3. Pre-commit Setup:
We use pre-commit hooks to automatically format and lint code. Set them up with:
bash
pip install pre-commit
pre-commit install
text
That's it! The hooks will run automatically when you commit. To manually check all files:
bash
pre-commit run --all-files
text

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:

bash
pip install -e ".[dev]"
text
We use the following testing packages:
-
pytest==7.4.0: Core testing framework
-
pytest-depends: Manage test dependencies
-
pytest-asyncio: Test async code
-
pytest-vcr: Record and replay HTTP interactions
-
pytest-mock: Mocking functionality
-
pyfakefs: Mock filesystem operations
-
requests_mock==1.11.0: Mock HTTP requests

Using 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 correctly

Run tox:

bash
tox
text
This will:
1. Create fresh virtual environments
2. Install dependencies
3. Run pytest with our test suite
4. Generate coverage reports

Running Tests

1. Run All Tests:

bash
tox
text
2. Run Specific Test File:
bash
pytest tests/llms/test_anthropic.py -v
text
3. Run with Coverage:
bash
coverage run -m pytest
coverage report
text

Writing Tests

1. Test Structure:

python
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

text
2. Recording HTTP Interactions:
python
@pytest.mark.vcr() # Records HTTP interactions
def test_api_call():
response = client.make_request()
assert response.status_code == 200
text
3. Mocking Filesystem:
python
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')
text
4. Mocking HTTP Requests:
python
def test_http_client(requests_mock):
requests_mock.get('http://api.example.com', json={'key': 'value'})
response = make_request()
assert response.json()['key'] == 'value'
text

Testing 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 usage

2. Fixtures:
Create reusable test fixtures in
conftest.py:

python
@pytest.fixture
def mock_llm_client():
client = Mock()
client.chat.completions.create.return_value = Mock()
return client
text
3. Test Data:
- Store test data in
tests/data/
- Use meaningful test data names
- Document data format and purpose

4. VCR Cassettes:
- Store in
tests/cassettes/
- Sanitize sensitive information
- Update cassettes when API changes

CI 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 APIs

1. Notebook Tests:
- Located in
examples/ directory
- Each LLM provider has example notebooks
- CI runs notebooks on PR merges to main
- Tests run against multiple Python versions

2. Test Workflow:
The
test-notebooks.yml workflow:

yaml
name: Test Notebooks
on:
pull_request:
paths:
- "agentops/"
- "examples/"
- "tests/"
text
- Runs on PR merges and manual triggers
- Sets up environment with provider API keys
- Installs AgentOps from main branch
- Executes each notebook
- Excludes specific notebooks that require manual testing

3. 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 in
examples/provider_name/
- Include all provider functionality
- Add necessary secrets to GitHub Actions
- Update
exclude_notebooks in workflow if manual testing needed

Adding LLM Providers

The agentops/llms/ directory contains provider implementations. Each provider must:

1. Inherit from BaseProvider:

python
@singleton
class NewProvider(BaseProvider):
def __init__(self, client):
super().__init__(client)
self._provider_name = "ProviderName"
text
2. Implement Required Methods:
-
handle_response(): Process LLM responses
-
override(): Patch the provider's methods
-
undo_override(): Restore original methods

3. Handle Events:
Track:
- Prompts and completions
- Token usage
- Timestamps
- Errors
- Tool usage (if applicable)

4. Example Implementation Structure:

python
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))
text

Code Style

1. Formatting:
- Use Black for Python code formatting
- Maximum line length: 88 characters
- Use type hints

2. Documentation:
- Docstrings for all public methods
- Clear inline comments
- Update relevant documentation

3. Error Handling:
- Use specific exception types
- Log errors with meaningful messages
- Include context in error messages

Pull Request Process

1. Branch Naming:
-
feature/description
-
fix/description
-
docs/description

2. Commit Messages:
- Clear and descriptive
- Reference issues when applicable

3. PR Requirements:
- Pass all tests
- Maintain or improve code coverage
- Include relevant documentation
- Update CHANGELOG.md if applicable

4. Review Process:
- At least one approval required
- Address all review comments
- Maintain PR scope

Documentation

1. Types of Documentation:
- API reference
- Integration guides
- Examples
- Troubleshooting guides

2. Documentation Location:
- Code documentation in docstrings
- User guides in
docs/
- Examples in
examples/

3. Documentation Style:
- Clear and concise
- Include code examples
- Explain the why, not just the what

Getting 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 feedback

2. Discord Community:
- Join our Discord server for:
- Real-time help
- Community discussions
- Feature announcements
- Sharing your projects

3. Contact Form:
- For private inquiries, use our contact form
- Please note that public channels are preferred for technical discussions

License

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. /
text

agentops/client/client.py


/ Detailed source-code truncated for AI context efficiency. /
text

agentops/sdk/decorators/__init__.py


/ Detailed source-code truncated for AI context efficiency. /
text

Documentation

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:
npx mint-mcp add agentops

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...

python python
import agentops
agentops.init()
text
... that logs everything back to your AgentOps Dashboard.

AgentOps 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 @trace decorator (recommended) or manage traces manually for advanced use cases:

python python
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"

text
You can also set a custom trace name during initialization:
python python
import agentops
agentops.init(, trace_name="custom-trace-name")
text

The 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:
npx mint-mcp add agentops

Installation


First, install the AgentOps SDK. We recommend including
python-dotenv for easy API key management.
bash pip
pip install agentops python-dotenv
text
bash poetry
poetry add agentops python-dotenv
text
bash uv
uv add agentops python-dotenv
text

Initial 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 import agentops to your script.
2. Initialize AgentOps: Call
agentops.init() with your API key.

python Python
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.


text

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.

bash Export to CLI
export AGENTOPS_API_KEY="your_agentops_api_key_here"
text
txt Set in .env file
AGENTOPS_API_KEY="your_agentops_api_key_here"
text
If you use a .env file, make sure load_dotenv() is called before agentops.init().

Running 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 @operation


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.
python
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)


text

Tracking Agent Logic with @agent


If you structure your system with specific named agents (e.g., classes), use the
@agent decorator on the class and @operation on its methods to group all downstream operations under that agent's context.
python
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")


text

Tracking Tools with @tool


Track the usage of specific tools or functions, and optionally associate costs with them. This data will be aggregated in your dashboard.
python
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")


text

Grouping with Traces (@trace or manual)


Create custom traces to group a sequence of operations or define logical units of work. You can use the
@trace decorator or manage traces manually for more complex scenarios.
If
auto_start_session=False in agentops.init(), you must use @trace or agentops.start_trace() for any data to be recorded.
python
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


text

Updating Trace Metadata

You can also update metadata on running traces to add context or track progress:

python
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"]
})
text

Complete Example with Decorators

Here's a consolidated example showcasing how these decorators can work together:


/ Detailed source-code truncated for AI context efficiency. /
text

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 agentops.init(), 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.

Declarative 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 init function, a session is automatically created for you:

python
import agentops

Initialize AgentOps with automatic session creation


agentops.init(api_key="YOUR_API_KEY")
text
By default, all events and API calls will be associated with this session. For more advanced use cases, you can control session creation manually:
python

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"])
text

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)
text

Agents

An Agent represents a component in your application that performs tasks. You can create and track agents using the @agent decorator:

python
from agentops.sdk.decorators import agent, operation

@agent(name="customer_service")
class CustomerServiceAgent:
@operation
def answer_query(self, query):
# Agent logic here
pass

text

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

python
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?"}]
)
text

Tags

Tags help you organize and filter your sessions. You can add tags when initializing AgentOps or when starting a session:

python

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"])
text

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:

python
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


text

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'

bash pip
pip install agentops
text
bash poetry
poetry add agentops
text
Get an AgentOps API key here
python python
import agentops
agentops.init()
text
Execute your program and visit app.agentops.ai/drilldown to observe your Agent! 

After 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 @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 python
# 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
text
If you use specific named agents within your system, you can create agent spans that contain all downstream operations using the @agent decorator.
python python
# 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}"
text
Create a session to group all your agent operations by using the @session decorator. Sessions serve as the root span for all operations.
python python
# 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()
text

Example Code

Here is the complete code from the sections above

python python
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()
text
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. /
text

agentops/instrumentation/README.md

AgentOps Instrumentation

This package provides OpenTelemetry instrumentation for various LLM providers and related services.

Available Instrumentors

- OpenAI (v0.27.0+ and v1.0.0+)
- Anthropic (
v0.7.0+)
- Google GenAI (
v0.1.0+)
- IBM WatsonX AI (
v0.1.0+)
- CrewAI (
v0.56.0+)
- AG2/AutoGen (
v0.3.2+)
- Google ADK (
v0.1.0+)
- Agno (
v0.0.1+)
- Mem0 (
v0.1.0+)
- smolagents (
v0.1.0+)

Common Module Usage

The agentops.instrumentation.common module provides shared utilities for creating instrumentations:

Base Instrumentor

Use CommonInstrumentor for creating new instrumentations:

python
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)

text

Attribute Handlers

Create attribute handlers to extract data from method calls:

python
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

text

Span Management

Use the span management utilities for consistent span creation:

python
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
text

Token Counting

Use the token counting utilities for consistent token usage extraction:

python
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)
text

Streaming Support

Use streaming utilities for handling streaming responses:

python
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)
text

Metrics

Use standard metrics for consistency across instrumentations:

python
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)
text

Creating a New Instrumentor

1. Create a new directory under agentops/instrumentation/ for your provider
2. Create an
__init__.py file with version information
3. Create an
instrumentor.py file extending CommonInstrumentor
4. Create attribute handlers in an
attributes/ subdirectory
5. Add your instrumentor to the main
__init__.py configuration

Example structure:


agentops/instrumentation/
my_provider/
__init__.py
instrumentor.py
attributes/
__init__.py
handlers.py
text

Best Practices

1. Use Common Utilities: Leverage the common module for consistency
2. Follow Semantic Conventions: Use attributes from
agentops.semconv
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 instrumentor

Examples

See the examples/ directory for usage examples of each instrumentor.


agentops/instrumentation/providers/openai/instrumentor.py


/ Detailed source-code truncated for AI context efficiency. /
text

Examples

examples/openai/openai_example_sync.py


/ Detailed source-code truncated for AI context efficiency. /
text

examples/crewai/job_posting.py


/ Detailed source-code truncated for AI context efficiency. /
text

examples/langchain/langchain_examples.py


/ Detailed source-code truncated for AI context efficiency. /
text

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

- ag2/ - Examples for AG2 (AutoGen 2.0) multi-agent conversations
-
agentchat_with_memory - Agent chat with persistent memory
-
async_human_input - Asynchronous human input handling
-
tools_wikipedia_search - Wikipedia search tool integration

- anthropic/ - Anthropic Claude API integration examples
-
agentops-anthropic-understanding-tools - Deep dive into tool usage
-
anthropic-example-async - Asynchronous API calls
-
anthropic-example-sync - Synchronous API calls
-
antrophic-example-tool - Tool calling examples
-
README.md - Detailed Anthropic integration guide

- autogen/ - Microsoft AutoGen framework examples
-
AgentChat - Basic agent chat functionality
-
MathAgent - Mathematical problem-solving agent

- crewai/ - CrewAI multi-agent framework examples
-
job_posting - Job posting automation workflow
-
markdown_validator - Markdown validation agent

- gemini/ - Google Gemini API integration
-
gemini_example - Basic Gemini API usage with AgentOps

- google_adk/ - Google AI Development Kit examples
-
human_approval - Human-in-the-loop approval workflows

- langchain/ - LangChain framework integration
-
langchain_examples - Comprehensive LangChain usage examples

- litellm/ - LiteLLM proxy integration
-
litellm_example - Multi-provider LLM access through LiteLLM

- openai/ - OpenAI API integration examples
-
multi_tool_orchestration - Complex tool orchestration
-
openai_example_async - Asynchronous OpenAI API calls
-
openai_example_sync - Synchronous OpenAI API calls
-
web_search - Web search functionality

- openai_agents/ - OpenAI Agents SDK examples
-
agent_patterns - Common agent design patterns
-
agents_tools - Agent tool integration
-
customer_service_agent - Customer service automation

- smolagents/ - SmolAgents framework examples
-
multi_smolagents_system - Multi-agent system coordination
-
text_to_sql - Natural language to SQL conversion

- watsonx/ - IBM Watsonx AI integration
-
watsonx-streaming - Streaming text generation
-
watsonx-text-chat - Text generation and chat completion
-
watsonx-tokeniation-model - Tokenization and model details
-
README.md - Detailed Watsonx integration guide

- xai/ - xAI (Grok) API integration
-
grok_examples - Basic Grok API usage
-
grok_vision_examples - Vision capabilities with Grok

Utility Scripts

- generate_documentation.py - Script to convert Jupyter notebooks to MDX documentation files
- Converts notebooks from
examples/ to docs/v2/examples/
- Handles frontmatter, GitHub links, and installation sections
- Transforms
%pip install commands to CodeGroup format

Prerequisites

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 use

Documentation Generation

The generate_documentation.py script automatically converts these Jupyter notebook examples into documentation for the AgentOps website. It:

- 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 in
docs/v2/examples/

Usage

bash
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 visibility
9. 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.