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)); + } }