Repository navigation
feat(render): stream per-step token usage in ag-ui headless output - #1254
Merged
Merged
Conversation
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
Closed
3 tasks
inference-gateway-maintainer
Bot
marked this pull request as ready for review
September 20, 2026 11:06
inference-gateway-maintainer
Bot
requested a review
from a team
as a code owner
September 20, 2026 11:06
This was referenced Sep 20, 2026
Contributor
|
🎉 This PR is included in version 0.205.0 🎉 The release is available on: 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 #1253
Summary
infer headless --format ag-uinow emits aCUSTOMevent namedtoken_usageafter every LLM step, carrying the same cumulative session stats object that the terminalRUN_FINISHEDresult uses. Clients rendering live usage (the desktop status bar'sin,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: newemitTokenUsagecustom event onaguiEncoder, mirroring the establishedagent_status/background_tasks/queued_messageextension points; value is exactly the{"type":"CUSTOM","name":"token_usage","value":{...}}shape the desktop contract expects.internal/platform/render/render.go:RenderAGUIpublishes the event on eachChatCompleteEvent(after the step's message/tool-call framing, before tool execution continues) by reusingsessionResult(model, repo)- the same builder that feedsRUN_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 (includingcontextWindow), and no event for zero-request runs.docs/ag-ui-output.md: documents the per-step event in the event-mapping table and theRUN_FINISHEDresult section.RenderAGUIgained 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.