fix(cli): report cumulative total_tokens in stream-json/json output#8908
Closed
bzqzheng wants to merge 1 commit intoaaif-goose:mainfrom
Closed
fix(cli): report cumulative total_tokens in stream-json/json output#8908bzqzheng wants to merge 1 commit intoaaif-goose:mainfrom
bzqzheng wants to merge 1 commit intoaaif-goose:mainfrom
Conversation
The CLI's --json and --stream-json output modes were reading session.total_tokens, which stores only the last turn's token usage. This caused total_tokens to reset to the current turn's count on every chunk, rather than reporting the running session total. Fix by reading session.accumulated_total_tokens, which is already correctly maintained by update_session_metrics across all turns. Also update get_total_token_usage() to return the accumulated value for consistency. Test: add test_accumulated_total_tokens_across_multiple_turns asserting that accumulated_total_tokens grows cumulatively across turns while total_tokens remains per-turn. Fixes aaif-goose#8871 Signed-off-by: Trinity <trinity@multica.ai> Signed-off-by: Bright Zheng <bzqzheng@gmail.com>
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.
Problem
The CLI's "--json" and "--stream-json" output modes were reading
session.total_tokens, which stores only the last turn's token usage. This causedcomplete.total_tokensto reset to the current turn's count on every chunk, rather than reporting the running session total (fixes #8871).Root Cause
update_session_metricsincrates/goose/src/agents/reply_parts.rscorrectly computesaccumulated_total_tokensby adding usage across turns.total_tokensis set to the current turn's usage.crates/goose-cli/src/session/mod.rsreadssession.total_tokensinstead ofsession.accumulated_total_tokens.Fix
Read
session.accumulated_total_tokensin:JsonMetadataconstruction (--jsonmode)StreamEvent::Completeemission (--stream-jsonmode)get_total_token_usage()for consistencyTest
Added
test_accumulated_total_tokens_across_multiple_turnswhich:accumulated_total_tokens == 45(cumulative)total_tokens == 15(last turn only)Verification