Skip to content

fix(server-utils): Sum Vercel AI reasoning tokens onto invoke_agent spans - #26904

Merged
RulaKhaled merged 9 commits into
developfrom
fix/vercel-ai-parent-reasoning-tokens
Nov 26, 2026
Merged

RulaKhaled merged 9 commits into
developfrom
fix/vercel-ai-parent-reasoning-tokens

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What

Vercel AI invoke_agent spans now report the sum of their model calls' token counts (input, output, total, cache and reasoning), on every ai version from v4 to v7, for generateText and streamText.

  • Each model call's counts are derived once, from its usage corrected by its own providerMetadata. Model-call spans record them, and parents sum them over their steps, the v7 streamed parent included.
  • v4–v6 streamText parents now get their steps and providerMetadata, which the streamed path used to drop.
  • Model calls read reasoning from the SDK's usage. That covers every provider on v6+, and OpenAI on v5, where @ai-sdk/openai 2.x reports it only there.
  • Zero reasoning is recorded, like the other AI integrations.

Why

providerMetadata describes one step, and steps can use different providers. Before this, a parent mixed the SDK's aggregate usage with the last step's corrections, so multi-step parents undercounted output and reasoning, and total could disagree with output. Google's output on @ai-sdk/google 2.x and v4 excludes reasoning, and v4 has reasoning only in each step's metadata. Summing per-call counts that are already final keeps a parent equal to its children. From v6 on, the SDK's output includes reasoning, which matches the conventions' subset rule. On v5, its flat reasoningTokens is read only for OpenAI, because other providers don't say whether their output includes it.

The v10 branch still has the old static/streamed accumulation and isn't covered here.

Refs: #23993

via Rola Abuhasna.

--

View Session [Sentry]

…pans

Read reasoning from the AI SDK's own usage instead of `providerMetadata`,
which only describes the last step. Parents now get the summed count, and
v5/v6 `generateText` parents read `totalUsage` rather than the last step's
`usage`.

Refs #23993

Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 4b42345 (PR test merge using develop at b47034c)

Against: b47034c (develop) · base run #68096

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.54 kB - -
@sentry/browser - with treeshaking flags 28.73 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.6 kB - -
@sentry/browser (incl. Tracing) 53.66 kB - -
@sentry/browser (incl. Tracing Core) 40.58 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 53.66 kB - -
@sentry/browser (incl. Tracing, Profiling) 56.56 kB - -
@sentry/browser (incl. Tracing, Replay) 93.59 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 82.38 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 98.45 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 111.39 kB - -
@sentry/browser (incl. Feedback) 48.4 kB - -
@sentry/browser (incl. sendFeedback) 31.11 kB - -
@sentry/browser (incl. FeedbackAsync) 36.16 kB - -
@sentry/browser (incl. Metrics) 31.86 kB - -
@sentry/browser (incl. Logs) 32.13 kB - -
@sentry/browser (incl. Metrics & Logs) 32.8 kB - -
@sentry/react 32.56 kB - -
@sentry/react (incl. Tracing) 56.37 kB - -
@sentry/vue 38.74 kB - -
@sentry/vue (incl. Tracing) 56.67 kB - -
@sentry/svelte 30.57 kB - -
@sentry/effect (client) 42.22 kB - -
@sentry/effect (client, incl. Tracing) 54.96 kB - -
@sentry/effect (client, incl. Tracing, Logs, Metrics) 66.31 kB - -
@sentry/remix (Remix 3 client bundle) 59.39 kB - -
CDN Bundle 32.38 kB - -
CDN Bundle (incl. Tracing) 54.04 kB - -
CDN Bundle (incl. Logs, Metrics) 35.13 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 56.5 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 76.08 kB - -
CDN Bundle (incl. Tracing, Replay) 91.84 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 94.37 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 97.31 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.78 kB - -
CDN Bundle - uncompressed 94.56 kB - -
CDN Bundle (incl. Tracing) - uncompressed 159.84 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 102.9 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 167.47 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 233.29 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 280.28 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 287.9 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 292.21 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 299.83 kB - -
@sentry/nextjs (client) 60.08 kB - -
@sentry/sveltekit (client) 55.02 kB - -
@sentry/core/server 45.83 kB - -
@sentry/core/browser 14.46 kB - -
@sentry/node 157.54 kB +0.11% +164 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.45 kB - -
@sentry/node - without tracing 89.79 kB - -
@sentry/node - without channel injection 146.52 kB +0.12% +162 B 🔺
@sentry/aws-serverless 100.42 kB - -
@sentry/cloudflare (withSentry) - minified 211.94 kB - -
@sentry/cloudflare (withSentry) 491.72 kB - -
@sentry/cloudflare (withSentry, instrumented CommonJS package) - minified 214.88 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 237.18 kB - -

