8000 fix(ai): count Gemini toolUsePromptTokenCount as input tokens by pcbeingused333 · Pull Request #4066 · fosrl/pangolin · GitHub
[go: up one dir, main page]

Skip to content

fix(ai): count Gemini toolUsePromptTokenCount as input tokens - #4066

Merged
oschwartz10612 merged 1 commit into
fosrl:mainfrom
pcbeingused333:fix/gemini-tooluse-prompt-tokens
Nov 6, 2026
Merged

oschwartz10612 merged 1 commit into
fosrl:mainfrom
pcbeingused333:fix/gemini-tooluse-prompt-tokens

Conversation

@pcbeingused333
Copy link
Copy Markdown
Contributor

Community Contribution License Agreement

By creating this pull request, I grant the project maintainers an unlimited,
perpetual license to use, modify, and redistribute these contributions under any terms they
choose, including both the AGPLv3 and the Fossorial Commercial license terms. I
represent that I have the right to grant this license for all contributed content.

AI Disclosure

This change was prepared with AI assistance (Claude Code): code, test and description. It was reviewed before submission.

Description

Gemini reports the tokens of tool results (Google Search grounding, URL context, code execution) in usageMetadata.toolUsePromptTokenCount, separately from promptTokenCount (totalTokenCount = promptTokenCount + toolUsePromptTokenCount + candidatesTokenCount + thoughtsTokenCount). These tokens are billed as input. extractGoogleGenerateContent never read that field, so for Gemini requests that use tools the AI gateway under-counted input tokens, and with them the cost and budget figures.

The fix scans toolUsePromptTokenCount and adds it to promptTokens. It is not part of cachedContentTokenCount, so the cache subtraction is unchanged.

How to test?

npx tsx server/lib/aiUsageExtraction.test.ts: the new testGeminiToolUsePromptTokens fails on main (promptTokens 80 instead of 120) and passes with this change, along with the existing Bedrock tests.

Gemini reports the tokens of tool results (Google Search, URL context) in usageMetadata.toolUsePromptTokenCount, separate from promptTokenCount. The extractor ignored it, so usage and cost were under-counted.
@oschwartz10612
Copy link
Copy Markdown
Member

Thanks!

@oschwartz10612
oschwartz10612 merged commit 020c227 into fosrl:main Nov 6, 2026
3 checks passed
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.

2 participants

0