Skip to content

feat(ai-chat-ui): add token usage indicator to chat view#17028

Merged
ndoschek merged 4 commits intomasterfrom
feat/token-ui
Apr 10, 2026
Merged

feat(ai-chat-ui): add token usage indicator to chat view#17028
ndoschek merged 4 commits intomasterfrom
feat/token-ui

Conversation

@eneufeld
Copy link
Copy Markdown
Contributor

@eneufeld eneufeld commented Feb 20, 2026

What it does

Adds a per-session token usage indicator to the chat view, showing current conversation context size vs a 200k token budget.

Key design: extends TokenUsageService with a required sessionId field so all LLM providers (Anthropic, OpenAI, Google, Vercel, Copilot, Ollama) feed the indicator.

  • Add sessionId to TokenUsageParams/TokenUsage types (required)
  • Update all providers to pass sessionId on recordTokenUsage
  • Flow sessionId through TokenUsageServiceImpl backend → RPC → frontend
  • Add UsageResponsePart cache fields (cache_creation_input_tokens, cache_read_input_tokens)
  • Display latest inputTokens + cachedInputTokens + readCachedInputTokens + outputTokens to reflect actual token window pressure
  • Show a compact circular progress ring next to the send button that fills proportionally with token usage
  • Use muted fill for normal usage, yellow for warning (90%+), red for overflow (100%+)
  • Tint the chat input box border yellow/red when approaching or exceeding the context window limit
  • Show a hover tooltip with token breakdown (input/output, cache read/creation, total) and warning hints
  • Add consistent "Token Usage" header in hover tooltips across the ring and agent description hovers
  • CSS with Theia variable conventions and color thresholds
  • Controlled via experimental preference ai-features.chat.tokenUsageIndicator.enabled

Resolves GH-17322

How to test

  • Activate the experimental setting ai-features.chat.tokenUsageIndicator.enabled
  • Send messages to LLMs and see how the token count increases (ideally using different models)

Follow-ups

The context window of 200k tokens is hardcoded. This should come from the models.

See also #16703 for further follow ups

Breaking changes

  • This PR introduces breaking changes and requires careful review. If yes, the breaking changes section in the changelog has been updated.

Attribution

Review checklist

Reminder for reviewers

@eneufeld eneufeld requested review from ndoschek and sdirix February 20, 2026 11:56
@github-project-automation github-project-automation Bot moved this to Waiting on reviewers in PR Backlog Feb 20, 2026
Copy link
Copy Markdown
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

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

Seemingly works for me. I tried some requests and the numbers went up.

Major

  • I am not sure we need a new service for token counting. We already have usage response parts which we currently ignore. They could be reused for token counting
  • Connected with that, I think the token count should be stored in the model. We could do it via the usage response parts or store them in additional fields or data. Currently the count lives in React state which is a temporary place and can not handle persistence, chat branches, chat switching etc.

Minor
Until the 200k limit has any specific meaning I would personally not show it and just count the tokens.

Comment thread packages/ai-chat-ui/src/browser/style/index.css Outdated
Comment thread packages/ai-ollama/src/node/ollama-language-model.ts Outdated
Comment thread packages/ai-core/src/common/language-model.ts
Comment thread packages/ai-core/src/common/token-usage-service.ts
Comment thread packages/ai-chat-ui/src/browser/chat-token-usage-indicator.tsx Outdated
@github-project-automation github-project-automation Bot moved this from Waiting on reviewers to Waiting on author in PR Backlog Feb 23, 2026
@eneufeld
Copy link
Copy Markdown
Contributor Author

There is no new service in place. I just extended the existing one.
But I see your point of using the usage report parts. But that would be a bigger change as those need to be collected for a report.
The 200k magic number is needed to show a green/yellow/red bar

@sdirix
Copy link
Copy Markdown
Member

sdirix commented Feb 24, 2026

You're right, I meant the new client, not service.

Would you prefer to rework the approach or keep the one of the PR of now? The current implementation could serve as POC but it has so many downsides, I am not sure it's good to merge it in this state.

@eneufeld
Copy link
Copy Markdown
Contributor Author

@ndoschek if you agree i would rework this but it won't make the february release then

