Skip to content

Conversation

@Pnkcaht
Copy link
Contributor

@Pnkcaht Pnkcaht commented Jan 22, 2026

Context

Token usage always shows 0% in the TUI because some providers emit token usage multiple times during streaming, including partial or zero-valued snapshots.

Before this change, the runtime would overwrite previously captured usage with later snapshots, causing the final session token usage to be zero even when tokens were actually consumed.
This PR fixes token usage reporting by capturing usage once per stream and treating it as immutable, making the behavior provider-agnostic.

References

Fixed #1475

What I did

  • Updated the runtime streaming handler to capture token usage only once
    per stream.
  • Prevented later partial or zero-valued usage snapshots from overwriting
    valid usage data.
  • Ensured session-level input/output token counts are populated correctly.
  • Preserved existing cost calculation logic while making it stable across
    providers (OpenAI, Anthropic, Gemini, Bedrock).

Before / After

Before

  • Token usage could be overwritten by later streaming events.
  • Providers emitting partial usage snapshots caused final usage to be zero.
  • TUI always displayed 0% token usage.

After

  • Token usage is captured once and treated as immutable per stream.
  • Partial or repeated usage snapshots no longer affect final totals.
  • TUI correctly displays token usage across all supported providers.

Signed-off-by: pnkcaht <samzoovsk19@gmail.com>
@Pnkcaht Pnkcaht requested a review from a team as a code owner January 22, 2026 15:23
@dgageot
Copy link
Member

dgageot commented Feb 7, 2026

@Pnkcaht Could you please rebase? Is it possible to add a test that shows the issue?

Signed-off-by: pnkcaht <samzoovsk19@gmail.com>
@Pnkcaht Pnkcaht force-pushed the fix/token-usage-issue branch from 6571944 to d04fb38 Compare February 7, 2026 17:31
@Pnkcaht
Copy link
Contributor Author

Pnkcaht commented Feb 7, 2026

I performed a rebase and added an example to the test file, which compiles and runs correctly. @dgageot

image

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.

<short bug description>

2 participants