Title: Vercel AI: reasoning tokens never reach `invoke_agent` parents, and streamed parents report candidate-only output · Issue #23993 · getsentry/sentry-javascript · GitHub
Open Graph Title: Vercel AI: reasoning tokens never reach `invoke_agent` parents, and streamed parents report candidate-only output · Issue #23993 · getsentry/sentry-javascript
X Title: Vercel AI: reasoning tokens never reach `invoke_agent` parents, and streamed parents report candidate-only output · Issue #23993 · getsentry/sentry-javascript
Description: Follow-up to #23433, which fixes Gemini reasoning tokens on model-call (gen_ai.generate_content) spans. Two gaps are deliberately out of scope there and need a decision. Background Per the conventions, gen_ai.usage.output_tokens is reaso...
Open Graph Description: Follow-up to #23433, which fixes Gemini reasoning tokens on model-call (gen_ai.generate_content) spans. Two gaps are deliberately out of scope there and need a decision. Background Per the conventi...
X Description: Follow-up to #23433, which fixes Gemini reasoning tokens on model-call (gen_ai.generate_content) spans. Two gaps are deliberately out of scope there and need a decision. Background Per the conventi...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/23993
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Vercel AI: reasoning tokens never reach `invoke_agent` parents, and streamed parents report candidate-only output","articleBody":"Follow-up to #23433, which fixes Gemini reasoning tokens on model-call (`gen_ai.generate_content`) spans. Two gaps are deliberately out of scope there and need a decision.\n\n### Background\n\nPer the [conventions](https://getsentry.github.io/sentry-conventions/attributes/gen_ai/), `gen_ai.usage.output_tokens` is reasoning-inclusive and `gen_ai.usage.reasoning.output_tokens` is a subset of it. On `ai` v4/v5 with `@ai-sdk/google`, the SDK's `outputTokens` is candidate-only — reasoning (\"thoughts\") is exposed only through `providerMetadata.google.usageMetadata`. #23433 recomputes output on model-call spans from `candidatesTokenCount + thoughtsTokenCount`.\n\nIt cannot do the same on `gen_ai.invoke_agent` parents: their `ai.usage.*` is `totalUsage` summed across every step, while `ai.response.providerMetadata` describes the **last step only**. So #23433 gates `output_tokens`, `total_tokens` and `reasoning.output_tokens` off parents entirely.\n\n### Gap 1 — streamed parents report candidate-only output\n\nWith `spanStreamingIntegration` enabled, each span is emitted independently, so there is no pass that can re-derive a parent from its children. Measured on v10 with #23433 applied:\n\n| parent | static (event processor) | streamed |\n|---|---|---|\n| single-step | `output 101, total 115` ✅ | `output 1, total 15` ⚠️ |\n| multi-step | `output 201, total 1101` ✅ | `output 21, total 921` ⚠️ |\n\nThe static path is correct because `applyAccumulatedTokens` overwrites the parent by summing its (already corrected) children. The streamed path has no equivalent.\n\n### Gap 2 — `reasoning.output_tokens` never reaches parents\n\n`accumulateTokensForParent` / `applyAccumulatedTokens` sum input and output but never reasoning, so unlike output/total there is no fallback once it is gated. A parent therefore carries no reasoning count on either path. Writing the ungated value would be wrong rather than merely absent: on a two-step call it reports the last step's reasoning as the whole call's (e.g. `100` where the true aggregate is `80 + 100 = 180`).\n\n### Options to investigate\n\n1. **Sum reasoning in `accumulateTokensForParent` / `applyAccumulatedTokens`.** Gives parents a true aggregate reasoning count and closes Gap 2 on the static path. Does not help the streamed path, which has no accumulation. Touches shared token plumbing that every provider flows through, so it needs its own testing.\n\n2. **Read the SDK's own reasoning tokens instead of deriving them from `providerMetadata`.** `ai` v6+ exposes `ai.usage.outputTokenDetails.reasoningTokens` (and the deprecated `ai.usage.reasoningTokens`) directly on the span, sourced from `usage.outputTokens.reasoning`, and v6 `outputTokens` is already `total` = text + reasoning. Nothing in the SDK reads either today. This is provider-agnostic, works on both paths, and would cover Gemini, OpenAI and Anthropic at once — but only for SDK versions that emit it.\n\n### Also worth folding in\n\n- On `v10`, the OpenAI branch of `getProviderMetadataAttributes` writes `gen_ai.usage.output_tokens.reasoning`, which `@sentry/conventions` marks deprecated in favour of `gen_ai.usage.reasoning.output_tokens`. It has the same last-step-only issue on parents.\n- On `develop`, the Vercel AI OTel path was removed in #23384. `enrichSpanOnEnd` in `vercel-ai-dc-subscriber.ts` reads input/output/total but never `outputTokenDetails.reasoningTokens`, so `gen_ai.usage.reasoning.output_tokens` is currently only ever populated for OpenAI. Option 2 likely applies there more cleanly than on `v10`.\n","author":{"url":"https://github.com/RulaKhaled","@type":"Person","name":"RulaKhaled"},"datePublished":"2026-09-03T10:07:55Z","url":"https://github.com/getsentry/sentry-javascript/issues/23993"}
| 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:7485587f-ba3d-5f43-2f35-95a4bfd97d7e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8260:34B9D4:64AAA3:8EDF65:6B046E8B |
| html-safe-nonce | dbba2eefa23ae622c19e1747343f3f38e9cc42a525becb7d20dc1aa4c574acb3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MjYwOjM0QjlENDo2NEFBQTM6OEVERjY1OjZCMDQ2RThCIiwidmlzaXRvcl9pZCI6IjU2NDY5NDc5MDA3NzgxMTQ2OTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a01da663eecc7f44f9ce027a9f648ea5ce2d27b5b77169195081ab84a650f7e9 |
| hovercard-subject-tag | issue:5335205534 |
| 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/23993/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ada6eab004686608a30f10ccaa9b6c6786a629e194fb47239bd7ae9d79b4c934/getsentry/sentry-javascript/issues/23993 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ada6eab004686608a30f10ccaa9b6c6786a629e194fb47239bd7ae9d79b4c934/getsentry/sentry-javascript/issues/23993 |
| og:image:alt | Follow-up to #23433, which fixes Gemini reasoning tokens on model-call (gen_ai.generate_content) spans. Two gaps are deliberately out of scope there and need a decision. Background Per the conventi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RulaKhaled |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| 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 | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| 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