Skip to content

fix(google): count tool-use prompt tokens as input tokens - #485

Open
pcbeingused333 wants to merge 1 commit into
posit-dev:mainfrom
pcbeingused333:fix/google-tool-use-prompt-tokens
Open

pcbeingused333 wants to merge 1 commit into
posit-dev:mainfrom
pcbeingused333:fix/google-tool-use-prompt-tokens

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

GoogleProvider.value_tokens() reported prompt_token_count (minus cached) as the input tokens and ignored tool_use_prompt_token_count. Gemini reports the tokens of content fetched by built-in tools (URL context, search, ...) there, separately from the prompt, and Google's URL context documentation says that content "is counted as part of the input tokens". A turn that fetched a large page therefore under-reported its input tokens and cost: with prompt_token_count=27, tool_use_prompt_token_count=10309, candidates_token_count=40 and thoughts_token_count=200, the turn tokens were (27, 240, 0).

value_tokens() now adds tool_use_prompt_token_count to the input tokens, giving (10336, 240, 0) for that response. Responses without tool-use tokens are unchanged.

Tests: test_google_value_tokens_counts_tool_use_prompt_tokens_as_input uses a real GenerateContentResponseUsageMetadata and fails on main; test_google_value_tokens_without_tool_use_prompt_tokens checks the cached-token case is unchanged. CHANGELOG entry added.

This change was made with Claude Code.

🤖 Generated with Claude Code

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant