Skip to content

fix(ui): correctly display token usage across sessions#1509

Open
onematchfox wants to merge 2 commits intokagent-dev:mainfrom
onematchfox:fix-token-usage-display
Open

fix(ui): correctly display token usage across sessions#1509
onematchfox wants to merge 2 commits intokagent-dev:mainfrom
onematchfox:fix-token-usage-display

Conversation

@onematchfox
Copy link
Contributor

When testing #1500, I struggled to visually demonstrate the affect of the duplicated session history (aka context bloat) when chatting with agents. The current implementation of "Usage" within the UI (introduced in #679) uses Math.max independently per field (total, prompt, completion) across all tasks in a session. This produces incoherent values where total does not necessarily match prompt + completion, since each field1 (most notably prompt) can peak during differing tasks. And, it doesn't correctly update to reflect usage across multiple API calls in a multi-turn conversation. All we see is which API call has the greatest of each type of token usage.

This PR addresses those shortcomings by:

  • Updating the existing token usage display to reflect session total across multiple turns
  • Show per-call token usage tooltip on tool call cards and text messages

Footnotes

  1. not 100% certain here but I think that fields other than input could also peak at different levels within cached prompt scenarios.

- Update existing token usage display to reflect session total across multiple turns
- Show per-call token usage tooltip on tool call cards and text messages

Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
@onematchfox onematchfox marked this pull request as ready for review March 16, 2026 15:20
@onematchfox onematchfox requested a review from peterj as a code owner March 16, 2026 15:20
Copilot AI review requested due to automatic review settings March 16, 2026 15:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes incoherent token-usage reporting in the chat UI by switching from per-field Math.max aggregation to true per-turn/per-session accumulation, and by surfacing per-call usage on individual messages/tool cards.

Changes:

  • Rename token usage fields to prompt/completion (from input/output) and update UI accordingly.
  • Accumulate session token totals across multiple invocations/turns and persist pending turn stats across React re-renders.
  • Add per-message/per-tool-call token usage tooltips and inject per-call stats into message metadata.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
ui/src/types/index.ts Updates TokenStats shape to prompt/completion for clearer semantics.
ui/src/lib/messageHandlers.ts Implements per-invocation tagging + session accumulation; injects tokenStats into message metadata.
ui/src/lib/tests/messageHandlers.test.ts Adjusts and expands tests for message enrichment + summed stats across history/invocations.
ui/src/components/chat/ToolCallDisplay.tsx Passes per-call tokenStats into tool call UI components.
ui/src/components/chat/TokenStatsTooltip.tsx Adds a tooltip component to display per-call token usage.
ui/src/components/chat/TokenStats.tsx Updates session usage display to prompt/completion and renames component.
ui/src/components/chat/ChatMessage.tsx Adds per-message token usage tooltip next to feedback controls.
ui/src/components/chat/ChatInterface.tsx Tracks session totals, preserves pending turn stats via ref, and memoizes handlers.
ui/src/components/chat/AgentCallDisplay.tsx Displays per-call token usage tooltip on agent call cards.
ui/src/components/ToolDisplay.tsx Displays per-call token usage tooltip on tool cards.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

3 participants