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.GetInputTokens(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.GetInputTokens(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.GetInputTokens(tokenUsage),
CachedTextInputTokens = ProviderHelper.GetCachedInputTokens(tokenUsage),
TextOutputTokens = ProviderHelper.GetOutputTokens(tokenUsage)
});
}
Expand Down
14 changes: 14 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,18 @@ public static long GetOutputTokens(GenerativeAI.Types.UsageMetadata? usage)
{
return (usage?.CandidatesTokenCount ?? 0) + (usage?.ThoughtsTokenCount ?? 0);
}

/// <summary>
/// Gemini's prompt count includes the tokens served from the context cache,
/// which are billed at a separate rate, so they are subtracted here.
/// </summary>
public static long GetInputTokens(GenerativeAI.Types.UsageMetadata? usage)
{
return Math.Max((usage?.PromptTokenCount ?? 0) - GetCachedInputTokens(usage), 0);
}

public static long GetCachedInputTokens(GenerativeAI.Types.UsageMetadata? usage)
{
return usage?.CachedContentTokenCount ?? 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.GetInputTokens(response?.UsageMetadata),
CachedTextInputTokens = ProviderHelper.GetCachedInputTokens(response?.UsageMetadata),
TextOutputTokens = ProviderHelper.GetOutputTokens(response?.UsageMetadata)
});
}
Expand Down
18 changes: 18 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,22 @@ 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_not_counted_as_fresh_input()
{
var usage = new UsageMetadata { PromptTokenCount = 1000, CachedContentTokenCount = 800 };

Assert.Equal(200, ProviderHelper.GetInputTokens(usage));
Assert.Equal(800, ProviderHelper.GetCachedInputTokens(usage));
}

[Fact]
public void Input_tokens_without_cache_are_the_prompt_count()
{
Assert.Equal(50, ProviderHelper.GetInputTokens(new UsageMetadata { PromptTokenCount = 50 }));
Assert.Equal(0, ProviderHelper.GetCachedInputTokens(new UsageMetadata { PromptTokenCount = 50 }));
Assert.Equal(0, ProviderHelper.GetInputTokens(null));
Assert.Equal(0, ProviderHelper.GetCachedInputTokens(null));
}
}
Loading