Skip to content

feat(render): stream per-step token usage in ag-ui headless output - #1254

Merged
edenreich merged 1 commit into
mainfrom
fix/issue-1253
Sep 20, 2026
Merged

edenreich merged 1 commit into
mainfrom
fix/issue-1253

Conversation

@inference-gateway-maintainer

@inference-gateway-maintainer inference-gateway-maintainer Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

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.

emit a token_usage custom event after every llm step, reusing the sessionresult builder that feeds the run_finished result so live usage consumers (desktop status bar) track cumulative stats mid-run

resolves #1253
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.205.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser 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.

Headless: emit per-step token usage in the AG-UI stream (desktop status bar)

1 participant