Repository navigation
fix(server-utils): Sum Vercel AI reasoning tokens onto invoke_agent spans - #26904
Merged
Merged
Conversation
…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>
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.
|
Contributor
Optional E2E testsSome optional checks were not successful. They do not block the merge. 7 not successful, 56 successful. See the run.
Successful checks
|
RulaKhaled
reviewed
Nov 25, 2026
RulaKhaled
reviewed
Nov 25, 2026
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
marked this pull request as ready for review
November 26, 2026 11:10
javascript-sdk-gitflow
Bot
requested review from
andreiborza,
isaacs and
timfish
November 26, 2026 11:11
RulaKhaled
self-requested a review
November 26, 2026 11:11
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ 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>
51 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What
Vercel AI
invoke_agentspans now report the sum of their model calls' token counts (input, output, total, cache and reasoning), on everyaiversion from v4 to v7, forgenerateTextandstreamText.providerMetadata. Model-call spans record them, and parents sum them over their steps, the v7 streamed parent included.streamTextparents now get their steps andproviderMetadata, which the streamed path used to drop.@ai-sdk/openai2.x reports it only there.Why
providerMetadatadescribes 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/google2.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 flatreasoningTokensis read only for OpenAI, because other providers don't say whether their output includes it.The
v10branch still has the old static/streamed accumulation and isn't covered here.Refs: #23993
via Rola Abuhasna.
--
View Session [Sentry]