Skip to content

fix(google-ai): bill Gemini cached prompt tokens separately - #1496

Open
pcbeingused333 wants to merge 1 commit into
SciSharp:masterfrom
pcbeingused333:fix/google-cached-tokens
Open

pcbeingused333 wants to merge 1 commit into
SciSharp:masterfrom
pcbeingused333:fix/google-cached-tokens

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

No linked issue (sibling of #1486: same usage-accounting gap, here for cached input instead of thinking tokens).

Gemini's UsageMetadata.PromptTokenCount includes the tokens served from the context cache (CachedContentTokenCount). The Google AI provider reported the whole prompt count as TextInputTokens and never set CachedTextInputTokens, so cached tokens were charged at the full input price in TokenStatistics. The OpenAI, Azure OpenAI, DeepSeek and Anthropic providers already split the two.

Fix: ProviderHelper.GetInputTokens (prompt count minus cached count, floored at 0) and GetCachedInputTokens, used by the three usage sites in the chat provider and the text completion provider.

Test: GoogleAiUsageTests covers the split, the no-cache case and null usage. They fail on main (the helpers do not exist; the old code reported 1000 fresh input tokens and 0 cached for a 1000/800 usage).

This change was prepared with the assistance of an AI coding tool (Claude Code); the author is responsible for the submission.

Gemini's PromptTokenCount includes tokens served from the context cache. They were reported entirely as fresh input and CachedTextInputTokens was never set, so cached tokens were charged at the full input price.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

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