Repository navigation
fix(google-ai): bill Gemini context-cache hits at the cached input rate - #1502
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
Gemini's PromptTokenCount includes the tokens served from the context cache, and UsageMetadata.CachedContentTokenCount was never read. All prompt tokens were therefore reported as TextInputTokens and billed at the full input price, so CachedTextInputCost had no effect. Report CachedContentTokenCount as CachedTextInputTokens and subtract it from TextInputTokens in the chat, streaming and text providers. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Contributor
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
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.
No linked issue (sibling of the Gemini thinking-token fix: found by checking which
UsageMetadatafields the Google AI provider reads for cost stats).Gemini's
UsageMetadata.PromptTokenCountalready includes the tokens served from the context cache (implicit or explicit), andCachedContentTokenCountreports that part. The provider only readPromptTokenCount, so every prompt token was reported asTextInputTokensand billed atTextInputCost;CachedTextInputCostin the model settings never applied to Gemini, which overstates the cost whenever implicit caching hits.Fix:
ProviderHelper.GetCachedInputTokensreturnsCachedContentTokenCount, andGetTextInputTokensreturnsPromptTokenCountminus the cached part (never negative). The chat, async chat, streaming and text completion providers now fillTextInputTokensandCachedTextInputTokensfrom them, the same split the OpenAI provider already uses.Test: two new cases in
GoogleAiUsageTests(cached tokens read from usage; text input excludes cached tokens). With the old behaviour they fail (Expected: 200, Actual: 1000andExpected: 800, Actual: 0).This change was prepared with the assistance of an AI coding tool (Claude Code); the author is responsible for the submission.