View base workflow run

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 7 not successful, 56 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E nuxt-5 (development) Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E nuxt-5 (bun) Test (optional) Failing after 1m Details
❌ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Failing after 1m Details
❌ CI: Optional E2E Tests / E2E nuxt-5 (kv-cache) Test (optional) Failing after 1m Details
❌ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Failing after 1m Details
❌ CI: Optional E2E Tests / E2E nuxt-5 (deno) Test (optional) Failing after 1m Details
❌ CI: Optional E2E Tests / E2E nuxt-5 (cloudflare) Test (optional) Failing after 1m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E astro-7 (static trace lifecycle) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-without-tracing-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry (static trace lifecycle) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-14-supabase Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E hapi-21 (bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-4-sourcemaps (Vite server) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hapi-21 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (0.x) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (manual upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2-tunnel Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (content off) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 59s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (delete all) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (no upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (delete all) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 55s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (no upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 (MCP SDK OpenTelemetry) Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps enabled by Sentry) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (manual upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E vinext-cf-workers (workers cache) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (no upload) Test (optional) Successful in 1m Details

sentry-junior Bot and others added 6 commits November 25, 2026 20:16
Derive an invoke_agent span's output and reasoning from each step the way
its model-call span does, so each step is read by its own provider and v4
keeps reasoning from per-step `providerMetadata`. Pass `steps` and
`providerMetadata` through the v4-v6 `streamText` path, read v5 OpenAI
model-call reasoning from the usage, and record zero reasoning.
Compute a model call's final input, output, total, cache and reasoning
counts in one place, set them on model-call spans, and sum them over the
steps for invoke_agent spans, including the v7 streamed parent. Parents no
longer see last-step values, so total stays in step with the corrected
output, and `LAST_STEP_ONLY_USAGE_KEYS` goes away.

Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
Every `generateText`/`streamText` result from v4 to v7 exposes `steps`, so
drop the `totalUsage` reads and treat a result without steps as one model
call. Build the streamed result's shared fields once for both outcomes, and
detect a streamed result by `steps` so an aborted v6 stream doesn't leave
an unobserved `totalUsage` rejection.

Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
@RulaKhaled
RulaKhaled marked this pull request as ready for review November 26, 2026 11:10
@RulaKhaled
RulaKhaled self-requested a review November 26, 2026 11:11

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 370368c. Configure here.

nicohrubec pushed a commit that referenced this pull request Nov 26, 2026
…ts (#26922)

## What

`Plan CI jobs` checks out the PR base commit again before it computes
affected Nx projects. It stops failing with `fatal: bad object
<base.sha>`.

## Why

#26889 moved planning into `ci-planning.yml`,
and the base-commit checkout from the old test-matrix job didn't come
along. The job now fetches only the merge commit at depth 2, so the
affected-projects diff can't find the base for any PR whose merge-base
isn't the current `develop` tip. That's most PRs once `develop` moves,
for example #26904. This brings back the same
step the other jobs in `build.yml` still use.

<!-- junior-request-attribution:start -->
via **Rola Abuhasna**.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AD0C848BQ509%3A1795631065.014619 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AD0C848BQ509%3A1795631065.014619)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AD0C848BQ509%3A1795631065.014619/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Rola Abuhasna <rola.abuhasna@sentry.io>

@andreiborza andreiborza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing!

@RulaKhaled
RulaKhaled merged commit 901cf40 into develop Nov 26, 2026
748 of 757 checks passed
@RulaKhaled
RulaKhaled deleted the fix/vercel-ai-parent-reasoning-tokens branch November 26, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants