Skip to content

fix: clamp output token count to prevent negative values#9292

Closed
singhvishalkr wants to merge 1 commit intoKilo-Org:mainfrom
singhvishalkr:fix/clamp-negative-output-tokens
Closed

fix: clamp output token count to prevent negative values#9292
singhvishalkr wants to merge 1 commit intoKilo-Org:mainfrom
singhvishalkr:fix/clamp-negative-output-tokens

Conversation

@singhvishalkr
Copy link
Copy Markdown
Contributor

Summary

  • Clamp output token count in getUsage to Math.max(0, ...) to prevent negative values when a provider reports reasoningTokens > outputTokens

Fixes #9168

Root cause

packages/opencode/src/session/index.ts:319 subtracts reasoningTokens from outputTokens to 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

  • Display: no more negative token counts in TUI, VS Code extension, or session exports
  • Stats: stats.ts aggregation no longer skewed by negative output values
  • Cost: unchanged (Kilo gateway cost is reported separately)

Changes

packages/opencode/src/session/index.ts (line 319):

\\diff

  • output: safe(outputTokens - reasoningTokens),
  • output: Math.max(0, safe(outputTokens - reasoningTokens)),
    \\

Test plan

  • Build passes (pnpm build)
  • With a model that has reasoningTokens > outputTokens, output shows 0 instead of negative
  • Normal sessions (reasoningTokens < outputTokens) are unaffected

@kilo-code-bot
Copy link
Copy Markdown
Contributor

kilo-code-bot Bot commented Apr 21, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 files)
  • packages/opencode/src/session/session.ts

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.
@singhvishalkr singhvishalkr force-pushed the fix/clamp-negative-output-tokens branch from 0d9ce1f to 474f31b Compare April 23, 2026 07:31
@singhvishalkr
Copy link
Copy Markdown
Contributor Author

Rebased onto main at 74df868. The session code moved from packages/opencode/src/session/index.ts (now a barrel re-export) into packages/opencode/src/session/session.ts, so the one-line clamp has been re-applied against the new location at session.ts:321. Diff is unchanged in intent: output: Math.max(0, safe(outputTokens - reasoningTokens)).

@marius-kilocode
Copy link
Copy Markdown
Collaborator

Duplicate of #9178 with the same patch. That one was opened first and references the underlying issue #9168, so closing this in favor of it.

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.

Negative output token count when reasoning > outputTokens (Kilo gateway / Moonshot kimi-k2.5 thinking)

2 participants