Repository navigation
fix: count Gemini toolUsePromptTokenCount as input tokens - #904
Merged
chenyme merged 2 commits intoOct 31, 2026
Merged
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
pcbeingused333
force-pushed
the
fix/gemini-tool-use-prompt-tokens
branch
from
October 29, 2026 02:20
0e05f78 to
12c5308
Compare
chenyme
self-requested a review
October 31, 2026 08:52
chenyme
approved these changes
Oct 31, 2026
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.
With Gemini's built-in tools (
url_context,google_search),usageMetadata.toolUsePromptTokenCountreports the tokens of the tool results that are fed back to the model. They are separate frompromptTokenCountand are part oftotalTokenCount(for example 31 prompt + 73 candidates + 77 thought + 73 tool-use = 254), butparseGeminiUsageignored them, so the input tokens came out too low, and so did the cost derived from them (Google's pricing page bills URL context "as input tokens per model pricing").parseGeminiUsagenow addstoolUsePromptTokenCounttopromptTokenCountbefore it subtracts the cached tokens. The tool-use tokens are not part of the cached prefix, so they stay in the non-cached input. A response without the field gives the same result as before. The header comment of the file lists the field.Tests in
usage_test.gocover 31 prompt tokens plus 73 tool-use tokens giving 104 input tokens (it fails before the change), and a response with cached tokens and tool-use tokens (100 prompt, 40 cached, 25 tool-use gives 85 non-cached input and 40 cache reads).go test ./internal/infra/llm/ -run 'Usage|NonCached'passes, andgofmtandgo vetare clean for the package. The rest of the suite and live requests were not run.This change was made with Claude Code.
🤖 Generated with Claude Code