fix: clamp output token count to prevent negative values#9292
Closed
singhvishalkr wants to merge 1 commit intoKilo-Org:mainfrom
Closed
fix: clamp output token count to prevent negative values#9292singhvishalkr wants to merge 1 commit intoKilo-Org:mainfrom
singhvishalkr wants to merge 1 commit intoKilo-Org:mainfrom
Conversation
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (1 files)
Reviewed by gpt-5.4-20260305 · 372,255 tokens |
Some providers report reasoning tokens greater than the total output tokens for a turn, which made the per-message telemetry render negative output counts and occasionally break UI totals. Clamp the subtraction to zero so the output token count is always non-negative.
0d9ce1f to
474f31b
Compare
Contributor
Author
|
Rebased onto |
Collaborator
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.
Summary
outputtoken count ingetUsagetoMath.max(0, ...)to prevent negative values when a provider reportsreasoningTokens > outputTokensFixes #9168
Root cause
packages/opencode/src/session/index.ts:319subtractsreasoningTokensfromoutputTokensto derive the 'visible output' count. When the Kilo gateway reports an inconsistent usage breakdown (reasoningTokens > outputTokens), the result goes negative.Observed with:
moonshotai/kimi-k2.5(thinking variant) via Kilo gateway.Impact of fix
stats.tsaggregation no longer skewed by negative output valuesChanges
packages/opencode/src/session/index.ts(line 319):\\diff
\\
Test plan
pnpm build)reasoningTokens > outputTokens, output shows 0 instead of negative