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

# AutoGen

> Trace AutoGen multi-agent runs in Braintrust to debug agents, teams, and tool calls

[AutoGen](https://microsoft.github.io/autogen/stable/) is a Microsoft framework for building multi-agent conversational systems. Braintrust traces individual agents, multi-agent teams, and their tool calls. The integration works with any model provider AutoGen supports. LLM call tracing inside the agent run comes from the provider's own Braintrust integration.

**Python**

## Setup

Install the Braintrust SDK and AutoGen with the OpenAI extension (used in the examples below), then set your Braintrust and OpenAI API keys.

**1. Install packages**

```bash uv theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
uv add braintrust autogen-agentchat "autogen-ext[openai]"
```

```bash pip theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
pip install braintrust autogen-agentchat "autogen-ext[openai]"
```

**2. Set environment variables**

```bash title=".env" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
BRAINTRUST_API_KEY=<your-braintrust-api-key>
OPENAI_API_KEY=<your-openai-api-key>
```

## Auto-instrumentation

To trace AutoGen alongside Braintrust's other supported libraries, call `braintrust.auto_instrument()` before creating AutoGen agents or teams.

```python Python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import asyncio
import os

import braintrust
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient

braintrust.auto_instrument()
braintrust.init_logger(
    api_key=os.environ["BRAINTRUST_API_KEY"],
    project="autogen-example",  # Replace with your project name
)

async def main():
    model_client = OpenAIChatCompletionClient(model="gpt-4o-mini")
    agent = AssistantAgent(
        "assistant",
        model_client=model_client,
        system_message="Answer in one sentence.",
    )

    await Console(agent.run_stream(task="What is the capital of France?"))
    await model_client.close()

if __name__ == "__main__":
    asyncio.run(main())
```

To trace AutoGen without auto-instrumenting other libraries, use `setup_autogen()` instead of `braintrust.auto_instrument()`.

```python Python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import asyncio

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from braintrust.integrations.autogen import setup_autogen

setup_autogen(project_name="autogen-example")

async def main():
    model_client = OpenAIChatCompletionClient(model="gpt-4o-mini")
    agent = AssistantAgent(
        "assistant",
        model_client=model_client,
        system_message="Answer in one sentence.",
    )

    await Console(agent.run_stream(task="What is the capital of France?"))
    await model_client.close()

if __name__ == "__main__":
    asyncio.run(main())
```

> [!TIP]
>
> Already running AutoGen with OpenTelemetry? Keep that setup and route spans to Braintrust using the [Braintrust OpenTelemetry guide](https://docs.braintrust.dev/integrations/sdk-integrations/opentelemetry/send-traces-and-logs.md#python-sdk-configuration).

## What Braintrust traces

Braintrust patches AutoGen's chat agent, team, and `FunctionTool` entry points (`autogen-agentchat>=0.7.0`). Captured spans:

* Agent run spans (`<agent>.run` and `<agent>.run_stream`), with task input, agent metadata (name, description, class), output, and errors. Emitted for both direct agent calls and agents running inside a team (such as `RoundRobinGroupChat`).
* Team run spans (`<team>.run` and `<team>.run_stream`), with task input, team metadata (name, description, class, participant names), output, and errors.
* Tool call spans (`<tool>.run`) for `FunctionTool` invocations, with arguments, tool metadata (name, description, class), output, and errors.
* Streaming events aggregated into the parent span's output, not emitted as per-chunk child spans.
* Parent-child nesting: agent spans nest under any enclosing team span, and LLM and tool call spans nest under the agent that issued them. With `auto_instrument()`, provider LLM calls inherit this nesting automatically.

## Resources

* [AutoGen documentation](https://microsoft.github.io/autogen/stable/)
* [Braintrust Python SDK reference](https://docs.braintrust.dev/sdks/python/versions/latest.md)
* [Braintrust OpenTelemetry guide](https://docs.braintrust.dev/integrations/sdk-integrations/opentelemetry/send-traces-and-logs.md)


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