feat(gateway): emit token_usage events after each API call for real-time context bar - #38822
Closed
OmarB97 wants to merge 2 commits into
Closed
feat(gateway): emit token_usage events after each API call for real-time context bar#38822OmarB97 wants to merge 2 commits into
OmarB97 wants to merge 2 commits into
Conversation
added 2 commits
May 31, 2026 12:54
…ime context bar
After every API call that updates session token counters, emit a structured
token.usage event through the existing status_callback → _status_update →
WebSocket event pipe. This gives the desktop app's context bar component
(@nous-research/ui) the raw material to animate smoothly during a turn
instead of jumping to the final value at end-of-turn.
Changes:
- run_agent.py: add _emit_token_usage() method that JSON-encodes
token counts and context dimensions, then fires via status_callback
- conversation_loop.py: call _emit_token_usage() after session_*_tokens +=
- tui_gateway/server.py: handle kind=token_usage in _status_update by
parsing the JSON payload and re-emitting as a typed 'token.usage' event
Token.usage payload:
{ input_tokens, output_tokens, total_tokens, context_tokens,
context_length, context_pct }
The frontend side (@nous-research/ui npm package) requires a separate
upstream change to subscribe to 'token.usage' events for the context bar.
Contributor
Author
14 tasks
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.
Problem
The desktop app's context window progress bar only updates at the end of a turn, causing a jarring jump. Token count data is only sent to the UI via session snapshots that fire after the turn completes.
Solution
After every API call that updates session token counters in
conversation_loop.py, emit a structuredtoken.usageevent through the existingstatus_callback→_status_update→ WebSocket event pipe. The desktop app's@nous-research/uicontext bar component can then subscribe totoken.usageevents and update smoothly during a turn.Changes
run_agent.py_emit_token_usage()method — JSON-encodes token counts + context dimensions, fires viastatus_callbackagent/conversation_loop.py_emit_token_usage()after session token counters are updatedtui_gateway/server.pykind=token_usagein_status_update— parse JSON payload, re-emit as typedtoken.usageeventEvent Payload
{ "input_tokens": 12345, "output_tokens": 678, "total_tokens": 13023, "context_tokens": 54321, "context_length": 131072, "context_pct": 41.5 }Frontend Note
The
@nous-research/uinpm package (v0.18.2) renders the context bar component. A separate change in that package is needed to subscribe totoken.usageevents. This PR provides the backend event pipe — the frontend half is independent.Testing
status_callback— never blocks the conversation loopstatus_callbackis wired, the event is silently dropped