Add ai integration

From getsentry/sentry-javascript · View interactive version

Add ai integration is an AI agent skill from the GitHub repository getsentry/sentry-javascript. Add a new AI provider integration to the Sentry JavaScript SDK. Use when contributing a new AI instrumentation (OpenAI, Anthropic, Vercel AI, LangChain, etc.) or modifying an existing one.

How to use this skill

Skill file

Adding a New AI Integration

Conventions First

Span ops and attributes are specified outside this repo. Never invent or hardcode either:

Derive the op with getGenAiSpanOp() from ai/core/utils.ts rather than picking one by hand. ai/core/gen-ai-attributes.ts is for gap-fillers only — keys with no @sentry/conventions equivalent — so check it last, not first.

Which Pattern

Does the SDK publish its own `diagnostics_channel` telemetry?
|- YES (ai >= 7) -> Pattern 1: Native tracing channel
+- NO -> Does the SDK expose callback/exporter hooks?
    |- YES (LangChain, Mastra) -> Pattern 3: Callback/Exporter
    +- NO (OpenAI, Anthropic, Google GenAI, ai < 7) -> Pattern 2: Orchestrion-injected channels

| Pattern | Use when | Reference |

| -------------------------- | ------------------------------------------ | --------------------------------------------------------------- |

| 1 — Native tracing channel | the SDK publishes to diagnostics_channel | integrations/vercel-ai/vercel-ai-dc-subscriber.ts |

| 2 — Orchestrion channels | the SDK has no telemetry of its own | integrations/openai.ts + orchestrion/config/openai.ts |

| 3 — Callback/exporter | the SDK exposes hooks or an exporter | ai/langchain/, ai/mastra/ (exporter-shaped agent framework) |

What the reference files won't tell you:

Where The Code Goes

Gotchas

  1. Detect streaming from the result shape — an async-iterable or the SDK's stream object — not from params.stream. Only the manual instrument{Provider}Client() API keys off params.stream === true.
  2. Never set streamed response attributes by hand. Accumulate into a StreamResponseState and call endStreamSpan() (ai/openai/streaming.ts for an async generator, ai/anthropic-ai/streaming.ts for a listener-based stream).
  3. Never truncate message payloads. Truncation was removed in v11 (#23045) and nothing downstream caps them; size limiting is server-side.
  4. Never roll child token usage up onto parent spans. Tree totals are computed product-side from the full span tree.
  5. Never read dataCollection.genAI directly. Gate input/output recording on resolveAIRecordingOptions().
  6. LangChain must be registered first in getTracingIntegrations(), so it can disable the provider integrations before they instrument.
  7. Set SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN = 'auto.ai.{provider}' (alphanumerics, _, . only).

Checklist

When in doubt, follow the pattern of the most similar existing integration.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.