@sdirix sdirix marked this pull request as draft February 26, 2026 10:05
@eneufeld eneufeld force-pushed the feat/token-ui branch 2 times, most recently from 2234bd0 to 5d6d461 Compare February 28, 2026 09:33
@eneufeld eneufeld marked this pull request as ready for review February 28, 2026 09:42
@eneufeld eneufeld requested a review from sdirix February 28, 2026 09:42
@ndoschek ndoschek marked this pull request as draft March 2, 2026 08:41
@eneufeld
Copy link
Copy Markdown
Contributor Author

eneufeld commented Mar 2, 2026

@ndoschek why did you change this to draft? I updated the pr and it is ready imho

@ndoschek ndoschek marked this pull request as ready for review March 2, 2026 13:56
@ndoschek
Copy link
Copy Markdown
Member

ndoschek commented Mar 2, 2026

Ah sorry Eugen, I was working through my GH notification mails and only saw your comment from before, changed it back again

Copy link
Copy Markdown
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

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

Looks good conceptually to me.

I wonder whether we should also record the LLM/Provider into each response as the token count is conceptually dependent on that, what do you think?

Comment thread packages/ai-chat-ui/src/browser/chat-token-usage-indicator.tsx Outdated
Comment thread packages/ai-chat/src/common/chat-agents.ts
Comment thread packages/ai-vercel-ai/src/node/vercel-ai-language-model.ts Outdated
sdirix
sdirix previously requested changes Mar 18, 2026
Copy link
Copy Markdown
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

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

Generally speaking the PR works but it's easy to get into a "broken" state.

Image

I let Coder look at a larger lock file. Initially the context was counted as 6k tokens. Then after I sent the next message, it suddenly jumped to 569k. I was using Opus 4.6. Coder still worked properly, so it's definitely some wrong counting


The initial UI does not look good:

Image

Because the content-bar is not filled, it looks like a broken separator. Personally I would not show the bar nor the token count at all if no tokens were counted.


Maybe we should mark this feature as experimental and disable it by default in Theia itself? For "Theia Next" we could enable it by default via the preferences, so it gets some testing before we hand it over to the users.

The PR must also be rebased/merged with master.

const tokenUsage = isResponseNode(node) ? node.response.tokenUsage : undefined;
const hasTokenInfo = tokenUsage && (tokenUsage.inputTokens > 0 || tokenUsage.outputTokens > 0);
const tokenInfo = hasTokenInfo
? `Input: ${formatTokenCount(tokenUsage.inputTokens)} · Output: ${formatTokenCount(tokenUsage.outputTokens)}`
Copy link
Copy Markdown
Member

@sdirix sdirix Mar 18, 2026

Choose a reason for hiding this comment

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

"Input:", "Output:", and the · separator are user-facing strings and must be localized with nls.localize.

@ndoschek ndoschek moved this to In Review in Theia Roadmap Mar 23, 2026
@ndoschek ndoschek mentioned this pull request Mar 23, 2026
25 tasks
@eneufeld eneufeld force-pushed the feat/token-ui branch 2 times, most recently from a8dc1b4 to a831c30 Compare April 1, 2026 12:53
@eneufeld eneufeld requested a review from sdirix April 1, 2026 22:42
Copy link
Copy Markdown
Member

@ndoschek ndoschek left a comment

Choose a reason for hiding this comment

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

Thank you @eneufeld, overall this works nicely!

I have a few inline comments and one comment below.

I agree with Stefan, that the hardcoded 200k context window can be a bit misleading across different models.
About the default setting to false, I can take care of opening a ticket that we enable it for the Theia next product after this PR has been completed.

One additional thing I noticed:
When a request is aborted by the user, the token usage indicator is not updated for that request afaics. It keeps showing the data from the previous request. The tokens consumed by the aborted request seem to then show up added to the next request (if any), since the next request's input includes the prior conversation context. It would be good to also update the indicator on abort with whatever usage data was received up to that point. This could also be addressed in a follow-up or documented as a known limitation for now.

