Skip to content

fix(gemini): exclude tool-execution tokens from output_tokens - #2406

Open
iluv7 wants to merge 2 commits into
agentscope-ai:mainfrom
iluv7:fix/gemini-output-tokens
Open

fix(gemini): exclude tool-execution tokens from output_tokens#2406
iluv7 wants to merge 2 commits into
agentscope-ai:mainfrom
iluv7:fix/gemini-output-tokens

Conversation

@iluv7

@iluv7 iluv7 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

AgentScope Version

2.0.6

Description

Gemini reports prompt, candidate, server-side tool-use, and thinking tokens separately in usage_metadata. GeminiChatModel previously derived output_tokens as total_token_count - prompt_token_count, which incorrectly classified tool_use_prompt_token_count as model output. These tokens come from server-side tool results (for example, Google Search grounding or code execution) that are fed back to the model as input.

This caused incorrect input/output usage reporting and distorted weighted cost or budget accounting such as ReplyBudgetControlMiddleware.

Fixes #2405

Changes:

  • Classify tool-use prompt tokens as input: input_tokens = prompt_token_count + tool_use_prompt_token_count
  • Count only model-generated candidate and thinking tokens as output: output_tokens = candidates_token_count + thoughts_token_count
  • When candidates_token_count is unavailable, fall back to total_token_count - input_tokens, so tool-use tokens are still excluded from output
  • Add regression coverage for non-streaming, streaming, thinking-token, and missing-candidate-count paths in tests/model_gemini_test.py

For example, given prompt=500, candidates=120, tool_use=300, thoughts=10, and total=930, the model now reports:

input_tokens = 800
output_tokens = 130

The two values preserve the API-reported total while correctly classifying tool-use results as input rather than output.

How to test:

pytest tests/model_gemini_test.py

Checklist

…cope-ai#2405)

Gemini's total_token_count covers prompt, candidates, tool-use prompt,
and thinking tokens. Deriving output_tokens as total - prompt counted
the tool-execution results fed back to the model as output, inflating
usage reported for server-side tools like Google Search grounding and
budget accounting in ReplyBudgetControlMiddleware. Count only the
tokens the model generated: candidates_token_count plus
thoughts_token_count, falling back to total - prompt on older SDKs.

Co-Authored-By: Claude <noreply@anthropic.com>
@iluv7 iluv7 changed the title fix(gemini): exclude tool-execution tokens from output_tokens (#2405) fix(gemini): exclude tool-execution tokens from output_tokens Aug 24, 2026
@iluv7
iluv7 marked this pull request as draft August 24, 2026 02:17
@DavdGao
DavdGao requested a review from qbc2016 August 24, 2026 06:23
@DavdGao

DavdGao commented Aug 24, 2026

Copy link
Copy Markdown
Member

@iluv7 Is this pr ready for review?

@iluv7
iluv7 marked this pull request as ready for review August 24, 2026 08:08
@iluv7

iluv7 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

@iluv7 Is this pr ready for review?

Yep.

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.

[Bug]: GeminiChatModel reports inflated output_tokens when a response includes tool-execution usage

2 participants