Repository navigation
fix(status-bar): count tool calls live and parse per-step token_usage - #306
Merged
Merged
Conversation
inference-gateway-maintainer
Bot
marked this pull request as ready for review
September 20, 2026 10:32
This was referenced Sep 20, 2026
edenreich
pushed a commit
to inference-gateway/cli
that referenced
this pull request
Sep 20, 2026
…1254) Resolves #1253 ## Summary `infer headless --format ag-ui` now emits a `CUSTOM` event named `token_usage` after every LLM step, carrying the same cumulative session stats object that the terminal `RUN_FINISHED` result uses. Clients rendering live usage (the desktop status bar's `in`, `out`, `cached`, `Context %`, `cost`) stay current as the run progresses instead of jumping once at the end. The desktop parser side is already prepared in inference-gateway/desktop#306 and stays inert until this emission lands in a release. ## Changes - `internal/platform/render/agui.go`: new `emitTokenUsage` custom event on `aguiEncoder`, mirroring the established `agent_status` / `background_tasks` / `queued_message` extension points; value is exactly the `{"type":"CUSTOM","name":"token_usage","value":{...}}` shape the desktop contract expects. - `internal/platform/render/render.go`: `RenderAGUI` publishes the event on each `ChatCompleteEvent` (after the step's message/tool-call framing, before tool execution continues) by reusing `sessionResult(model, repo)` - the same builder that feeds `RUN_FINISHED` - so per-step and terminal values can never drift. Suppressed when the session has no recorded LLM requests. Verified the agent records usage (`AddTokenUsage`) and assistant tool calls in the repo before the completion event publishes, so per-step snapshots are current. - `internal/platform/render/render_test.go`: `TestRenderAGUI_TokenUsageStreamsPerStep` - one event per step, all seven value fields checked (including `contextWindow`), and no event for zero-request runs. - `docs/ag-ui-output.md`: documents the per-step event in the event-mapping table and the `RUN_FINISHED` result section. - `RenderAGUI` gained one branch and crossed the cyclop cap; nolint-ed per the repo convention for cohesive event switches instead of splitting. Docs: repo-local contract page (`docs/ag-ui-output.md`) updated in this PR; org docs follow-up tracked in inference-gateway/docs#700. Co-authored-by: inference-gateway-maintainer[bot] <246577062+inference-gateway-maintainer[bot]@users.noreply.github.com>
Contributor
Author
|
🎉 This PR is included in version 0.46.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
Resolves #305
Summary
The status bar usage line only updated when a run finished because
AgentEvent::TokenUsagewas emitted from exactly one place - the terminalRUN_FINISHEDstats in the NDJSON parser - so the reducer had nothing to reduce mid-run. This change counts tool calls live in the transcript reducer (the same client-side countinghistoryUsagealready does for loaded history) and adds atoken_usageCUSTOM-event arm to the agent parser so per-step token/cost usage is picked up as soon as the CLI emits it.The tool-call half is desktop-only and works with the current CLI. The token/cost half is desktop-ready but inert until
inference-gateway/cliemits per-step usage in theinfer headlessstream; the parser arm and reducer already handle it when it lands.Changes
frontend/lib/transcript.ts: the reducer incrementsusage.total_tool_callsas live tool items are created - one increment per call on theTOOL_CALL_ENDpath (AssistantMessage.tool_calls), plus one when a tool result arrives with no matching streamed call (the uncorrelated fallback). The authoritativeRUN_FINISHEDfigure still overwrites the running count when it lands.backend/src/agent.rs: factored theRUN_FINISHEDstats extraction into a sharedusage_from_statshelper and added aCUSTOMnametoken_usagearm that maps the same camelCase stats object (inputTokens,outputTokens,cacheReadTokens,totalToolCalls,lastInputTokens,contextWindow,cost) ontoTokenUsagemid-run. Atoken_usageevent without an object value falls through toRawLineso it cannot wipe the previous run's numbers with zeros.token_usageCUSTOM event (full payload and missing value), and reducer tests for the live tool-call count with theRUN_FINISHEDoverwrite plus the uncorrelated-result fallback.in/out/cached/Context %/costrequireinference-gateway/clito emit per-step usage (naturally as a CUSTOM event carrying the stats object, as established byagent_statusandbackground_tasks); no desktop changes will be needed to light it up.Verification
cargo fmt --check,cargo clippy -p inference-gateway-desktop -- -D warnings,cargo test(167 pass),bun run build(tsc + vite),bun test(170 pass) - all green locally; the pre-commit hook ran the same suite on commit.