How to use AgentOps with AutoGen
Quick answer
Use
agentops.init() to automatically instrument your AI agents built with AutoGen. Wrap your agent calls with agentops.start_session() and agentops.end_session() to track sessions and capture detailed telemetry with minimal code changes.PREREQUISITES
Python 3.8+AgentOps API key (set in AGENTOPS_API_KEY environment variable)pip install agentops autogen openai>=1.0OpenAI API key (set in OPENAI_API_KEY environment variable)
Setup
Install the required packages and set environment variables for AgentOps and OpenAI API keys.
- Install packages:
pip install agentops autogen openai - Set environment variables in your shell or .env file:
export AGENTOPS_API_KEY=<your_agentops_key>export OPENAI_API_KEY=<your_openai_key>
pip install agentops autogen openai output
Collecting agentops Collecting autogen Collecting openai Successfully installed agentops autogen openai
Step by step
This example shows how to initialize AgentOps for automatic tracing and integrate it with an AutoGen agent using the OpenAI gpt-4o model. It demonstrates starting and ending a session to capture observability data.
import os
import agentops
from autogen import AssistantAgent, UserProxy
from openai import OpenAI
# Initialize AgentOps for automatic instrumentation
agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])
# Create OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Define a simple AutoGen assistant agent
assistant = AssistantAgent(
name="assistant",
llm=client,
model="gpt-4o"
)
# Define a user proxy to interact with the assistant
user = UserProxy(name="user")
# Start an AgentOps session to track this interaction
session = agentops.start_session(tags=["autogen-example"])
# Run a simple conversation
response = assistant.chat([user.message("Hello, how can you help me today?")])
print("Assistant reply:", response[0].content)
# End the session with success status
agentops.end_session("Success") output
Assistant reply: Hello! I can help you with a variety of tasks such as answering questions, generating text, or assisting with coding. What would you like to do today?
Common variations
You can use AgentOps with asynchronous AutoGen agents by awaiting calls and using async session management. Also, you can customize tags and metadata in start_session() for richer observability.
Example async usage:
import asyncio
import agentops
from autogen import AssistantAgent, UserProxy
from openai import OpenAI
async def main():
agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
assistant = AssistantAgent(name="assistant", llm=client, model="gpt-4o")
user = UserProxy(name="user")
session = agentops.start_session(tags=["autogen-async"])
response = await assistant.chat_async([user.message("Async hello")])
print("Assistant reply:", response[0].content)
agentops.end_session("Success")
asyncio.run(main()) output
Assistant reply: Hello! How can I assist you asynchronously today?
Troubleshooting
- If you see no telemetry in AgentOps dashboard, verify
AGENTOPS_API_KEYis set correctly andagentops.init()is called before agent usage. - Ensure your
AutoGenagent uses the OpenAI client initialized withOPENAI_API_KEY. - If sessions do not end properly, always call
agentops.end_session()to flush data.
Key Takeaways
- Initialize AgentOps early with
agentops.init()to enable automatic instrumentation. - Wrap AutoGen agent interactions with
agentops.start_session()andagentops.end_session()for detailed observability. - Use environment variables for all API keys to keep credentials secure and configurable.
- AgentOps supports both synchronous and asynchronous AutoGen workflows with minimal code changes.