Skip to content

fix(harness): count reasoning tokens once in the session spend - #787

Merged
Ishaan Gangwani (ishaan1124) merged 3 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/harness-reasoning-token-count
Sep 29, 2026
Merged

Ishaan Gangwani (ishaan1124) merged 3 commits into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/harness-reasoning-token-count

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

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

  • The figure is what the model reads about its own spend, so it reasoned about an inflated total.
  • \harness.cost.max_usd\ is a soft ceiling checked against this spend. A long reasoning session could cross the ceiling while real spend was well under it, and the wrap-up reminder would end the work early.
  • The usage report (\src/session/usage.ts) already used \TokenUsage.total, which excludes
    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

  •  tokens += message.info.tokens.input + message.info.tokens.output + message.info.tokens.reasoning
    
  •  // A reasoning model's reasoning tokens are a subset of its output, not
    
  •  // an extra quantity, so adding 
    

easoning on top of output inflated

  •  // the figure and let a soft ceiling pass unnoticed.
    
  •  tokens += TokenUsage.uncached(message.info.tokens)
    

\\

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

@vercel

vercel Bot commented Sep 28, 2026

Copy link
Copy Markdown

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.

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 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): outputTokens is Gemini's candidatesTokenCount, and reasoning is the separate thoughtsTokenCount. With this PR, Gemini thinking tokens vanish from the spend figure.
  • @ai-sdk/xai (2.0.51): xAI's totals add reasoning on top of completion_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.

…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.
@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

Done in 3619883 — this is now one commit, replacing the previous two-step change.

  • Moved the fix to where you said, getUsage in backend/cli/src/session/index.ts. A REASONING_OUTSIDE_OUTPUT set holds @ai-sdk/google, @ai-sdk/google-vertex and @ai-sdk/xai, and for those output becomes outputTokens + reasoningTokens. Since the catalog cost is billed off tokens.output, that also stops Gemini thinking being priced at zero, which I agree is the underlying bug.
  • TokenUsage.uncached stays the one expression the harness uses, so the figure is right for every route and the double count on the OpenAI-compatible ones is gone.
  • The cost.ts comment claiming the soft ceiling "let a ceiling pass unnoticed" is removed — you are right that the gate compares spend.cost + spend.workers against max_usd and never reads spend.tokens; the token figure only reaches the reminder text.
  • Tests: a Gemini-shaped case asserting output 500 → 600 with uncached 1,400, total 1,600 and the cost recomputed off 600; the same for xAI; and a case pinning that an OpenAI-compatible route keeps its inclusive 500 so it is not double folded. Both folding tests fail against the previous getUsage. The existing five-route loop is untouched and still passes — it passes a model with no api, which is why the lookup is input.model.api?.npm rather than api.npm.
  • The CHANGELOG entry was a truncated one-liner ending in "A"; it is written out now and covers both directions.

@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

All checks are green on 3619883e — the rebase resolved the conflict and the new tests pass. Ishaan Gangwani (@ishaan1124) ready for another look whenever you have a moment.

…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]>
@ishaan1124

Copy link
Copy Markdown
Member

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 output_tokens already includes reasoning (661 stored instead of 538). The fold now happens only when the provider's own total shows reasoning billed outside output, which holds for Gemini and xAI chat and never for OpenAI or xAI Responses. I also added a Grok 4.5 Responses test and dropped the soft-ceiling clause from the cost.ts comment, since the ceiling compares dollars. CI is green.

@ishaan1124
Ishaan Gangwani (ishaan1124) dismissed their stale review September 29, 2026 15:33

The requested changes are in and verified; CI is green.

@ishaan1124
Ishaan Gangwani (ishaan1124) merged commit ac1791f into synthetic-sciences:main Sep 29, 2026
8 of 9 checks passed
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.

The session spend figure counts reasoning tokens twice, so a soft ceiling can fire early

2 participants