
Sentry Setup Ai Monitoring
Configure Sentry traces sampling so gen_ai agent span trees are not dropped when the root HTTP transaction is undersampled.
Install
npx skills add https://github.com/getsentry/sentry-for-ai --skill sentry-setup-ai-monitoringWhat is this skill?
- Explains why undersampled root spans erase entire agent execution trees
- Scenario 1: gen_ai span as root (cron, queue, CLI)—sample gen_ai at 1.0
- Scenario 2: gen_ai children under HTTP—force 1.0 on /api/chat and /api/agent routes
- JavaScript @sentry/node >=9.x with inheritOrSampleWith and Python sentry-sdk >=2.x traces_sampler examples
- Baseline inheritance pattern (e.g. inheritOrSampleWith(0.2)) for non-AI traffic
Adoption & trust: 1.5k installs on skills.sh; 197 GitHub stars; 3/3 security scanners passed (skills.sh audits).
Recommended Skills
Microsoft Foundrymicrosoft/azure-skills
Azure Aimicrosoft/azure-skills
Azure Hosted Copilot Sdkmicrosoft/azure-skills
Lark Eventlarksuite/cli
Running Claude Code Via Litellm Copilotxixu-me/skills
Setup Matt Pocock Skillsmattpocock/skills
Journey fit
Primary fit
AI observability matters most in production, so Operate/monitoring is the canonical shelf even though SDK changes happen during Build. monitoring fits span sampling, DSN init, and tracesSampler/traces_sampler policies for agent runs—not feature coding.
Common Questions / FAQ
Is Sentry Setup Ai Monitoring safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Sentry Setup Ai Monitoring
# Sampling Strategy for AI Agent Spans > `@sentry/node` >=9.x (`inheritOrSampleWith`), `sentry-sdk` >=2.x (`traces_sampler`) ## The Problem Agent runs are span trees. Sampling decides at the root; children inherit. Drop the root, lose every child span. At any rate below 1.0, you lose entire agent executions. ## How It Works `tracesSampler` / `traces_sampler` only fires on **root spans**. Non-root spans (including `gen_ai.*` children) inherit unconditionally. **Scenario 1: gen_ai span IS the root** (cron, queue consumer, CLI). The sampler sees `gen_ai.*` directly. Match and return 1.0. **Scenario 2: gen_ai spans are children of HTTP transactions** (most web apps). `POST /api/chat` is sampled before any AI code runs. Solution: sample AI routes at 1.0. ## JavaScript ```javascript Sentry.init({ dsn: process.env.SENTRY_DSN, tracesSampler: ({ name, attributes, inheritOrSampleWith }) => { // Standalone gen_ai root spans if (attributes?.['sentry.op']?.startsWith('gen_ai.') || attributes?.['gen_ai.system']) { return 1.0; } // HTTP routes that trigger AI calls if (name?.includes('/api/chat') || name?.includes('/api/agent')) { return 1.0; } return inheritOrSampleWith(0.2); // adjust to your baseline }, }); ``` ## Python ```python def traces_sampler(sampling_context): tx = sampling_context.get("transaction_context", {}) op, name = tx.get("op", ""), tx.get("name", "") if op.startswith("gen_ai."): return 1.0 if op == "http.server" and any(p in name for p in ["/api/chat", "/api/agent"]): return 1.0 parent = sampling_context.get("parent_sampled") if parent is not None: return float(parent) return 0.2 sentry_sdk.init(dsn="...", traces_sampler=traces_sampler) ``` If AI is the core product, skip `tracesSampler` and use `tracesSampleRate: 1.0`. ## Fallback: Metrics + Logs If 100% tracing isn't feasible, emit metrics and logs on every LLM call (independent of trace sampling): ```python # Metrics - 100% coverage of cost/usage/latency sentry_sdk.metrics.distribution("gen_ai.token_usage", usage.total_tokens, attributes={"model": model, "user_id": str(user.id)}) sentry_sdk.metrics.count("gen_ai.calls", 1, attributes={"model": model, "status": "error" if error else "success"}) # Logs - 100% searchable per-call records sentry_sdk.logger.info("LLM call", model=model, input_tokens=usage.prompt_tokens, output_tokens=usage.completion_tokens, latency_ms=response_time_ms) ``` JS equivalent uses `Sentry.metrics.*` and `Sentry.logger.*` with the same attribute patterns. ## Troubleshooting | Issue | Solution | |-------|----------| | gen_ai spans missing despite sampler returning 1.0 | Parent HTTP transaction was sampled at a lower rate. Add the route to your sampler. | | `tracesSampler` not called for gen_ai spans | Expected. It only runs on root spans. Sample the parent HTTP route instead. | | All traces at 100% | Check the fallback rate in `inheritOrSampleWith()` / default return value. | --- name: sentry-setup-ai-monitoring description: Setup Sentry AI Agent Monitoring in any project. Use when asked to monitor LLM calls, track AI agents, track conversations, or instrument OpenAI/Anthropic/Vercel AI/LangChain/Google GenAI/Pydantic AI. Detects installed AI SDKs and configures appropriate integrations. license: Apache-2.0 category: feature-setup parent: sentry-feature-setup disable-model-invocation: true --- > [All Skills](../../SKILL_TREE.md) > [Feature Setup](../sentry-feature-setup/SKILL.md) > AI Monitoring # 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