fix(ui): correctly display token usage across sessions#1509
Open
onematchfox wants to merge 2 commits intokagent-dev:mainfrom
Open
fix(ui): correctly display token usage across sessions#1509onematchfox wants to merge 2 commits intokagent-dev:mainfrom
onematchfox wants to merge 2 commits intokagent-dev:mainfrom
Conversation
- 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>
Contributor
There was a problem hiding this comment.
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(frominput/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.
EItanya
approved these changes
Mar 16, 2026
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.
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.maxindependently per field (total,prompt,completion) across all tasks in a session. This produces incoherent values wheretotaldoes not necessarily matchprompt+completion, since each field1 (most notablyprompt) 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:
Footnotes
not 100% certain here but I think that fields other than input could also peak at different levels within cached prompt scenarios. ↩