From d749997a51f935e6a3c04294e36dad53bd2f1ee7 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Wed, 18 Nov 2026 06:56:21 -0400 Subject: [PATCH] fix(google-ai): bill Gemini context-cache hits at the cached input rate 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 --- .../Providers/Chat/ChatCompletionProvider.cs | 9 +++++--- .../Providers/ProviderHelper.cs | 18 ++++++++++++++++ .../Providers/Text/TextCompletionProvider.cs | 3 ++- .../Plugins/GoogleAiUsageTests.cs | 21 +++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs index 4843f7105..216549896 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs @@ -126,7 +126,8 @@ public async Task GetChatCompletions(Agent agent, List GetChatCompletionsAsync(Agent agent, List GetChatCompletionsStreamingAsync(Agent agent, Prompt = prompt, Provider = Provider, Model = _model, - TextInputTokens = tokenUsage?.PromptTokenCount ?? 0, + TextInputTokens = ProviderHelper.GetTextInputTokens(tokenUsage), + CachedTextInputTokens = ProviderHelper.GetCachedInputTokens(tokenUsage), TextOutputTokens = ProviderHelper.GetOutputTokens(tokenUsage) }); } diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs index 91d38ae09..255e27df5 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs @@ -26,4 +26,22 @@ public static long GetOutputTokens(GenerativeAI.Types.UsageMetadata? usage) { return (usage?.CandidatesTokenCount ?? 0) + (usage?.ThoughtsTokenCount ?? 0); } + + /// + /// Tokens served from the implicit/explicit context cache. Gemini includes + /// them in PromptTokenCount, so they are billed at the cached rate. + /// + public static long GetCachedInputTokens(GenerativeAI.Types.UsageMetadata? usage) + { + return usage?.CachedContentTokenCount ?? 0; + } + + /// + /// Prompt tokens billed at the full input price (prompt minus cache hits). + /// + public static long GetTextInputTokens(GenerativeAI.Types.UsageMetadata? usage) + { + var uncached = (usage?.PromptTokenCount ?? 0) - GetCachedInputTokens(usage); + return uncached > 0 ? uncached : 0; + } } diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/TextCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/TextCompletionProvider.cs index 6e5c267dc..fbae0eb6b 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/TextCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Text/TextCompletionProvider.cs @@ -62,7 +62,8 @@ public async Task GetCompletion(string text, string agentId, string mess Prompt = text, Provider = Provider, Model = _model, - TextInputTokens = response?.UsageMetadata?.PromptTokenCount ?? 0, + TextInputTokens = ProviderHelper.GetTextInputTokens(response?.UsageMetadata), + CachedTextInputTokens = ProviderHelper.GetCachedInputTokens(response?.UsageMetadata), TextOutputTokens = ProviderHelper.GetOutputTokens(response?.UsageMetadata) }); } diff --git a/tests/BotSharp.Core.UnitTests/Plugins/GoogleAiUsageTests.cs b/tests/BotSharp.Core.UnitTests/Plugins/GoogleAiUsageTests.cs index c6142996d..99035eb96 100644 --- a/tests/BotSharp.Core.UnitTests/Plugins/GoogleAiUsageTests.cs +++ b/tests/BotSharp.Core.UnitTests/Plugins/GoogleAiUsageTests.cs @@ -20,4 +20,25 @@ public void Output_tokens_without_thinking_are_the_candidates_count() Assert.Equal(20, ProviderHelper.GetOutputTokens(new UsageMetadata { CandidatesTokenCount = 20 })); Assert.Equal(0, ProviderHelper.GetOutputTokens(null)); } + + [Fact] + public void Cached_tokens_are_read_from_usage_metadata() + { + var usage = new UsageMetadata { PromptTokenCount = 1000, CachedContentTokenCount = 800 }; + + Assert.Equal(800, ProviderHelper.GetCachedInputTokens(usage)); + Assert.Equal(0, ProviderHelper.GetCachedInputTokens(new UsageMetadata { PromptTokenCount = 10 })); + Assert.Equal(0, ProviderHelper.GetCachedInputTokens(null)); + } + + [Fact] + public void Text_input_tokens_exclude_cached_tokens() + { + // Gemini's PromptTokenCount already includes the cached part. + var usage = new UsageMetadata { PromptTokenCount = 1000, CachedContentTokenCount = 800 }; + + Assert.Equal(200, ProviderHelper.GetTextInputTokens(usage)); + Assert.Equal(10, ProviderHelper.GetTextInputTokens(new UsageMetadata { PromptTokenCount = 10 })); + Assert.Equal(0, ProviderHelper.GetTextInputTokens(null)); + } }