Comment thread packages/ai-chat-ui/src/browser/style/index.css Outdated
Comment thread packages/ai-chat-ui/src/browser/chat-view-widget.tsx Outdated
Comment thread packages/ai-chat/src/common/chat-model.ts
Comment thread packages/ai-chat-ui/src/browser/chat-token-usage-indicator.tsx Outdated
Comment thread packages/ai-chat-ui/src/browser/style/index.css Outdated
Comment thread packages/ai-chat-ui/src/browser/style/index.css
Comment thread packages/ai-anthropic/src/node/anthropic-language-model.ts Outdated
Comment thread packages/ai-chat-ui/src/browser/chat-tree-view/chat-view-tree-widget.tsx Outdated
eneufeld added 2 commits April 8, 2026 16:01
Add a context window usage indicator bar between the chat messages
and input area, showing cumulative token consumption across all
requests in a session.

Architecture:
- Language models yield UsageResponsePart inline in their
  stream/response instead of calling TokenUsageService directly
- AbstractChatAgent captures usage into ChatResponseModel.tokenUsage
  and records it centrally via TokenUsageService
- Token usage is serialized/restored with chat sessions
- UI reads directly from the chat model

Changes across providers:
- Remove TokenUsageService injection from all 6 provider managers
  (Anthropic, Google, Ollama, OpenAI, Copilot, Vercel AI)
- All providers yield usage data as UsageResponsePart in streaming
  and return usage on text/parsed responses
- Claude Code and Codex agents call setTokenUsage() directly
  as they don't extend AbstractChatAgent

UI:
- Progress bar with green/yellow/red color coding against
  a 200k context window
- Per-response token counts shown on agent label hover
- Comprehensive unit tests for indicator logic and rendering
@ndoschek ndoschek dismissed sdirix’s stale review April 9, 2026 12:41

Stefan is on vacation this week, Simon will take over

@ndoschek ndoschek self-requested a review April 9, 2026 12:41
Copy link
Copy Markdown
Member

@ndoschek ndoschek left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @eneufeld, works great for me 👍
As discussed yesterday, I had a quick look and did a little redesign of the UI indicator and pushed it on top.
I'll ask Simon to have a final look.

The UI is now a donut ring icon, filling up according to token usage.
For getting closer to the limit, we use warning colors for the icon and the input broder and add an info message to the hover content. If the limit was reached we use error colors and a different info message.

Some screenshots:

image image image

@ndoschek ndoschek dismissed their stale review April 9, 2026 12:45

Simon will take over as I did the redesign of the UI

@ndoschek ndoschek requested a review from sgraband April 9, 2026 12:46
ndoschek added a commit to eclipse-theia/theia-ide that referenced this pull request Apr 9, 2026
For more extensive testing we enable the token UI in the
chat view by default for the Theia IDE next product
See also eclipse-theia/theia#17028
- Remove standalone ChatTokenUsageIndicatorWidget and its DI binding
- Integrate token usage display into the chat input widget directly
- Replace full-width progress bar with a compact circular progress ring
  next to the send button, using conic-gradient fill
- Tint input box border yellow/red when approaching or exceeding
  context window limit
- Show multiline hover tooltip with input/output/cache breakdown
  and warning hints for warning/error states
- Use MarkdownString for hover to support proper line breaks
- Reuse localization keys for token info across badge and agent
  description hovers
- Keep utility functions (formatTokenCount, getUsageColorClass, etc.)
  and their tests, remove widget-specific tests
- Rename tsx file to util ts file as we do not have a dedicated indicator
  widget anymore
Copy link
Copy Markdown
Contributor

@sgraband sgraband left a comment

Choose a reason for hiding this comment

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

Thank you! I really like the new design! LGTM 👍

@github-project-automation github-project-automation Bot moved this from Waiting on author to Needs merge in PR Backlog Apr 10, 2026
@ndoschek ndoschek merged commit 2fa22c7 into master Apr 10, 2026
24 of 32 checks passed
@ndoschek ndoschek deleted the feat/token-ui branch April 10, 2026 14:22
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Apr 10, 2026
ndoschek added a commit to eclipse-theia/theia-ide that referenced this pull request Apr 10, 2026
For more extensive testing we enable the token UI in the
chat view by default for the Theia IDE next product
See also eclipse-theia/theia#17028
@github-actions github-actions Bot added this to the 1.71.0 milestone Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Show per-session token usage indicator in the chat view

4 participants