Skip to main content
AG2 (formerly AutoGen) is a framework for building multi-agent conversational AI systems. AgentOps provides seamless, automatic instrumentation for AG2 — just call agentops.init() and all agent interactions are tracked.

Installation

pip install agentops pyautogen

Setting Up API Keys

Before using AG2 with AgentOps, you need to set up your API keys. You can obtain: Then to set them up, you can either export them as environment variables or set them in a .env file.
export OPENAI_API_KEY="your_openai_api_key_here"
export AGENTOPS_API_KEY="your_agentops_api_key_here"
Then load the environment variables in your Python code:
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Set up environment variables with fallback values
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")

Usage

Initialize AgentOps at the beginning of your application to automatically track all AG2 agent interactions:
import agentops
import autogen
import os

# Initialize AgentOps
agentops.init()

# Configure your AG2 agents
config_list = [
    {
        "model": "gpt-4",
        "api_key": os.getenv("OPENAI_API_KEY"),
    }
]

llm_config = {
    "config_list": config_list,
    "timeout": 60,
}

# Create a single agent
assistant = autogen.AssistantAgent(
    name="assistant",
    llm_config=llm_config,
    system_message="You are a helpful AI assistant."
)

user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    human_input_mode="TERMINATE",
    max_consecutive_auto_reply=10,
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
    code_execution_config={"last_n_messages": 3, "work_dir": "coding"},
)

# Initiate a conversation
user_proxy.initiate_chat(
    assistant,
    message="How can I implement a basic web scraper in Python?"
)

Examples

AG2 Async Agent Chat

AG2 Async Agent Chat with Automated Responses

Async Human Input

Demonstrates asynchronous human input with AG2 agents.

Wikipedia Search Tool

Example of AG2 agents using a Wikipedia search tool.

Multi-Agent Group Chat

Orchestrate a team of specialized agents (researcher, coder, critic) with full AgentOps tracing.

Resources

AG2 - AgentOps Ecosystem Docs

Official AG2 documentation on integrating with AgentOps.

AG2 OpenTelemetry Tracing

Full observability for multi-agent systems with AG2’s built-in tracing.