Sentry Setup Ai Monitoring

作者 getsentry9f54eb021916Apache-2.025 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫6 個月前更新

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/Pydantic AI. Detects installed AI SDKs and configures appropriate integrations.

AI 產生的概覽

設定 Sentry AI Agent Monitoring,用來追蹤專案中的 LLM 呼叫、代理程式執行、工具使用與 token 消耗。

功能
此技能會引導代理程式在程式碼庫中設定 Sentry AI Agent Monitoring。它會偵測已安裝的 AI SDK(OpenAI、Anthropic、Vercel AI、LangChain、Google GenAI、Pydantic AI 等),並提供 JavaScript 與 Python 的 Sentry 初始化與整合設定,在找不到支援的 SDK 時給出手動 span 埋點方式。內容也涵蓋提示與輸出擷取的隱私提醒、驗證步驟與疑難排解。
適用情境
當使用者想監控或追蹤 LLM 呼叫、AI 代理程式或工具使用,或詢問 token 消耗、模型延遲、AI 成本時使用。適合已使用 Sentry 或正在導入 Sentry 追蹤的專案。
執行需求
僅為說明文件,不含指令碼。需要一個 Sentry 專案與 DSN,啟用追蹤(tracesSampleRate 大於 0),並能存取專案的相依性清單以偵測已安裝的 AI SDK。隱含需要連線至 Sentry 及其文件的網路存取。

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

Important: The SDK versions, API names, and code samples below are examples. Always verify against docs.sentry.io before implementing, as APIs and minimum versions may have changed.

Prerequisites

AI monitoring requires tracing enabled (tracesSampleRate > 0).

Data Capture Warning

Prompt and output recording captures user content that is likely PII. Before enabling recordInputs/recordOutputs (JS) or include_prompts/send_default_pii (Python), confirm:

  • The application's privacy policy permits capturing user prompts and model responses
  • Captured data complies with applicable regulations (GDPR, CCPA, etc.)
  • Sentry data retention settings are appropriate for the sensitivity of the data

Ask the user whether they want prompt/output capture enabled. Do not enable it by default — configure it only when explicitly requested or confirmed. Use tracesSampleRate: 1.0 only in development; in production, use a lower value or a tracesSampler function.

Detection First

Always detect installed AI SDKs before configuring:

bash
# JavaScriptgrep -E '"(openai|@anthropic-ai/sdk|ai|@langchain|@google/genai)"' package.json
# Pythongrep -E '(openai|anthropic|langchain|huggingface)' requirements.txt pyproject.toml 2>/dev/null

Supported SDKs

JavaScript

PackageIntegrationMin Sentry SDKAuto?
openaiopenAIIntegration()10.28.0Yes
@anthropic-ai/sdkanthropicAIIntegration()10.28.0Yes
ai (Vercel)vercelAIIntegration()10.6.0Yes*
@langchain/*langChainIntegration()10.28.0Yes
@langchain/langgraphlangGraphIntegration()10.28.0Yes
@google/genaigoogleGenAIIntegration()10.28.0Yes

*Vercel AI: 10.6.0+ for Node.js, Cloudflare Workers, Vercel Edge Functions, Bun. 10.12.0+ for Deno. Requires experimental_telemetry per-call.

Python

Integrations auto-enable when the AI package is installed — no explicit registration needed:

PackageAuto?Notes
openaiYesIncludes OpenAI Agents SDK
anthropicYes
langchain / langgraphYes
huggingface_hubYes
google-genaiYes
pydantic-aiYes
litellmNoRequires explicit integration
mcp (Model Context Protocol)Yes

JavaScript Configuration

Node.js — auto-enabled integrations

Just ensure tracing is enabled. Integrations auto-enable when the AI package is installed:

javascript
Sentry.init({  dsn: "YOUR_DSN",  tracesSampleRate: 1.0, // Lower in production (e.g., 0.1)  // OpenAI, Anthropic, Google GenAI, LangChain integrations auto-enable in Node.js});

To customize (e.g., enable prompt capture — see Data Capture Warning):

javascript
integrations: [  Sentry.openAIIntegration({    // recordInputs: true,  // Opt-in: captures prompt content (PII)    // recordOutputs: true, // Opt-in: captures response content (PII)  }),],

Browser / Next.js OpenAI (manual wrapping required)

In browser-side code or Next.js meta-framework apps, auto-instrumentation is not available. Wrap the client manually:

javascript
import OpenAI from "openai";import * as Sentry from "@sentry/nextjs"; // or @sentry/react, @sentry/browser
const openai = Sentry.instrumentOpenAiClient(new OpenAI());// Use 'openai' client as normal

LangChain / LangGraph (auto-enabled)

javascript
integrations: [  Sentry.langChainIntegration({    // recordInputs: true,  // Opt-in: captures prompt content (PII)    // recordOutputs: true, // Opt-in: captures response content (PII)  }),  Sentry.langGraphIntegration({    // recordInputs: true,    // recordOutputs: true,  }),],

Vercel AI SDK

Add to sentry.edge.config.ts for Edge runtime:

javascript
integrations: [Sentry.vercelAIIntegration()],

Enable telemetry per-call:

javascript
await generateText({  model: openai("gpt-4o"),  prompt: "Hello",  experimental_telemetry: {    isEnabled: true,    // recordInputs: true,  // Opt-in: captures prompt content (PII)    // recordOutputs: true, // Opt-in: captures response content (PII)  },});

Python Configuration

Integrations auto-enable — just init with tracing. Only add explicit imports to customize options:

python
import sentry_sdk
sentry_sdk.init(    dsn="YOUR_DSN",    traces_sample_rate=1.0,  # Lower in production (e.g., 0.1)    # send_default_pii=True,  # Opt-in: required for prompt capture (sends user PII)    # Integrations auto-enable when the AI package is installed.    # Only specify explicitly to customize (e.g., include_prompts):    # integrations=[OpenAIIntegration(include_prompts=True)],)

Manual Instrumentation

Use when no supported SDK is detected.

Span Types

op ValuePurpose
gen_ai.requestIndividual LLM calls
gen_ai.invoke_agentAgent execution lifecycle
gen_ai.execute_toolTool/function calls
gen_ai.handoffAgent-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

AttributeDescription
gen_ai.request.modelModel identifier
gen_ai.request.messagesJSON input messages
gen_ai.usage.input_tokensInput token count
gen_ai.usage.output_tokensOutput token count
gen_ai.agent.nameAgent identifier
gen_ai.tool.nameTool identifier

Enable prompt/output capture only after confirming with the user (see Data Capture Warning above).

Verification

After configuring, make an LLM call and check the Sentry Traces dashboard. AI spans appear with gen_ai.* operations showing model, token counts, and latency.

Troubleshooting

IssueSolution
AI spans not appearingVerify tracesSampleRate > 0, check SDK version
Token counts missingSome providers don't return tokens for streaming
Prompts not capturedEnable recordInputs/include_prompts
Vercel AI not workingAdd experimental_telemetry to each call

來源與署名

來源:getsentry/sentry-agent-skills位於skills/sentry-setup-ai-monitoring提交9f54eb0

授權條款: Apache-2.0

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架