Skip to content

fix(status-bar): count tool calls live and parse per-step token_usage - #306

Merged
edenreich merged 2 commits into
mainfrom
fix/issue-305
Sep 20, 2026
Merged

edenreich merged 2 commits into
mainfrom
fix/issue-305

Conversation

@inference-gateway-maintainer

Copy link
Copy Markdown
Contributor

Resolves #305

Summary

The status bar usage line only updated when a run finished because AgentEvent::TokenUsage was emitted from exactly one place - the terminal RUN_FINISHED stats 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 counting historyUsage already does for loaded history) and adds a token_usage CUSTOM-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/cli emits per-step usage in the infer headless stream; the parser arm and reducer already handle it when it lands.

Changes

  • frontend/lib/transcript.ts: the reducer increments usage.total_tool_calls as live tool items are created - one increment per call on the TOOL_CALL_END path (AssistantMessage.tool_calls), plus one when a tool result arrives with no matching streamed call (the uncorrelated fallback). The authoritative RUN_FINISHED figure still overwrites the running count when it lands.
  • backend/src/agent.rs: factored the RUN_FINISHED stats extraction into a shared usage_from_stats helper and added a CUSTOM name token_usage arm that maps the same camelCase stats object (inputTokens, outputTokens, cacheReadTokens, totalToolCalls, lastInputTokens, contextWindow, cost) onto TokenUsage mid-run. A token_usage event without an object value falls through to RawLine so it cannot wipe the previous run's numbers with zeros.
  • Tests: parser tests for the per-step token_usage CUSTOM event (full payload and missing value), and reducer tests for the live tool-call count with the RUN_FINISHED overwrite plus the uncorrelated-result fallback.
  • Cross-repo note: live in / out / cached / Context % / cost require inference-gateway/cli to emit per-step usage (naturally as a CUSTOM event carrying the stats object, as established by agent_status and background_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.

@inference-gateway-maintainer
inference-gateway-maintainer Bot marked this pull request as ready for review September 20, 2026 10:32
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>
@edenreich
edenreich merged commit 1738b25 into main Sep 20, 2026
1 check passed
@edenreich
edenreich deleted the fix/issue-305 branch September 20, 2026 11:27
@inference-gateway-maintainer

Copy link
Copy Markdown
Contributor Author

🎉 This PR is included in version 0.46.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@inference-gateway-maintainer inference-gateway-maintainer Bot added the released Shipped in a release label Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Status bar token and tool-call stats only update when the run finishes

1 participant