Title: AI client instrumentation reports caller-handled provider errors as unhandled, once per retry · Issue #23023 · getsentry/sentry-javascript · GitHub
Open Graph Title: AI client instrumentation reports caller-handled provider errors as unhandled, once per retry · Issue #23023 · getsentry/sentry-javascript
X Title: AI client instrumentation reports caller-handled provider errors as unhandled, once per retry · Issue #23023 · getsentry/sentry-javascript
Description: Is there an existing issue for this? Searched open and closed issues; the closest is #17212 (mechanism consistency), which is closed and does not cover the AI integrations. How do you use Sentry? Sentry Saas (sentry.io) Which SDK are you...
Open Graph Description: Is there an existing issue for this? Searched open and closed issues; the closest is #17212 (mechanism consistency), which is closed and does not cover the AI integrations. How do you use Sentry? S...
X Description: Is there an existing issue for this? Searched open and closed issues; the closest is #17212 (mechanism consistency), which is closed and does not cover the AI integrations. How do you use Sentry? S...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/23023
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"AI client instrumentation reports caller-handled provider errors as unhandled, once per retry","articleBody":"### Is there an existing issue for this?\n\n* Searched open and closed issues; the closest is getsentry/sentry-javascript#17212 (mechanism consistency), which is closed and does not cover the AI integrations.\n\n### How do you use Sentry?\n\nSentry Saas ([sentry.io](\u003chttp://sentry.io>))\n\n### Which SDK are you using?\n\n`@sentry/node`\n\n### SDK Version\n\n10.69.0\n\n### Framework Version\n\nNode 24, no framework\n\n### Link to Sentry event\n\n*n/a, reproduced locally with a recording transport*\n\n### Reproduction Example/SDK Setup\n\n```js\nimport * as Sentry from '@sentry/node';\n\nSentry.init({ dsn: '...', tracesSampleRate: 1 });\n\nconst client = Sentry.instrumentOpenAiClient(openai);\n\n// Ordinary application-level retry\nasync function withRetry(fn, attempts) {\n let lastError;\n for (let i = 0; i \u003c attempts; i++) {\n try { return await fn(); } catch (error) { lastError = error; }\n }\n throw lastError;\n}\n\n// First attempt 429s, second succeeds.\nawait withRetry(() => client.chat.completions.create(request), 3);\n```\n\n### Steps to Reproduce\n\n1. Instrument an OpenAI, Anthropic, or Google GenAI client with the exported `instrument*Client` function.\n2. Make a call that fails once and succeeds on retry.\n3. Observe the events sent to Sentry.\n\nRunning the above against 10.69.0 with a recording transport:\n\n```\nA. transient failure, retry succeeds\n outcome: recovered\n events sent: 1\n - \"Rate limit exceeded\" handled=false type=auto.ai.openai\n\nB. all attempts fail\n outcome: exhausted\n events sent: 3\n - \"Rate limit exceeded\" handled=false type=auto.ai.openai\n - \"Rate limit exceeded\" handled=false type=auto.ai.openai\n - \"Rate limit exceeded\" handled=false type=auto.ai.openai\n```\n\nAll three exported client instrumentations behave the same way:\n\n```\n openai outcome=recovered events=1 handled=false (auto.ai.openai)\n anthropic outcome=recovered events=1 handled=false (auto.ai.anthropic)\n google-genai outcome=recovered events=1 handled=false (auto.ai.google_genai)\n```\n\n### Expected Result\n\nNo error event for a call the application recovered from, and at most one event for a call that ultimately fails, carrying the caller's own `handled` status.\n\n### Actual Result\n\nThe instrumentation calls `captureException(error, { mechanism: { handled: false } })` and then rethrows, so it decides the error is an unhandled crash before any application retry or fallback logic runs. Because the capture happens inside the wrapped method, every retry produces another event.\n\nConcretely, this means:\n\n* A transient failure that the application successfully recovered from still appears in Sentry as an unhandled crash.\n* A request retried N times produces N duplicate events.\n* When the call really is fatal, the application's own boundary captures it too, so the same failure is recorded twice with conflicting `handled` values. `dedupeIntegration` is not in the Node defaults, so both land.\n\nThe affected call sites all rethrow to the caller:\n\n\u003c!-- linear:table-colwidths:400,400 -->\n| File | Mechanism type |\n| -- | -- |\n| `ai/openai/index.ts` (streaming + non-streaming) | `auto.ai.openai.stream`, `auto.ai.openai` |\n| `ai/anthropic-ai/index.ts` (streaming + non-streaming) | `auto.ai.anthropic` |\n| `ai/google-genai/index.ts` (streaming + non-streaming) | `auto.ai.google_genai` |\n| `ai/core/utils.ts` (`.withResponse()` wrapper) | caller-supplied |\n\nThese are reachable through `instrumentOpenAiClient`, `instrumentAnthropicAiClient` and `instrumentGoogleGenAIClient`, which are exported from `@sentry/node`, `@sentry/bun`, `@sentry/cloudflare`, `@sentry/vercel-edge`, `@sentry/aws-serverless`, `@sentry/google-cloud-serverless` and `@sentry/elysia`. On the edge and serverless runtimes this is the only available instrumentation path.\n\nThe AI integration tests currently mask this: the OpenAI scenario catches two provider errors with the comment `// Error is expected and handled`, while the test runner drops the resulting events via `.ignore('event')`.\n\n### Impact\n\n`handled` feeds release health, so the effect lands on numbers teams act on. An unhandled error marks the session crashed, so a retried 429 costs the same crash-free rate as a real crash. Deploy during a provider blip and release health flags the new version as a regression. Unhandled issues are also auto-prioritized, so recovered failures outrank real bugs in triage, and volume-threshold alerts fire on retry storms that never reached a user.\n\nA provider outage compounds it. One retried request bills 3 to 5 events instead of 0, and orgs near quota start dropping real errors, so an OpenAI incident costs visibility into unrelated production bugs.\n\nAI apps hit this harder than other integrations because the multipliers are standard practice: retry with backoff, fallback chains across models and vendors, agent loops making 10 to 30 calls per user action, and errors that are ordinary control flow rather than faults (`context_length_exceeded` handled by truncating and retrying, or a content-policy refusal rendered as a normal message).\n\nThe workaround today is to filter the auto-instrumented event and capture explicitly at the application boundary, which is what we're doing. The other option teams reach for is dropping the AI integration to stop the false crash reports, which gives up the gen-AI spans and token accounting with it.\n\n### Suggested direction\n\nThis looks like the same call that was already made for the channel-based OpenAI integration in [#21877](\u003chttps://github.com/getsentry/sentry-javascript/issues/21877>), whose merge commit reads:\n\n> Let errors bubble to the global handlers instead of capturing them as unhandled at the instrumentation level, matching the DB/cache channel subscribers.\n\nApplying that convention to the exported client instrumentations would keep the error span status while leaving the `handled` decision to the application boundary that actually knows the outcome.\n\nWorth keeping as-is, for contrast: the places where a provider reports an error as *data* rather than by rejecting. That covers Anthropic's `error` stream events and error-shaped responses, and Google GenAI's blocked-content signal. The caller never sees those as a thrown error, so dropping those captures would lose the signal entirely.\n\nHappy to open a PR for this.","author":{"url":"https://github.com/lux-in-tenebris-lucet","@type":"Person","name":"lux-in-tenebris-lucet"},"datePublished":"2026-08-04T14:31:50Z","url":"https://github.com/getsentry/sentry-javascript/issues/23023"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6e0731e9-65f7-cd32-5589-3317bea3542d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D59A:13F724:3EA01:543AA:6B0442B1 |
| html-safe-nonce | 9eb9df2362303836f33a84630e09f0631055afc792f584e7bcf9fae7db5f382e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTlBOjEzRjcyNDozRUEwMTo1NDNBQTo2QjA0NDJCMSIsInZpc2l0b3JfaWQiOiI0MjAyNzc5NzQ2MDkxOTQyNTc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 03f5f4aaf4c6bd643e7bd34e9a02db75786abbd8c75e6fff1328f4a8d0bbfd72 |
| hovercard-subject-tag | issue:5061649850 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/23023/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7b69c910a4f26ef3c373424584fc3fce1917da441f96681b9e7438ed94b9ef80/getsentry/sentry-javascript/issues/23023 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7b69c910a4f26ef3c373424584fc3fce1917da441f96681b9e7438ed94b9ef80/getsentry/sentry-javascript/issues/23023 |
| og:image:alt | Is there an existing issue for this? Searched open and closed issues; the closest is #17212 (mechanism consistency), which is closed and does not cover the AI integrations. How do you use Sentry? S... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lux-in-tenebris-lucet |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9185ccfefd80c1ff2d9ddcdb0cd2becf46b5a1efbbf30741a895e4bb8de25e64 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 06dc448a76fe4751ac1db26524aefa3aa3e889a8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width