sentry-setup-ai-monitoring
Original:🇺🇸 English
Not Translated
Setup Sentry AI Agent Monitoring in any project. Use when asked to monitor LLM calls, track AI agents, or instrument OpenAI/Anthropic/Vercel AI/LangChain/Google GenAI. Detects installed AI SDKs and configures appropriate integrations.
4installs
Sourcegetsentry/agent-skills
Added on
NPX Install
npx skill4agent add getsentry/agent-skills sentry-setup-ai-monitoringSKILL.md Content
Setup Sentry AI Agent Monitoring
Configure Sentry to track LLM calls, agent executions, tool usage, and token consumption.
Invoke This Skill When
- User asks to "monitor AI/LLM calls" or "track OpenAI/Anthropic usage"
- User wants "AI observability" or "agent monitoring"
- User asks about token usage, model latency, or AI costs
Prerequisites
AI monitoring requires tracing enabled ().
tracesSampleRate > 0Detection First
Always detect installed AI SDKs before configuring:
bash
# JavaScript
grep -E '"(openai|@anthropic-ai/sdk|ai|@langchain|@google/genai)"' package.json
# Python
grep -E '(openai|anthropic|langchain|huggingface)' requirements.txt pyproject.toml 2>/dev/nullSupported SDKs
JavaScript
| Package | Integration | Min Sentry SDK | Auto? |
|---|---|---|---|
| | 10.2.0 | Yes |
| | 10.12.0 | Yes |
| | 10.6.0 | Node only* |
| | 10.22.0 | Yes |
| | 10.25.0 | Yes |
| | 10.14.0 | Yes |
*Vercel AI requires explicit setup for Edge runtime and per-call.
experimental_telemetryPython
| Package | Install | Min SDK |
|---|---|---|
| | 2.41.0 |
| | 2.x |
| | 2.x |
| | 2.x |
JavaScript Configuration
Auto-enabled integrations (OpenAI, Anthropic, Google GenAI, LangChain)
Just ensure tracing is enabled. To capture prompts/outputs:
javascript
Sentry.init({
dsn: "YOUR_DSN",
tracesSampleRate: 1.0,
integrations: [
Sentry.openAIIntegration({ recordInputs: true, recordOutputs: true }),
],
});Next.js OpenAI (additional step required)
For Next.js projects using OpenAI, you must wrap the client:
javascript
import OpenAI from "openai";
import * as Sentry from "@sentry/nextjs";
const openai = Sentry.instrumentOpenAiClient(new OpenAI());
// Use 'openai' client as normalLangChain / LangGraph (explicit)
javascript
integrations: [
Sentry.langChainIntegration({ recordInputs: true, recordOutputs: true }),
Sentry.langGraphIntegration({ recordInputs: true, recordOutputs: true }),
],Vercel AI SDK
Add to for Edge runtime:
sentry.edge.config.tsjavascript
integrations: [Sentry.vercelAIIntegration()],Enable telemetry per-call:
javascript
await generateText({
model: openai("gpt-4o"),
prompt: "Hello",
experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true },
});Python Configuration
python
import sentry_sdk
from sentry_sdk.integrations.openai import OpenAIIntegration # or anthropic, langchain
sentry_sdk.init(
dsn="YOUR_DSN",
traces_sample_rate=1.0,
send_default_pii=True, # Required for prompt capture
integrations=[OpenAIIntegration(include_prompts=True)],
)Manual Instrumentation
Use when no supported SDK is detected.
Span Types
| Purpose |
|---|---|
| Individual LLM calls |
| Agent execution lifecycle |
| Tool/function calls |
| Agent-to-agent transitions |
Example (JavaScript)
javascript
await Sentry.startSpan({
op: "gen_ai.request",
name: "LLM request gpt-4o",
attributes: { "gen_ai.request.model": "gpt-4o" },
}, async (span) => {
span.setAttribute("gen_ai.request.messages", JSON.stringify(messages));
const result = await llmClient.complete(prompt);
span.setAttribute("gen_ai.usage.input_tokens", result.inputTokens);
span.setAttribute("gen_ai.usage.output_tokens", result.outputTokens);
return result;
});Key Attributes
| Attribute | Description |
|---|---|
| Model identifier |
| JSON input messages |
| Input token count |
| Output token count |
| Agent identifier |
| Tool identifier |
PII Considerations
Prompts/outputs are PII. To capture:
- JS: per-integration
recordInputs: true, recordOutputs: true - Python: +
include_prompts=Truesend_default_pii=True
Troubleshooting
| Issue | Solution |
|---|---|
| AI spans not appearing | Verify |
| Token counts missing | Some providers don't return tokens for streaming |
| Prompts not captured | Enable |
| Vercel AI not working | Add |