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

# Microsoft Agent Framework

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

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

**.NET**

## Setup

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

**1. 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
```

**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>
```

## Manual instrumentation

To trace the Agent Framework, add Braintrust's middleware to your chat client and agent builder, then wrap the agent. `UseBraintrustLLMTracing` instruments the LLM calls on the `ChatClientBuilder`, `UseBraintrustFunctionTracing` instruments tool invocations on the agent builder, 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();

// Trace LLM calls on the chat client
var chatClient = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
    .GetChatClient("gpt-5-mini").AsIChatClient()
    .AsBuilder()
    .UseBraintrustLLMTracing(activitySource)
    .Build();

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

// Trace tool calls on the agent and wrap each run
var agent = new ChatClientAgent(
        chatClient,
        instructions: "You are a helpful assistant. Use tools when appropriate.",
        name: "WeatherAgent",
        tools: [getWeather])
    .AsBuilder()
    .UseBraintrustFunctionTracing(activitySource)
    .Build()
    .WithBraintrustAgentTracing(activitySource);

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

Call `UseBraintrustLLMTracing` alone to trace only LLM calls, or `UseBraintrustFunctionTracing` alone to trace only tool invocations. `UseBraintrustLLMTracing` and `WithBraintrustAgentTracing` accept a `captureMessageContent` flag to omit message content from spans. `UseBraintrustFunctionTracing` accepts a `captureToolArguments` flag to omit function arguments and results.

## What Braintrust traces

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.

## Resources

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


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