Repository navigation
Count OpenCode reasoning tokens once in output totals - #2823
Open
peyton-alt wants to merge 1 commit into
Open
peyton-alt wants to merge 1 commit into
peyton-alt wants to merge 1 commit into
Conversation
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
added a commit
that referenced
this pull request
Oct 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Entire-Checkpoint: 01M5BGKW3PXX4NWKVHK414YKHG
Contributor
There was a problem hiding this comment.
🟢 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
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.
https://entire.io/gh/entireio/cli/trails/1615
What was wrong
OpenCode 1.3.16 and later store reasoning tokens apart from output tokens.
CalculateTokenUsagesummedtokens.outputonly, 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'stotalequalsinput + output + cache. That identity means reasoning is already inside output (messages written before 1.3.16), so it is not added again.totalhas been recorded since OpenCode 1.1.57. Messages without atotal(zero-usage messages, or ones written before 1.1.57) are assumed to store reasoning apart. Verified against realopencode exportoutput, which carriestotal; the new test fixtures use its field layout.Not changed
outputTokensalready includes reasoning. On 66 local usage records, the billed output token count always equalsoutputTokens, and no separate reasoning token type is billed. Adding reasoning would double count.How it composes
Tokens.OutputWithReasoningcode 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.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+).
CalculateTokenUsagenow sumsTokens.OutputWithReasoning()instead of rawoutput. The helper parses exporttotaland only addsreasoningwhen it is not already folded intooutput(whentotal == input + output + cache). Messages with nototalstill 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.