Skip to content

fix: accumulate tokens across multi-step tool calls instead of overwriting - #22032

Closed
KonstantinMirin wants to merge 1 commit into
anomalyco:devfrom
KonstantinMirin:fix/multi-step-token-accumulation
Closed

KonstantinMirin wants to merge 1 commit into
anomalyco:devfrom
KonstantinMirin:fix/multi-step-token-accumulation

Conversation

@KonstantinMirin

Copy link
Copy Markdown

Fixes #21913

The bug

In multi-step tool-call assistant messages, processor.ts overwrites assistantMessage.tokens on each finish-step event instead of accumulating additive fields. Only the last step's output, reasoning, and cache.write survive. cost was 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 steps
  • total: derived from components by getUsage() instead of using API's totalTokens (which double-counts cached tokens)

Derived total (index.ts) — getUsage() computes total = input + output + reasoning + cache.read + cache.write. Kept as a top-level field (not inside tokens) so existing consumers of msg.tokens.total continue to work.

Named helpers (message-v2.ts) — MessageV2.promptSize() (input + cache) and MessageV2.totalSize() (all fields) so consumers don't hand-compute the sum inconsistently.

Compaction (overflow.ts) — Uses MessageV2.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.context defaults to 128_000 and limit.output to 4_096 via named constants instead of 0.

ACP usage (agent.ts) — Includes cache.write in the used token count.

Test coverage

  • 3 modified existing tests: getUsage total assertions (now derived), provider limit defaults (128k/4096), overflow semantics (uses totalSize)
  • 12 new tests: promptSize/totalSize helpers, isOverflow pure unit tests, getUsage total derivation, 3-step accumulation simulation mirroring processor.ts logic

…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
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

@github-actions

Copy link
Copy Markdown
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.

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.

Multi-step tool calls overwrite token counts instead of accumulating (output, reasoning, cache.write lost)

1 participant