--- title: AG2 description: "Track and analyze your AG2 agents with AgentOps" --- [AG2](https://ag2.ai/) (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 ```bash pip pip install agentops pyautogen ``` ```bash poetry poetry add agentops pyautogen ``` ```bash uv uv pip install agentops pyautogen ``` ## Setting Up API Keys Before using AG2 with AgentOps, you need to set up your API keys. You can obtain: - **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys) - **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/) Then to set them up, you can either export them as environment variables or set them in a `.env` file. ```bash Export to CLI export OPENAI_API_KEY="your_openai_api_key_here" export AGENTOPS_API_KEY="your_agentops_api_key_here" ``` ```txt Set in .env file OPENAI_API_KEY="your_openai_api_key_here" AGENTOPS_API_KEY="your_agentops_api_key_here" ``` Then load the environment variables in your Python code: ```python 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: ```python Single Agent Conversation 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 with Automated Responses Demonstrates asynchronous human input with AG2 agents. Example of AG2 agents using a Wikipedia search tool. Orchestrate a team of specialized agents (researcher, coder, critic) with full AgentOps tracing. ## Resources Official AG2 documentation on integrating with AgentOps. Full observability for multi-agent systems with AG2's built-in tracing.