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

# Install and instrument

> Install the .NET SDK, set an API key, and configure tracing by hand.

> [!WARNING]
>
> The .NET SDK is in [public preview](https://docs.braintrust.dev/feature-lifecycle.md) and can change before reaching general availability.

> [!NOTE]
>
> To use an agent for automatic setup, see the [Quickstart](https://docs.braintrust.dev/sdks/csharp/quickstart.md).

## Sign up

If you don't have a Braintrust account, sign up for free at [braintrust.dev](https://www.braintrust.dev/signup).

## Install the SDK

The Braintrust .NET SDK requires .NET 8.0 or later. Add the core package to your project:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
dotnet add package Braintrust.Sdk
```

Each AI provider has its own integration package, which you add alongside the core package in the steps below.

## Set an API key

Create an API key in [API key settings](https://www.braintrust.dev/app/~/settings/api-keys) and set it as an environment variable:

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
BRAINTRUST_API_KEY="your-api-key"
```

The SDK reads `BRAINTRUST_API_KEY` from the environment, or from a `.env.braintrust` file discovered upward from your working directory. Keep your API key out of version control.

## Set a project

Traces are logged to a project that you name. Set `BRAINTRUST_DEFAULT_PROJECT_NAME` alongside your API key, or pass the project name to `BraintrustConfig.Of` and hand the config to `Braintrust.Get`. Use the config form to keep the project name with the rest of your application configuration.

```bash title="Environment variable" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
BRAINTRUST_DEFAULT_PROJECT_NAME="My project"
```

```csharp title="BraintrustConfig" #skip-compile theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
var config = BraintrustConfig.Of(
    ("BRAINTRUST_DEFAULT_PROJECT_NAME", "My project")
);
var braintrust = Braintrust.Sdk.Braintrust.Get(config);
```

The examples below use `Braintrust.Get()`, which reads the environment variable.

## Configure tracing

Wrap your AI provider's client with Braintrust to trace its calls. Select your provider:

> [accordion] OpenAI
>
> **1. Add the OpenAI integration package**
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> dotnet add package Braintrust.Sdk.OpenAI
> dotnet add package OpenAI
> ```
>
> **2. Wrap the OpenAI client**
>
> Initialize Braintrust, get an `ActivitySource`, and wrap the client with `BraintrustOpenAI.WrapOpenAI`. Every call on the wrapped client is traced.
>
> ```csharp theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> using System;
> using System.Threading.Tasks;
> using Braintrust.Sdk;
> using Braintrust.Sdk.OpenAI;
> using OpenAI;
> using OpenAI.Chat;
>
> class OpenAITracing
> {
>     static async Task Main(string[] args)
>     {
>         var braintrust = Braintrust.Sdk.Braintrust.Get();
>         var activitySource = braintrust.GetActivitySource();
>
>         var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
>         if (string.IsNullOrEmpty(apiKey))
>         {
>             Console.WriteLine("Error: OPENAI_API_KEY environment variable is not set.");
>             return;
>         }
>
>         // Wrap the OpenAI client with Braintrust instrumentation
>         var client = BraintrustOpenAI.WrapOpenAI(
>             activitySource,
>             apiKey
>         );
>
>         // All API calls are automatically logged
>         var chatClient = client.GetChatClient("gpt-5-mini");
>         var messages = new ChatMessage[]
>         {
>             new SystemChatMessage("You are a helpful assistant."),
>             new UserChatMessage("What is machine learning?")
>         };
>
>         var result = await chatClient.CompleteChatAsync(messages);
>     }
> }
> ```

> [accordion] Anthropic
>
> **1. Add the Anthropic integration package**
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> dotnet add package Braintrust.Sdk.Anthropic
> dotnet add package Anthropic
> ```
>
> **2. Wrap the Anthropic client**
>
> Call `.WithBraintrust()` on the Anthropic client. Once wrapped, every `Messages.Create` call (including streaming) emits a span.
>
> ```csharp theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> using System;
> using System.Collections.Generic;
> using System.Threading.Tasks;
> using Anthropic;
> using Anthropic.Models.Messages;
> using Braintrust.Sdk.Anthropic;
>
> class AnthropicTracing
> {
>     static async Task Main(string[] args)
>     {
>         // Wrap the Anthropic client with Braintrust instrumentation
>         var client = new AnthropicClient().WithBraintrust();
>
>         // All API calls are automatically logged
>         var result = await client.Messages.Create(new MessageCreateParams
>         {
>             Model = "claude-sonnet-4-5-20250929",
>             MaxTokens = 1024,
>             Messages = new List<MessageParam>
>             {
>                 new MessageParam { Role = "user", Content = "What is machine learning?" }
>             }
>         });
>
>         if (result.Content[0].TryPickText(out var textBlock))
>         {
>             Console.WriteLine(textBlock.Text);
>         }
>     }
> }
> ```

> [accordion] Azure OpenAI
>
> **1. Add the Azure OpenAI integration package**
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> dotnet add package Braintrust.Sdk.AzureOpenAI
> ```
>
> **2. Wrap the Azure OpenAI client**
>
> Create an instrumented client with `WrapAzureOpenAI`, then pass your Azure deployment name to `GetChatClient`.
>
> ```csharp #skip-compile theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> using Azure.AI.OpenAI;
> using Braintrust.Sdk;
> using Braintrust.Sdk.AzureOpenAI;
> using OpenAI.Chat;
>
> var braintrust = Braintrust.Sdk.Braintrust.Get();
> var activitySource = braintrust.GetActivitySource();
>
> var endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"));
> var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
>
> // Create an instrumented Azure OpenAI client
> var client = BraintrustAzureOpenAI.WrapAzureOpenAI(activitySource, endpoint, apiKey);
>
> // Use your Azure deployment name, not the underlying model name
> var chatClient = client.GetChatClient("gpt-5-mini");
> var response = await chatClient.CompleteChatAsync(
>     new ChatMessage[]
>     {
>         new UserChatMessage("What is the capital of France?")
>     });
> ```
>
> For Microsoft Entra ID authentication, pass a `TokenCredential` to `WrapAzureOpenAI` in place of the API key. See the [Azure AI Foundry integration](https://docs.braintrust.dev/integrations/ai-providers/azure.md) for details.

> [accordion] Microsoft Agent Framework
>
> **1. Add the Agent Framework integration package**
>
> ```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
> dotnet add package Braintrust.Sdk.AgentFramework
> dotnet add package Microsoft.Agents.AI.OpenAI
> ```
>
> **2. Add Braintrust tracing to your agent**
>
> `UseBraintrustLLMTracing` instruments LLM calls on the `ChatClientBuilder`, `UseBraintrustFunctionTracing` instruments tool calls 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);
> ```

To learn more about what each integration captures, find your provider in [SDK integrations](https://docs.braintrust.dev/sdks/csharp/sdk-integrations.md).

## Verify tracing

Run your app and make an AI call. A trace will show up in your [Braintrust Logs](https://www.braintrust.dev/app/~/logs), usually within seconds.

> [!TIP]
>
> If traces appear in Braintrust, you've successfully set up the SDK.

If your traces don't appear in Braintrust, see [Troubleshooting](https://docs.braintrust.dev/sdks/csharp/troubleshooting.md).

## Next steps

Learn more about using the SDK to observe, evaluate, and improve your AI application:

* [Instrument](https://docs.braintrust.dev/instrument.md) — trace LLM calls and application logic
* [Observe](https://docs.braintrust.dev/observe.md) — search and analyze production traces
* [Annotate](https://docs.braintrust.dev/annotate.md) — label traces and build datasets
* [Evaluate](https://docs.braintrust.dev/evaluate.md) — measure quality and catch regressions
* [Deploy](https://docs.braintrust.dev/deploy.md) — ship to production with the AI gateway


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