From c99942289e193ed2ce7c9f79edc536b2aa1b8c74 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Fri, 7 Nov 2025 15:32:26 -0500 Subject: [PATCH 1/7] first draft done --- .../instrumentation/ai-agents-module.mdx | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx diff --git a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx new file mode 100644 index 0000000000000..7a2d86c5e1cc1 --- /dev/null +++ b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx @@ -0,0 +1,154 @@ +--- +title: Instrument AI Agents +sidebar_order: 500 +description: "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`. + +## Installation + +Install the `Sentry.Extensions.AI` package: + +```shell {tabTitle:.NET CLI} +dotnet add package Sentry.Extensions.AI +``` + +```shell {tabTitle:Package Manager} +Install-Package Sentry.Extensions.AI +``` + +The `Sentry.Extensions.AI` integration depends on the `Microsoft.Extensions.AI.Abstractions` package (version 9.7.0 or higher). + +## Automatic Instrumentation + +The `Sentry.Extensions.AI` package provides automatic instrumentation for AI 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/https://www.nuget.org/packages/Microsoft.Extensions.AI.AzureAIInference/) +- [Anthropic.SDK](https://www.nuget.org/packages/Anthropic.SDK) + +### Basic Setup + + +AI Agent monitoring is marked as experimental. + + +To instrument your AI agent, wrap your `IChatClient` with the `AddSentry()` extension method: + +If your AI agent uses tools (function calling), you can instrument them using the `AddSentryToolInstrumentation()` extension method on `ChatOptions`: + + +You must wrap your `IChatClient` before creating a `ChatClientBuilder` with it. If you run `AddSentry()` on an `IChatClient` that already has function invocation, spans will 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.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 = + [ + AIFunctionFactory.Create(async (string location) => + { + // Tool implementation + 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 + +The `AddSentry()` method accepts an optional configuration delegate to customize the instrumentation: + + + +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. + + + + + +Whether to include response content in spans. When enabled, the content of responses from the AI model will be recorded in the span data. + + + + + +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. + + + +## ASP.NET Core Integration + +For ASP.NET Core applications, you can integrate Sentry AI Agent monitoring as follows: + +```csharp +var builder = WebApplication.CreateBuilder(args); + +// Initialize Sentry for ASP.NET Core +builder.WebHost.UseSentry(options => +{ + options.Dsn = "___PUBLIC_DSN___"; + options.TracesSampleRate = 1.0; +}); + +// Set up the AI client with Sentry instrumentation +var openAiClient = new OpenAI.Chat.ChatClient("gpt-4o-mini", apiKey) + .AsIChatClient() + .AddSentry(options => + { + options.Experimental.RecordInputs = true; + options.Experimental.RecordOutputs = true; + }); + +var chatClient = new ChatClientBuilder(openAiClient) + .UseFunctionInvocation() + .Build(); + +// Register as a singleton +builder.Services.AddSingleton(chatClient); + +var app = builder.Build(); + +// Use in endpoints +app.MapGet("/chat", async (IChatClient client, string message) => +{ + var options = new ChatOptions + { + ModelId = "gpt-4o-mini", + Tools = [ /* your tools */ ] + }.AddSentryToolInstrumentation(); + + var response = await client.GetResponseAsync(message, options); + return Results.Ok(response.Message.Text); +}); + +app.Run(); +``` + From b5ceed336fb305f48d9bb2c7e52065c3d3c22048 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Fri, 7 Nov 2025 16:22:15 -0500 Subject: [PATCH 2/7] mention ai agents in agents getting started section --- .../instrumentation/ai-agents-module.mdx | 6 ++- .../insights/ai/agents/getting-started.mdx | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx index 7a2d86c5e1cc1..7641108d9f061 100644 --- a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx +++ b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx @@ -67,9 +67,9 @@ var options = new ChatOptions MaxOutputTokens = 1024, Tools = [ + // Sample Tool AIFunctionFactory.Create(async (string location) => { - // Tool implementation await Task.Delay(500); return $"The weather in {location} is sunny"; }, "GetWeather", "Gets the current weather for a location") @@ -104,6 +104,8 @@ Name of the AI Agent. This name will be used to identify the agent in the Sentry + + ## ASP.NET Core Integration For ASP.NET Core applications, you can integrate Sentry AI Agent monitoring as follows: @@ -152,3 +154,5 @@ app.MapGet("/chat", async (IChatClient client, string message) => app.Run(); ``` + + diff --git a/docs/product/insights/ai/agents/getting-started.mdx b/docs/product/insights/ai/agents/getting-started.mdx index c390ea6ab8834..f429674f24e34 100644 --- a/docs/product/insights/ai/agents/getting-started.mdx +++ b/docs/product/insights/ai/agents/getting-started.mdx @@ -194,6 +194,58 @@ result = await agents.Runner.run( ``` +### .NET - Microsoft.Extensions.AI SDK + +The Sentry .NET SDK supports AI agent monitoring through the Microsoft.Extensions.AI integration, which automatically captures spans for your AI agent workflows using the library's built-in telemetry. + +#### Supported Platforms + +- + +#### Quick Start with Microsoft.Extensions.AI + +```csharp +// 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); +``` + + You can also instrument AI agents manually by following our [manual instrumentation guides](/platforms/python/tracing/instrumentation/custom-instrumentation/ai-agents-module). From 3c2fabae5e842cc335ea3c875e86597908a58546 Mon Sep 17 00:00:00 2001 From: Alex Sohn <44201357+alexsohn1126@users.noreply.github.com> Date: Thu, 13 Nov 2025 11:54:11 -0500 Subject: [PATCH 3/7] Update docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx Co-authored-by: James Crosswell --- .../dotnet/common/tracing/instrumentation/ai-agents-module.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx index 7641108d9f061..db6db42f94795 100644 --- a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx +++ b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx @@ -36,7 +36,7 @@ The `Sentry.Extensions.AI` package provides automatic instrumentation for AI age AI Agent monitoring is marked as experimental. -To instrument your AI agent, wrap your `IChatClient` with the `AddSentry()` extension method: +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`: From 81aa680a9503ff0f01cb894de7e162433e0d4eda Mon Sep 17 00:00:00 2001 From: Alex Sohn <44201357+alexsohn1126@users.noreply.github.com> Date: Thu, 13 Nov 2025 11:54:20 -0500 Subject: [PATCH 4/7] Update docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx Co-authored-by: James Crosswell --- .../dotnet/common/tracing/instrumentation/ai-agents-module.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx index db6db42f94795..a0e18cff0f881 100644 --- a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx +++ b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx @@ -41,7 +41,7 @@ To instrument your AI agent, simply call `IChatClient.AddSentry()` before buildi If your AI agent uses tools (function calling), you can instrument them using the `AddSentryToolInstrumentation()` extension method on `ChatOptions`: -You must wrap your `IChatClient` before creating a `ChatClientBuilder` with it. If you run `AddSentry()` on an `IChatClient` that already has function invocation, spans will not show up correctly. +You must call `IChatClient.AddSentry()` before creating a `ChatClientBuilder` with it. If you run `AddSentry()` on an `IChatClient` that already has function invocation, spans will not show up correctly. ```csharp From d2a654fad813463b5b5ca8e36ae33d483f3aeb8b Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Fri, 14 Nov 2025 10:53:18 -0500 Subject: [PATCH 5/7] Fix wrong formatting and add more info about tool spans --- .../common/tracing/instrumentation/ai-agents-module.mdx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx index a0e18cff0f881..554952d3ba729 100644 --- a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx +++ b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx @@ -36,12 +36,10 @@ The `Sentry.Extensions.AI` package provides automatic instrumentation for AI age AI Agent monitoring is marked as experimental. -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`: +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`: -You must call `IChatClient.AddSentry()` before creating a `ChatClientBuilder` with it. If you run `AddSentry()` on an `IChatClient` that already has function invocation, spans will not show up correctly. +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 From c949fa3069cb6b8df7cfd6fc091a5a3c0e213256 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Fri, 14 Nov 2025 11:05:37 -0500 Subject: [PATCH 6/7] Add detail on why this is experimental --- .../dotnet/common/tracing/instrumentation/ai-agents-module.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx index 554952d3ba729..a337631ea4b0b 100644 --- a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx +++ b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx @@ -33,7 +33,7 @@ The `Sentry.Extensions.AI` package provides automatic instrumentation for AI age ### Basic Setup -AI Agent monitoring is marked as experimental. +AI Agent monitoring is marked as experimental. You may encounter unexpected behaviour from this SDK. 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`: From 1b9ee23f35bc3cc099708e839c42cb3548727ca6 Mon Sep 17 00:00:00 2001 From: Alex Sohn Date: Fri, 14 Nov 2025 11:20:57 -0500 Subject: [PATCH 7/7] fix aspnetcore code --- .../dotnet/common/tracing/instrumentation/ai-agents-module.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx index a337631ea4b0b..3c0dff9709acd 100644 --- a/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx +++ b/docs/platforms/dotnet/common/tracing/instrumentation/ai-agents-module.mdx @@ -132,7 +132,7 @@ var chatClient = new ChatClientBuilder(openAiClient) .Build(); // Register as a singleton -builder.Services.AddSingleton(chatClient); +builder.Services.AddSingleton(chatClient) var app = builder.Build();