anomalyco / anomalyco/opencode

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

Open
#42,324 3 comments 0 reactions 1 assignee View on GitHub

@kitlangton is already working on this.

Since Aug 13, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
Avg merge
7h 2m
Merged PRs (30d)
384

Description

Bug

In multi-step tool-call assistant messages, processor.ts overwrites assistantMessage.tokens on each step-finish event instead of accumulating additive fields. Only the last step's output, reasoning, and cache.write survive.

Root cause

packages/opencode/src/session/processor.ts step-finish handler:

ctx.assistantMessage.cost += usage.cost     // accumulates
ctx.assistantMessage.tokens = usage.tokens  // overwrites!

Impact

For an assistant message with N tool-call steps, the message-level tokens only reflect the last step:

  • Context metrics (session context meter) under-report token usage for multi-step messages
  • compaction.isOverflow checks use lastFinished.tokens (prompt.ts), so underestimating tokens delays auto-compaction and makes the next request more likely to hit the provider context limit

Notes

  • Follow-up to #21913, which was auto-closed for inactivity (bot comment says to open a new one if still relevant). The bug still exists on the latest dev.
  • Earlier fix attempts (#22077, #22032, #10215) were closed unmerged by automated cleanup, not by technical review.

Expected behavior

Output, reasoning, and cache.write tokens should accumulate across steps (like cost already does). Input and cache.read should keep the final step's values, since the final step's request already includes all prior steps.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.