Skip to content

Count OpenCode reasoning tokens once in output totals - #2823

Open
peyton-alt wants to merge 1 commit into
mainfrom
peyton/opencode-reasoning-in-totals
Open

peyton-alt wants to merge 1 commit into
mainfrom
peyton/opencode-reasoning-in-totals

Conversation

@peyton-alt

@peyton-alt peyton-alt commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

https://entire.io/gh/entireio/cli/trails/1615

What was wrong

OpenCode 1.3.16 and later store reasoning tokens apart from output tokens. CalculateTokenUsage summed tokens.output only, so reasoning was dropped. On a local OpenCode database that is an ~8% undercount of output tokens.

The rule

Per assistant message, output = output + reasoning, unless the message's total equals input + output + cache. That identity means reasoning is already inside output (messages written before 1.3.16), so it is not added again. total has been recorded since OpenCode 1.1.57. Messages without a total (zero-usage messages, or ones written before 1.1.57) are assumed to store reasoning apart. Verified against real opencode export output, which carries total; the new test fixtures use its field layout.

Not changed

  • Copilot CLI: its outputTokens already includes reasoning. On 66 local usage records, the billed output token count always equals outputTokens, and no separate reasoning token type is billed. Adding reasoning would double count.
  • Older checkpoints keep their lower stored counts. This only affects new checkpoints.

How it composes

  • Add per-agent cumulative token sources #2782: the Tokens.OutputWithReasoning code is identical, so the two merge cleanly in either order. This PR rewords its comment (drops "most providers", states the fallback plainly); Add per-agent cumulative token sources #2782 needs the same comment change. Add per-agent cumulative token sources #2782's cumulative OpenCode source already adds reasoning, so this makes the per-slice path agree with it. Merge order: land this before the ledger that switches sessions to cumulative baselines. If a session's earlier checkpoints were counted per-slice without reasoning, its first cumulative delta after that upgrade would otherwise absorb the missing reasoning as one jump.
  • The planned tokens rewrite already contains this fix. If it lands first, close this PR.

Tests

New cases fail on main: reasoning stored apart, a total matching neither layout, reasoning without a total, and an offset-2 slice over two assistant messages. Also pinned: reasoning already inside output, no reasoning with a total, a zero-usage message without a total. The OpenCode subagent integration test (built binary, simulated hooks) now expects 90 output tokens (80 + 10 reasoning).

Size: +107/-8.

🤖 Generated with Claude Code


Note

Medium Risk
Changes billing-adjacent token aggregation for OpenCode checkpoints; logic is version-aware but new totals only apply to new checkpoints, not historical ones.

Overview
Fixes OpenCode token usage undercounting by including reasoning tokens in output totals when OpenCode stores them separately (1.3.16+).

CalculateTokenUsage now sums Tokens.OutputWithReasoning() instead of raw output. The helper parses export total and only adds reasoning when it is not already folded into output (when total == input + output + cache). Messages with no total still add reasoning on top of output.

Tests cover pre/post-1.3.16 layouts, missing or mismatched total, and offset slicing; existing fixtures and the OpenCode subagent integration test expect higher output counts (e.g. 80 → 90).

Reviewed by Cursor Bugbot for commit 8718ae2. Configure here.

OpenCode 1.3.16 and later store reasoning apart from output, and
CalculateTokenUsage summed output only, so those reasoning tokens were
dropped. Each message now adds its reasoning unless its total shows
reasoning is already inside output, using the same helper as the
cumulative path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Entire-Checkpoint: 01M5BGFH04A974ABBPQ29WCHT6
@peyton-alt
peyton-alt requested a review from a team as a code owner October 20, 2026 01:59
Copilot AI balanced review requested due to automatic review settings October 20, 2026 01:59
peyton-alt added a commit that referenced this pull request Oct 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Entire-Checkpoint: 01M5BGKW3PXX4NWKVHK414YKHG

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The localized implementation matches the stated accounting rule with focused regression coverage and no blocking findings; tests were not executed.

No open issues 🎉

What changed in this PR

Corrects OpenCode checkpoint output totals by counting separately stored reasoning tokens without double-counting reasoning already included in output.

Changes:

  • Adds total-aware reasoning token accounting.
  • Applies it to transcript token aggregation.
  • Expands regression coverage and updates subagent expectations.
File Description
cmd/​entire/​cli/​integration_test/​opencode_subagent_test.go Expects reasoning-inclusive subagent output totals.
cmd/​entire/​cli/​agent/​opencode/​types.go Adds total and the reasoning accounting helper.
cmd/​entire/​cli/​agent/​opencode/​transcript.go Uses the helper when aggregating output tokens.
cmd/​entire/​cli/​agent/​opencode/​transcript_test.go Covers token layouts, fallback behavior, and offset slicing.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants