fix(cli): emit cumulative token usage in stream-json/json complete event#8870
Open
namanvats-dev wants to merge 3 commits intoaaif-goose:mainfrom
Open
fix(cli): emit cumulative token usage in stream-json/json complete event#8870namanvats-dev wants to merge 3 commits intoaaif-goose:mainfrom
namanvats-dev wants to merge 3 commits intoaaif-goose:mainfrom
Conversation
The `complete` event in `goose run --output-format stream-json` (and `metadata.total_tokens` in `--output-format json`) was reporting `Session::total_tokens`, which is the *last turn's* context size — it gets overwritten on every LLM round-trip and reset to the summary output count after compaction (see `update_session_metrics` in `crates/goose/src/agents/reply_parts.rs`). Downstream tools (eval harnesses, billing, dashboards) interpret `total_tokens` as the cumulative session cost, so they were under- reporting actual usage by an order of magnitude on multi-turn runs. Switch to `Session::accumulated_total_tokens` (sum across all turns), falling back to `total_tokens` only when the accumulator is unset. Also surface `input_tokens` / `output_tokens` so consumers can attribute prompt vs. completion cost without re-deriving them. Backwards compatible: `total_tokens` keeps its name and position; the two new fields are `Option<i32>` and skipped when None.
Collapse the JsonMetadata field expressions onto single lines per rustfmt's preferred layout.
Author
|
Closes #8871 |
angiejones
requested changes
Apr 28, 2026
Collaborator
angiejones
left a comment
There was a problem hiding this comment.
please remove comments
Author
|
@angiejones removed the comment, can you please check once |
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.
Summary
The
completeevent ingoose run --output-format stream-json(andmetadata.total_tokensin--output-format json) reportsSession::total_tokens, which is the last turn's context size, it gets overwritten every LLM round-trip and reset to the summary output count after compaction (seeupdate_session_metricsincrates/goose/src/agents/reply_parts.rs).Downstream tools (eval harnesses, billing dashboards) interpret
total_tokensas the cumulative session cost, so they under-report by an order of magnitude on multi-turn runs. We hit this in our eval harness where a session that streamed thousands of chunks reportedtotal_tokens=17099, actually just the final-turn context.Fix: read
Session::accumulated_total_tokens(the true running sum), withtotal_tokensas a fallback for legacy/empty sessions. Also surfaceinput_tokens/output_tokensso consumers can split prompt vs. completion cost without re-deriving them.Backwards compatible:
total_tokenskeeps its name and position; existing consumers see a correct number where they previously saw an under-count. The two new fields areOption<i32>withskip_serializing_if = "Option::is_none"— omitted when unset, so no parser breakage.Files touched:
crates/goose-cli/src/session/mod.rs(JsonMetadata,StreamEvent::Complete, both emit sites).Testing
cargo check -p goose-cli: clean (verified locally, ~3 min build)cargo test -p goose-cli: please run in CIgoose run --output-format stream-jsonagainst our eval harness and confirmed the emittedtotal_tokensnow matches the cumulative provider-reported usage rather than the last-turn context sizeRelated Issues
N/A : surfaced internally while debugging token under-reporting in an external eval harness consuming
--output-format stream-json. Happy to file a tracking issue if preferred.Screenshots/Demos (for UX changes)
N/A : backend change, no UX surface.