Repository navigation
fix: accumulate tokens across multi-step tool calls instead of overwriting - #22032
Closed
KonstantinMirin wants to merge 1 commit into
Closed
KonstantinMirin wants to merge 1 commit into
KonstantinMirin wants to merge 1 commit into
Conversation
…iting Multi-step tool-call messages only retained the last step's output, reasoning, and cache.write tokens because processor.ts overwrote assistantMessage.tokens on each finish-step event instead of accumulating additive fields. - Accumulate output, reasoning, cache.write with += across steps - Replace input and cache.read with latest step's values (snapshots) - Derive total from components instead of API totalTokens (which double-counts cached tokens in AI SDK v6) - Add MessageV2.promptSize() and totalSize() helpers for consistent computation across consumers - Use totalSize() for compaction threshold (output/reasoning are part of the next turn's context) - Fix custom provider limit.context defaulting to 0 (now 128,000) and limit.output defaulting to 0 (now 4,096) which broke context % display and disabled auto-compaction - Include cache.write in ACP usage reporting Fixes anomalyco#21913
Contributor
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
Contributor
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
This was referenced Aug 13, 2026
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.
Fixes #21913
The bug
In multi-step tool-call assistant messages,
processor.tsoverwritesassistantMessage.tokenson eachfinish-stepevent instead of accumulating additive fields. Only the last step'soutput,reasoning, andcache.writesurvive.costwas correctly accumulated with+=.Separately, custom provider models not in models.dev got
limit.context: 0, which broke context % display and disabled auto-compaction entirely.Changes
Token accumulation (
processor.ts) — Field-wise accumulation replaces wholesale overwrite:input,cache.read: replaced each step (snapshots of current state)output,reasoning,cache.write: accumulated with+=across stepstotal: derived from components bygetUsage()instead of using API'stotalTokens(which double-counts cached tokens)Derived
total(index.ts) —getUsage()computestotal = input + output + reasoning + cache.read + cache.write. Kept as a top-level field (not insidetokens) so existing consumers ofmsg.tokens.totalcontinue to work.Named helpers (
message-v2.ts) —MessageV2.promptSize()(input + cache) andMessageV2.totalSize()(all fields) so consumers don't hand-compute the sum inconsistently.Compaction (
overflow.ts) — UsesMessageV2.totalSize()for overflow check. Output/reasoning tokens are included because they become part of the next turn's context.Custom provider defaults (
provider.ts) —limit.contextdefaults to128_000andlimit.outputto4_096via named constants instead of0.ACP usage (
agent.ts) — Includescache.writein theusedtoken count.Test coverage
getUsagetotal assertions (now derived), provider limit defaults (128k/4096), overflow semantics (usestotalSize)promptSize/totalSizehelpers,isOverflowpure unit tests,getUsagetotal derivation, 3-step accumulation simulation mirroring processor.ts logic