Skip to content

Commit 2313c3f

Browse files
feat(AI): Add AI Agent Monitoring documentation for .NET Microsoft.Extensions.AI (#15431)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR We are making a new AI Agent Monitoring SDK for .NET. - getsentry/sentry-dotnet#4657 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [ ] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## LEGAL BOILERPLATE <!-- Sentry employees and contractors can delete or ignore this section. --> Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: James Crosswell <jamescrosswell@users.noreply.github.com>
1 parent 1555290 commit 2313c3f

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
title: Instrument AI Agents
3+
sidebar_order: 500
4+
description: "Learn how to instrument your code to use Sentry's AI Agents module with Microsoft.Extensions.AI."
5+
---
6+
7+
With <Link to="/product/insights/ai/agents/dashboard/">Sentry AI Agent Monitoring</Link>, 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.
8+
9+
As a prerequisite to setting up AI Agent Monitoring with .NET, you'll need to first <PlatformLink to="/tracing/">set up tracing</PlatformLink>. Once this is done, you can use the `Sentry.Extensions.AI` package to automatically instrument AI agents created with `Microsoft.Extensions.AI`.
10+
11+
## Installation
12+
13+
Install the `Sentry.Extensions.AI` package:
14+
15+
```shell {tabTitle:.NET CLI}
16+
dotnet add package Sentry.Extensions.AI
17+
```
18+
19+
```shell {tabTitle:Package Manager}
20+
Install-Package Sentry.Extensions.AI
21+
```
22+
23+
The `Sentry.Extensions.AI` integration depends on the `Microsoft.Extensions.AI.Abstractions` package (version 9.7.0 or higher).
24+
25+
## Automatic Instrumentation
26+
27+
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:
28+
29+
- [Microsoft.Extensions.AI.OpenAI](https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI/)
30+
- [Microsoft.Extensions.AI.AzureAIInference](https://www.nuget.org/packages/Microsoft.Extensions.AI.AzureAIInference/)
31+
- [Anthropic.SDK](https://www.nuget.org/packages/Anthropic.SDK)
32+
33+
### Basic Setup
34+
35+
<Alert level="warning" title="Important">
36+
AI Agent monitoring is marked as experimental. You may encounter unexpected behaviour from this SDK.
37+
</Alert>
38+
39+
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`:
40+
41+
<Alert level="warning" title="When using tools">
42+
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.
43+
</Alert>
44+
45+
```csharp
46+
// Wrap your IChatClient with Sentry instrumentation
47+
var openAiClient = new OpenAI.Chat.ChatClient("gpt-4o-mini", apiKey)
48+
.AsIChatClient()
49+
.AddSentry(options =>
50+
{
51+
options.Experimental.RecordInputs = true;
52+
options.Experimental.RecordOutputs = true;
53+
options.Experimental.AgentName = "MyAgent";
54+
});
55+
56+
// Wrap your client with FunctionInvokingChatClient
57+
var chatClient = new ChatClientBuilder(openAiClient)
58+
.UseFunctionInvocation()
59+
.Build();
60+
61+
// Create chat options with tools and add Sentry instrumentation
62+
var options = new ChatOptions
63+
{
64+
ModelId = "gpt-4o-mini",
65+
MaxOutputTokens = 1024,
66+
Tools =
67+
[
68+
// Sample Tool
69+
AIFunctionFactory.Create(async (string location) =>
70+
{
71+
await Task.Delay(500);
72+
return $"The weather in {location} is sunny";
73+
}, "GetWeather", "Gets the current weather for a location")
74+
]
75+
}.AddSentryToolInstrumentation();
76+
77+
var response = await chatClient.GetResponseAsync(
78+
"What's the weather in New York?",
79+
options);
80+
```
81+
82+
83+
## Configuration Options
84+
85+
The `AddSentry()` method accepts an optional configuration delegate to customize the instrumentation:
86+
87+
<SdkOption name="Experimental.RecordInputs" type="bool" defaultValue="true">
88+
89+
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.
90+
91+
</SdkOption>
92+
93+
<SdkOption name="Experimental.RecordOutputs" type="bool" defaultValue="true">
94+
95+
Whether to include response content in spans. When enabled, the content of responses from the AI model will be recorded in the span data.
96+
97+
</SdkOption>
98+
99+
<SdkOption name="Experimental.AgentName" type="string" defaultValue="Agent">
100+
101+
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.
102+
103+
</SdkOption>
104+
105+
<PlatformSection supported={["dotnet.aspnetcore"]}>
106+
107+
## ASP.NET Core Integration
108+
109+
For ASP.NET Core applications, you can integrate Sentry AI Agent monitoring as follows:
110+
111+
```csharp
112+
var builder = WebApplication.CreateBuilder(args);
113+
114+
// Initialize Sentry for ASP.NET Core
115+
builder.WebHost.UseSentry(options =>
116+
{
117+
options.Dsn = "___PUBLIC_DSN___";
118+
options.TracesSampleRate = 1.0;
119+
});
120+
121+
// Set up the AI client with Sentry instrumentation
122+
var openAiClient = new OpenAI.Chat.ChatClient("gpt-4o-mini", apiKey)
123+
.AsIChatClient()
124+
.AddSentry(options =>
125+
{
126+
options.Experimental.RecordInputs = true;
127+
options.Experimental.RecordOutputs = true;
128+
});
129+
130+
var chatClient = new ChatClientBuilder(openAiClient)
131+
.UseFunctionInvocation()
132+
.Build();
133+
134+
// Register as a singleton
135+
builder.Services.AddSingleton<IChatClient>(chatClient)
136+
137+
var app = builder.Build();
138+
139+
// Use in endpoints
140+
app.MapGet("/chat", async (IChatClient client, string message) =>
141+
{
142+
var options = new ChatOptions
143+
{
144+
ModelId = "gpt-4o-mini",
145+
Tools = [ /* your tools */ ]
146+
}.AddSentryToolInstrumentation();
147+
148+
var response = await client.GetResponseAsync(message, options);
149+
return Results.Ok(response.Message.Text);
150+
});
151+
152+
app.Run();
153+
```
154+
155+
</PlatformSection>
156+

docs/product/insights/ai/agents/getting-started.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To start sending AI agent data to Sentry, make sure you've created a Sentry proj
1010

1111
- [Python](/platforms/python/tracing/instrumentation/custom-instrumentation/ai-agents-module)
1212
- [Node.js](/platforms/javascript/guides/node/tracing/instrumentation/ai-agents-module/)
13-
13+
- [.NET](/platforms/dotnet/tracing/instrumentation/ai-agents-module/)
1414

1515
<Alert title="Don't see your runtime?">
1616

0 commit comments

Comments
 (0)