Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
Prompt = prompt,
Provider = Provider,
Model = _model,
TextInputTokens = response?.UsageMetadata?.PromptTokenCount ?? 0,
TextInputTokens = ProviderHelper.GetTextInputTokens(response?.UsageMetadata),
CachedTextInputTokens = ProviderHelper.GetCachedInputTokens(response?.UsageMetadata),
TextOutputTokens = ProviderHelper.GetOutputTokens(response?.UsageMetadata)
});
}
Expand Down Expand Up @@ -183,7 +184,8 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogMode
Prompt = prompt,
Provider = Provider,
Model = _model,
TextInputTokens = response?.UsageMetadata?.PromptTokenCount ?? 0,
TextInputTokens = ProviderHelper.GetTextInputTokens(response?.UsageMetadata),
CachedTextInputTokens = ProviderHelper.GetCachedInputTokens(response?.UsageMetadata),
TextOutputTokens = ProviderHelper.GetOutputTokens(response?.UsageMetadata)
});
}
Expand Down Expand Up @@ -446,7 +448,8 @@ public async Task<RoleDialogModel> 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)
});
}
Expand Down
18 changes: 18 additions & 0 deletions src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,22 @@ public static long GetOutputTokens(GenerativeAI.Types.UsageMetadata? usage)
{
return (usage?.CandidatesTokenCount ?? 0) + (usage?.ThoughtsTokenCount ?? 0);
}

/// <summary>
/// Tokens served from the implicit/explicit context cache. Gemini includes
/// them in <c>PromptTokenCount</c>, so they are billed at the cached rate.
/// </summary>
public static long GetCachedInputTokens(GenerativeAI.Types.UsageMetadata? usage)
{
return usage?.CachedContentTokenCount ?? 0;
}

/// <summary>
/// Prompt tokens billed at the full input price (prompt minus cache hits).
/// </summary>
public static long GetTextInputTokens(GenerativeAI.Types.UsageMetadata? usage)
{
var uncached = (usage?.PromptTokenCount ?? 0) - GetCachedInputTokens(usage);
return uncached > 0 ? uncached : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public async Task<string> 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)
});
}
Expand Down
21 changes: 21 additions & 0 deletions tests/BotSharp.Core.UnitTests/Plugins/GoogleAiUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading