fix: accumulate tokens across multi-step tool calls instead of overwriting#22077
Open
KonstantinMirin wants to merge 1 commit intoanomalyco:devfrom
Open
fix: accumulate tokens across multi-step tool calls instead of overwriting#22077KonstantinMirin wants to merge 1 commit intoanomalyco:devfrom
KonstantinMirin wants to merge 1 commit intoanomalyco:devfrom
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
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.
Issue for this PR
Fixes #21913
Type of change
What does this PR do?
In multi-step tool-call messages,
processor.tsoverwritesassistantMessage.tokenson eachfinish-stepevent instead of accumulating additive fields. Only the last step'soutput,reasoning, andcache.writesurvive. The fix: accumulateoutput,reasoning,cache.writewith+=across steps; replaceinputandcache.readwith the latest step's values (they're snapshots).Additionally fixes custom provider
limit.contextdefaulting to0(now128_000) andlimit.outputdefaulting to0(now4_096), which broke context % display and disabled auto-compaction.Key changes:
getUsage()derivestotalfrom components (input + output + reasoning + cache.read + cache.write) instead of using API'stotalTokens(which double-counts cached tokens)promptSize()andtotalSize()named helpersMessageV2.totalSize()for compaction threshold (output/reasoning are part of next turn's context)DEFAULT_CONTEXT_LIMIT/DEFAULT_OUTPUT_LIMITinstead of magic0cache.writein ACP usage reportingHow did you verify your code works?
1903 existing tests pass + 12 new tests covering:
promptSize/totalSizehelpers (3 unit tests)isOverflowpure unit tests (5 tests: disabled, zero context, underground, overflow, custom reserved)getUsagetotal derivation from components (1 test)0to128_000/4_096)Screenshots / recordings
N/A — no UI changes.
Checklist