Instrument AI Agents

Learn how to instrument your code to use Sentry's AI Agents module with Microsoft.Extensions.AI.

With Sentry AI Agent Monitoring, you can monitor and debug your AI systems with full-stack context. You'll be able to track key insights like token usage, latency, tool usage, and error rates. AI Agent Monitoring data will be fully connected to your other Sentry data like logs, errors, and traces.

As a prerequisite to setting up AI Agent Monitoring with .NET, you'll need to first set up tracing. Once this is done, you can use the Sentry.Extensions.AI package to automatically instrument AI agents created with Microsoft.Extensions.AI.

Install the Sentry.Extensions.AI package:

Copied
dotnet add package Sentry.Extensions.AI

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

The Sentry.Extensions.AI package provides automatic instrumentation for AI agents built with Microsoft.Extensions.AI. This works with any AI provider that implements the IChatClient interface, including:

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

Copied
// Wrap your IChatClient with Sentry instrumentation
var openAiClient = new OpenAI.Chat.ChatClient("gpt-4o-mini", apiKey)
    .AsIChatClient()
    .AddSentry(options =>
    {
        options.Experimental.RecordInputs = true;
        options.Experimental.RecordOutputs = true;
        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);

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

Experimental.RecordInputs

Typebool
Defaulttrue

Whether to include request messages in spans. When enabled, the content of messages sent to the AI model will be recorded in the span data.

Experimental.RecordOutputs

Typebool
Defaulttrue

Whether to include response content in spans. When enabled, the content of responses from the AI model will be recorded in the span data.

Experimental.AgentName

Typestring
DefaultAgent

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

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").