Skip to content

fix(cli): report cumulative total_tokens in stream-json/json output#8908

Closed
bzqzheng wants to merge 1 commit intoaaif-goose:mainfrom
bzqzheng:fix/cumulative-token-reporting
Closed

fix(cli): report cumulative total_tokens in stream-json/json output#8908
bzqzheng wants to merge 1 commit intoaaif-goose:mainfrom
bzqzheng:fix/cumulative-token-reporting

Conversation

@bzqzheng
Copy link
Copy Markdown
Contributor

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 caused complete.total_tokens to reset to the current turn's count on every chunk, rather than reporting the running session total (fixes #8871).

Root Cause

  • update_session_metrics in crates/goose/src/agents/reply_parts.rs correctly computes accumulated_total_tokens by adding usage across turns.
  • However, total_tokens is set to the current turn's usage.
  • The CLI output code in crates/goose-cli/src/session/mod.rs reads session.total_tokens instead of session.accumulated_total_tokens.

Fix

Read session.accumulated_total_tokens in:

  • JsonMetadata construction (--json mode)
  • StreamEvent::Complete emission (--stream-json mode)
  • get_total_token_usage() for consistency

Test

Added test_accumulated_total_tokens_across_multiple_turns which:

  1. Creates a mock provider reporting 15 tokens per turn
  2. Runs 3 reply turns
  3. Asserts accumulated_total_tokens == 45 (cumulative)
  4. Asserts total_tokens == 15 (last turn only)

Verification

cargo test --package goose test_accumulated_total_tokens_across_multiple_turns
cargo test --package goose-cli

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>
@bzqzheng bzqzheng closed this Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

# stream-json/json complete.total_tokens reports last-turn context, not cumulative session usage

1 participant