> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AutoGen

> Auto-instrument AutoGen multi-agent conversations and tool calls

Automatically capture agent loops, message histories, and tool executions within the AutoGen framework.

## Install

> [tab] Python
>
> ```bash theme={null}
> pip install traceroot "ag2[gemini]>=0.14.0,<1.0"
> ```

## Set up

This integration instruments [AG2](https://github.com/ag2ai/ag2), the community-maintained continuation that provides the `autogen` package. Microsoft's newer `autogen-agentchat` package uses a different API and is not currently supported by this integration.

To capture the complete picture—including both the agent orchestrations and the underlying token costs—we highly recommend initializing both the AG2 integration and your specific LLM provider (e.g., Google GenAI, OpenAI, Anthropic).

> [tab] Python
>
> ```python theme={null}
> import traceroot
> from traceroot import Integration
>
> # Initialize AutoGen alongside your LLM provider to capture tokens and costs
> traceroot.initialize(integrations=[
>     Integration.AUTOGEN,
>     Integration.GOOGLE_GENAI  # Or OPENAI, ANTHROPIC, etc.
> ])
> ```

## Usage

Once initialized, agent conversations and tool calls are captured automatically:

```python theme={null}
import os
import autogen
from typing import Annotated
from autogen import register_function

llm_config = {
    "config_list": [{
        "model": "gemini-2.5-flash",
        "api_key": os.environ["GEMINI_API_KEY"],
        "api_type": "google"
    }]
}

assistant = autogen.AssistantAgent(
    name="assistant",
    llm_config=llm_config
)
user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=5
)

def get_weather(city: Annotated[str, "City name"]) -> str:
    """Get current weather for a city."""
    return f"The weather in {city} is 72 degrees and sunny."

def get_forecast(city: Annotated[str, "City name"], days: Annotated[int, "Number of days"]) -> str:
    """Get a multi-day weather forecast for a city."""
    return f"{days}-day forecast for {city}: mostly sunny with highs around 70-75°F."

# Register tools — caller decides when to use them, executor runs the function
register_function(
    get_weather,
    caller=assistant,
    executor=user_proxy,
    name="get_weather",
    description="Get current weather for a city",
)
register_function(
    get_forecast,
    caller=assistant,
    executor=user_proxy,
    name="get_forecast",
    description="Get a multi-day weather forecast for a city",
)

# The entire conversation hierarchy is automatically traced
user_proxy.initiate_chat(
    assistant,
    message="What's the weather in Tokyo today, and what does the 3-day forecast look like?",
)
```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `user_proxy.initiate_chat`, named after the agent that opened the chat.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Conversation Loop | The overarching `initiate_chat` session |
| Agent Steps | Individual spans for each `AssistantAgent` or `UserProxyAgent` turn |
| Messages | Full chat history, input messages, and agent replies |
| Tool calls | Function names, input arguments, and execution outputs |
| LLM calls | Raw completion requests to the provider |
| Tokens & Cost | Aggregated usage; cost when the model ID matches traceroot's pricing table |

## Limitations

* AutoGen (AG2) requires initializing both `Integration.AUTOGEN` and your underlying LLM provider integration (such as `Integration.GOOGLE_GENAI` or `Integration.OPENAI`) to capture full token usage and pricing.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [Python](https://github.com/traceroot-ai/traceroot/tree/main/examples/python/autogen-tool-agent)
>
> Run the Python example

## Next steps

> [card] [Python SDK Reference](https://traceroot.ai/docs/tracing/python-sdk.md)
>
> Explore the full Python SDK reference and instrumentation options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.