---
title: "Instrument Agents"
description: "Learn how to instrument your code to use Sentry's Agents module with Microsoft.Extensions.AI."
url: https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module/
last_updated: 2026-11-24T04:15:50.000Z
---

# Instrument Agents for Serilog

With [Sentry Agent Tracing](https://docs.sentry.io/product/agents.md), you can debug how an agent run flows through your application, and where it fails or slows down. Token usage, latency, and errors show up on that same trace, next to your requests, jobs, and logs.

As a prerequisite to setting up Agent Tracing with .NET, you'll need to first [set up tracing](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing.md). Once this is done, you can use the `Sentry.Extensions.AI` package to automatically instrument agents created with `Microsoft.Extensions.AI`.

## [Installation](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#installation)

Install the `Sentry.Extensions.AI` package:

```shell
dotnet add package Sentry.Extensions.AI
```

*Other available variations of the above snippet: Package Manager*

The `Sentry.Extensions.AI` integration depends on the `Microsoft.Extensions.AI.Abstractions` package (version 9.7.0 or higher).

## [Automatic Instrumentation](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#automatic-instrumentation)

The `Sentry.Extensions.AI` package provides automatic instrumentation for agents built with [Microsoft.Extensions.AI](https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-ai-preview/). This works with any AI provider that implements the `IChatClient` interface, including:

* [Microsoft.Extensions.AI.OpenAI](https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI/)
* [Microsoft.Extensions.AI.AzureAIInference](https://www.nuget.org/packages/Microsoft.Extensions.AI.AzureAIInference/)
* [Anthropic.SDK](https://www.nuget.org/packages/Anthropic.SDK)

### [Basic Setup](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#basic-setup)

##### Important

Agent tracing is marked as experimental. You may encounter unexpected behaviour from this SDK.

To instrument your agent, simply call `IChatClient.AddSentry()` before building your chat client. If your agent uses tools (function calling), you can instrument them using the `AddSentryToolInstrumentation()` extension method on `ChatOptions`:

##### When using tools

You must call `IChatClient.AddSentry()` before creating a `ChatClientBuilder` with it. If you run `AddSentry()` on an `IChatClient` that already has function invocation, spans may not show up correctly.

```csharp
// Wrap your IChatClient with Sentry instrumentation
var openAiClient = new OpenAI.Chat.ChatClient("gpt-4o-mini", apiKey)
    .AsIChatClient()
    .AddSentry(options =>
    {
        options.Experimental.AgentName = "MyAgent";
    });

// Wrap your client with FunctionInvokingChatClient
var chatClient = new ChatClientBuilder(openAiClient)
    .UseFunctionInvocation()
    .Build();

// Create chat options with tools and add Sentry instrumentation
var options = new ChatOptions
{
    ModelId = "gpt-4o-mini",
    MaxOutputTokens = 1024,
    Tools =
    [
        // Sample Tool
        AIFunctionFactory.Create(async (string location) =>
        {
            await Task.Delay(500);
            return $"The weather in {location} is sunny";
        }, "GetWeather", "Gets the current weather for a location")
    ]
}.AddSentryToolInstrumentation();

var response = await chatClient.GetResponseAsync(
    "What's the weather in New York?",
    options);
```

## [Configuration Options](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#configuration-options)

The `AddSentry()` method accepts an optional configuration delegate to customize the instrumentation:

### [Experimental.RecordInputs](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#Experimental.RecordInputs)

| Type    | `bool`                                |
| ------- | ------------------------------------- |
| Default | `true` (unless GenAI.Inputs is false) |

Whether to include AI inputs in spans: request messages, tool definitions, and tool call arguments. The model's response repeats the tool call arguments, so to keep them out of Sentry, turn off `RecordOutputs` as well.

Starting with version 7.1.0, setting it overrides `GenAI.Inputs` from [`ConfigureDataCollection`](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#stop-sending-prompts-and-responses) for this chat client. If you don't set it, `GenAI.Inputs` decides, and that defaults to `true`.

### [Experimental.RecordOutputs](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#Experimental.RecordOutputs)

| Type    | `bool`                                 |
| ------- | -------------------------------------- |
| Default | `true` (unless GenAI.Outputs is false) |

Whether to include AI outputs in spans: response text, response tool calls, and tool call results. The next request repeats the tool call results, so to keep them out of Sentry, turn off `RecordInputs` as well.

Starting with version 7.1.0, setting it overrides `GenAI.Outputs` from [`ConfigureDataCollection`](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#stop-sending-prompts-and-responses) for this chat client. If you don't set it, `GenAI.Outputs` decides, and that defaults to `true`.

### [Experimental.AgentName](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#Experimental.AgentName)

| Type    | `string` |
| ------- | -------- |
| Default | `Agent`  |

Name of the agent. This name will be used to identify the agent in the Sentry UI and helps differentiate between multiple agents in your application.

## [Stop Sending Prompts and Responses](https://docs.sentry.io/platforms/dotnet/guides/serilog/tracing/instrumentation/ai-agents-module.md#stop-sending-prompts-and-responses)

Starting with version 7.1.0, you can stop sending AI content from every chat client by turning off generative AI inputs and outputs when you initialize the SDK. Sentry still sends metadata, such as model names and token counts. For details, see [AI Agents Privacy](https://docs.sentry.io/product/agents/privacy.md#sdk-data-collection).

```csharp
SentrySdk.Init(options =>
{
    options.Dsn = "https://<key>@o<orgId>.ingest.sentry.io/<projectId>";
    options.TracesSampleRate = 1.0;
    options.ConfigureDataCollection(dc =>
    {
        dc.GenAI.Inputs = false;
        dc.GenAI.Outputs = false;
    });
});
```

`ConfigureDataCollection` is experimental and may change in a future release.

Once you call `ConfigureDataCollection`, its options replace `SendDefaultPii` for the data they cover, and any option you don't set defaults to collecting data. If you haven't set `SendDefaultPii` to `true`, the SDK then starts sending data it didn't send before, such as IP addresses and the headers of failed HTTP requests. To stop sending AI content without that change, set `RecordInputs` and `RecordOutputs` to `false` in each chat client's `AddSentry()` call instead.

To keep sending content from a specific chat client, set `RecordInputs` or `RecordOutputs` to `true` in its `AddSentry()` call.

---

Find other pages: [Sitemap](https://docs.sentry.io/sitemap.md) lists every page with a one-line summary, and [llms.txt](https://docs.sentry.io/llms.txt) gives an overview with guidance for agents.
