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

# Microsoft Agent Framework

> Trace Microsoft Agent Framework agents in Braintrust to debug agent runs, model calls, and tool executions

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.

[Microsoft Agent Framework](https://github.com/microsoft/agent-framework) is a .NET framework for building AI agents. Braintrust traces agent runs end-to-end, covering agent invocations, the underlying LLM calls, and tool executions.

<View title=".NET" icon="https://img.logo.dev/dotnet.microsoft.com?token=pk_BdcHD9e5SCW3j1rnJkNyMQ">
  <h2 id="setup-dotnet">
    Setup
  </h2>

  Install the Braintrust SDK and the Agent Framework integration, then set your API keys. The examples below use OpenAI.

  <Steps>
    <Step title="Install packages">
      ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      dotnet add package Braintrust.Sdk
      dotnet add package Braintrust.Sdk.AgentFramework
      dotnet add package Microsoft.Agents.AI.OpenAI
      ```
    </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="manual-instrumentation-dotnet">
    Manual instrumentation
  </h2>

  To trace the Agent Framework, add Braintrust's middleware to your chat client and wrap your agent. `UseBraintrustTracing` instruments both the LLM calls and the tool invocations on the `ChatClientBuilder`, and `WithBraintrustAgentTracing` wraps the agent so each run is captured as a span.

  ```csharp #skip-compile theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
  using Braintrust.Sdk;
  using Braintrust.Sdk.AgentFramework;
  using Microsoft.Agents.AI;
  using Microsoft.Extensions.AI;
  using OpenAI;

  var braintrust = Braintrust.Sdk.Braintrust.Get();
  var activitySource = braintrust.GetActivitySource();

  // Add Braintrust tracing to the chat client (LLM calls + function calls)
  var chatClient = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
      .GetChatClient("gpt-5-mini").AsIChatClient()
      .AsBuilder()
      .UseBraintrustTracing(activitySource)
      .Build();

  var getWeather = AIFunctionFactory.Create(
      (string city) => $"The weather in {city} is sunny, 72°F.",
      "GetWeather",
      "Gets the current weather for a city.");

  // Wrap the agent so each run is traced
  var agent = new ChatClientAgent(
          chatClient,
          instructions: "You are a helpful assistant. Use tools when appropriate.",
          name: "WeatherAgent",
          tools: [getWeather])
      .WithBraintrustAgentTracing(activitySource);

  var session = await agent.CreateSessionAsync();
  var response = await agent.RunAsync("What's the weather like in Seattle?", session);
  Console.WriteLine(response.Text);
  ```

  `UseBraintrustTracing` combines `UseBraintrustLLMTracing` (LLM calls) and `UseBraintrustFunctionTracing` (tool calls). Call a single method instead to trace only that layer. Each method accepts a `captureMessageContent` flag, and function tracing accepts a `captureToolArguments` flag, to turn off capturing message or argument content.

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

  Braintrust captures a span at each level of the Agent Framework pipeline:

  * Agent run spans (`agent:<name>`), with input and output messages, agent name and ID, token usage (prompt, completion, and total), and time to first token.
  * Chat completion spans (`Chat Completion` and `Chat Completion Stream`), with input and output messages, request and response model, and token usage (prompt, completion, and total). Streamed responses are aggregated and tagged with `stream`.
  * Function call spans (`function:<name>`), with the function name, arguments, result, call iteration and index, and duration.
  * Errors on any span, including the exception message and details.

  Set `captureMessageContent: false` to omit message content, or `captureToolArguments: false` to omit function arguments and results.

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

  * [Microsoft Agent Framework](https://github.com/microsoft/agent-framework)
  * [Braintrust C# SDK reference](/docs/sdks/csharp/api-reference)
  * [Braintrust .NET SDK on GitHub](https://github.com/braintrustdata/braintrust-sdk-dotnet)
</View>
