Repository navigation
fix(harness): count reasoning tokens once in the session spend - #787
Conversation
|
ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel. A member of the Team first needs to authorize it. |
Ishaan Gangwani (ishaan1124)
left a comment
There was a problem hiding this comment.
Thanks for digging into this. Output does already include reasoning for OpenAI, DeepSeek, OpenRouter and other OpenAI-compatible routes, so the double count is real there. But dropping tokens.reasoning from the sum undercounts other providers, because getUsage (backend/cli/src/session/index.ts, around line 797) stores the SDK's outputTokens unchanged, and the pinned SDKs report it differently:
@ai-sdk/google/google-vertex(2.0.52):outputTokensis Gemini'scandidatesTokenCount, and reasoning is the separatethoughtsTokenCount. With this PR, Gemini thinking tokens vanish from the spend figure.@ai-sdk/xai(2.0.51): xAI's totals add reasoning on top ofcompletion_tokens, so the same very likely applies.
So main's sum is actually right for Gemini. Also, the soft ceiling can't trip early from this: it compares spend.cost + spend.workers against max_usd (the env.lines hook in cost.ts), never spend.tokens. The token figure only appears in the reminder text.
Better to fix this where usage is recorded: in getUsage, set output = outputTokens + reasoningTokens when model.api.npm is @ai-sdk/google, @ai-sdk/google-vertex or @ai-sdk/xai. Then TokenUsage.uncached is right everywhere, and the catalog cost stops undercounting Gemini thinking, which is an existing bug. Please add a Gemini-shaped test.
c13f7e7 to
cf429cb
Compare
…t apart TokenUsage treats reasoning as a subset of output, so the session spend figure added a quantity that was already inside the number next to it and inflated the total on every OpenAI-compatible route, where the reported outputTokens is inclusive. Routes that bill it the other way round were wrong in the opposite direction and have been dropped from every total instead: `@ai-sdk/google` and `google-vertex` 2.0.52 map outputTokens to Gemini's candidatesTokenCount and report thinking as a separate thoughtsTokenCount, and `@ai-sdk/xai` 2.0.51 adds reasoning on top of completion_tokens, so the catalog cost was pricing their thinking at zero. Fold those back in inside getUsage, where the usage is recorded, rather than in the one consumer that happened to be wrong.
8ef2cd9 to
3619883
Compare
|
Done in 3619883 — this is now one commit, replacing the previous two-step change.
|
|
All checks are green on |
…al excludes it The npm-based fold treated all of @ai-sdk/xai alike, but grok-4.5 goes through xAI's Responses API, whose output_tokens already include reasoning, so a Responses step stored output 661 instead of 538 and the catalog cost came out about 19% high. Gemini and xAI chat bill reasoning outside outputTokens, and their SDKs pass the API's own total, which then exceeds input plus output by the reasoning; OpenAI, xAI Responses and the SDKs that compute a total (input plus output) never do. Fold on that test, counting cached tokens into the prompt when xAI reports them beside a smaller input. Drop the claim that the token overcount hid the soft ceiling: the ceiling compares dollars. Co-authored-by: Cursor <[email protected]>
|
I pushed one commit (00ea450) on top of yours. Folding reasoning into output by npm package double-counted on Grok 4.5, which goes through xAI's Responses API, where |
The requested changes are in and verified; CI is green.
# Conflicts: # CHANGELOG.md
ac1791f
into
synthetic-sciences:main
What
The session spend figure summed \ okens.reasoning\ on top of \ okens.input + tokens.output. Per the contract in \ ooling/util/src/token-usage.ts, \output\ already includes reasoning tokens and
easoning\ is a displayed subset of it,
ever an additional quantity to bill or add to a context total.
Why it matters
easoning, so the two disagreed.
Verification
A test that fails before and passes after:
\
(fail) reasoning is counted once ... Expected: 3000 Received: 4500
(pass) reasoning is counted once, since the provider already includes it in output
5 pass 0 fail
\\
All 4 pre-existing cost tests still pass. \�un run typecheck\ clean; the touched files are Prettier-clean (verified on LF-normalized copies, since this Windows checkout's \core.autocrlf=true\ makes Prettier flag every file in the repo).
The change
\\diff
easoning on top of output inflated
\\
Same one-token change at the \step-finish\ accumulation site, reusing the canonical helper instead of open-coding the arithmetic a third time.
Fixes #786