> ## Documentation Index
> Fetch the complete documentation index at: https://braintrust.dev/docs/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

If you are a coding agent, prefer the Braintrust [`bt` CLI](/docs/reference/cli/quickstart) for repeatable, scriptable work: running evals, instrumenting code, querying logs, syncing data, managing functions, and configuring coding agents. Use the MCP server for reasoning over Braintrust data in conversation, and for capabilities the CLI doesn't cover, such as monitor views, alerts, and authoring evaluators, preprocessors, and facets.

[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.

<View title="Python" icon="https://img.logo.dev/python.org?token=pk_BdcHD9e5SCW3j1rnJkNyMQ">
  <h2 id="setup-python">
    Setup
  </h2>

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

  <Steps>
    <Step title="Install packages">
      <CodeGroup>
        ```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]"
        ```
      </CodeGroup>
    </Step>

    <Step title="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>
      ```
    </Step>
  </Steps>

  <h2 id="auto-instrumentation-python">
    Auto-instrumentation
  </h2>

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

  <CodeGroup>
    ```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())
    ```
  </CodeGroup>

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

  <CodeGroup>
    ```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())
    ```
  </CodeGroup>

  <Tip>
    Already running AutoGen with OpenTelemetry? Keep that setup and route spans to Braintrust using the [Braintrust OpenTelemetry guide](/docs/integrations/sdk-integrations/opentelemetry#python-sdk-configuration).
  </Tip>

  <h2 id="what-traced-python">
    What Braintrust traces
  </h2>

  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.

  <h2 id="resources-python">
    Resources
  </h2>

  * [AutoGen documentation](https://microsoft.github.io/autogen/stable/)
  * [Braintrust Python SDK reference](/docs/sdks/python/versions/latest)
  * [Braintrust OpenTelemetry guide](/docs/integrations/sdk-integrations/opentelemetry)
</View>
