[codex] Stream live token usage into desktop context meter - #39201
[codex] Stream live token usage into desktop context meter#39201OmarB97 wants to merge 4 commits into
Conversation
…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.
Review: approve with suggestionsReviewed the diff for live token usage streaming into the desktop/TUI context meter. The data flow is complete and correct: backend Verified
Non-blocking suggestions
DecisionApprove — no blocker-severity findings. The PR achieves the stated goal of streaming live context usage into the desktop/TUI meter. |
|
Superseded by #39370, which carries this slice (gateway emission, preflight context estimate, desktop + TUI consumption) rebased on current main. Two micro-slices not carried there (scripts/release.py author-email map; execFileNoThrow.ts SpawnOptions stdio typing fix) — will rehome in a future small PR if still wanted. Closing to reduce reviewer surface. |
Summary
token.usageeventstoken.usagesnapshot before each API call using the already-computed request-size estimate, so providers that only report usage at completion still move the desktop context meter during the active turnmessage.completeui-tuitype-checks cleanlyWhy
The first live-usage patch handled final/canonical provider usage, but the official macOS app could still sit at
0/131.1kwhile a turn was running because many providers expose usage only at completion. The new preflight snapshot sends the current request/context estimate at call start, then the final provider usage corrects totals afterward.Fork / install follow-up
mainatc0c1bcd093eb10249cfd972e9f4146452c4534d6with a clean install stamp.Verification
python3 -m pytest tests/tui_gateway/test_token_usage_events.pypython3 -m pytest tests/run_agent/test_run_agent.py::TestRunConversation::test_preflight_token_usage_emits_before_api_response tests/run_agent/test_run_agent.py::TestRunConversation::test_stop_finish_reason_returns_responsepython3 -m py_compile agent/conversation_loop.pynpx vitest run --environment jsdom src/lib/token-usage.test.ts src/app/session/hooks/use-message-stream.test.tsxfromapps/desktopnpm run type-check --workspace ui-tuitoken.usagearrived at ~2.1s andmessage.completearrived at ~11.9s for the same turn, proving the desktop can receive usage before end-of-turn.