Bug: .NET: Gemini connector emits duplicate token usage metrics during streaming (similar to #12977 for Python/OpenAI)
Describe the bug
The .NET Gemini connector emits OpenTelemetry token usage metrics for every streaming chunk instead of only once at the end. This causes inflated token usage and cost tracking in downstream telemetry systems.
To Reproduce
- Use
GeminiChatCompletionClientwith streaming enabled - Monitor the OpenTelemetry counters (
s_promptTokensCounter,s_completionTokensCounter,s_totalTokensCounter) which expose the metricsMicrosoft.SemanticKernel.Connectors.Google.tokens.prompt,Microsoft.SemanticKernel.Connectors.Google.tokens.completion, andMicrosoft.SemanticKernel.Connectors.Google.tokens.total. - Observe metrics are emitted for every chunk (e.g., 30 chunks = 30 metric events for a single API call)
Expected: Token usage metrics emitted once per API call Actual: Token usage metrics emitted once per streaming chunk
Root Cause
In ProcessChatResponseStreamAsync (around line 540 in commit 5948dbc32), the method calls ProcessChatResponse() for every streaming chunk, which in turn calls LogUsage() (around line 568).
The Gemini API sends accumulated token counts in every chunk (not deltas), so each LogUsage() call emits the full token count to the OpenTelemetry counters (around lines 600–602).
File: dotnet/src/Connectors/Connectors.Google/Core/Gemini/Clients/GeminiChatCompletionClient.cs
Comparison with OpenAI Connector
The OpenAI connector does not call LogUsage() during streaming—only after non-streaming completion (see GetChatMessageContentsAsync around line 173 in the same commit).
File: dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs
Related
This appears to be the same bug pattern that was fixed for Python/OpenAI in #12977 (merged Sep 3, 2025).
Impact
Production systems show inflated costs (e.g., $0.40 logged vs. $0.02 actual API cost) due to duplicate telemetry.
Environment
- SK Version: 1.66.0 (commit 5948dbc32)
- Connector:
Microsoft.SemanticKernel.Connectors.Google - Models: All Gemini models (observed with
gemini-2.5-pro) - .NET: 8+
Just to add some more context:
- For OpenAI / Azure OpenAI, I have proposed a .NET fix in PR #13386 that makes the streaming path emit exactly one usage event, based on the final usage-only chunk (stream_options: {"include_usage": true}).
- That pattern was inspired by the already merged Python SK fix in #12977.
Conceptually, I think it would be good if all connectors followed the same rule:
“Per logical completion call, emit exactly one consolidated token usage event, regardless of whether the response is streaming or non-streaming.”
For Gemini, that would likely mean:
- During streaming, keep track of the latest usageMetadata (which is cumulative per chunk),
- and then log usage once at the end using that final cumulative value,
- instead of logging on every streaming chunk as today.
I’m happy to contribute a PR for the Gemini connector as well, once there is agreement on the desired semantics.
This issue is stale because it has been open for 90 days with no activity.
I traced the root cause in GeminiChatCompletionClient.cs and have a minimal fix ready. Happy to open a PR if this is still wanted.
Root cause
ProcessChatResponseStreamAsync calls ProcessChatResponse() for every SSE chunk:
private async IAsyncEnumerable<GeminiChatMessageContent> ProcessChatResponseStreamAsync(
Stream responseStream, ...)
{
await foreach (var response in this.ParseResponseStreamAsync(responseStream, ct: ct))
{
foreach (var messageContent in this.ProcessChatResponse(response)) // ← called N times
{
yield return messageContent;
}
}
}
ProcessChatResponse always calls LogUsage, and the Gemini API sends cumulative token counts in every chunk, so all three counters (tokens.prompt, tokens.completion, tokens.total) get N-times inflated where N = number of SSE chunks.
Proposed fix
Separate content yielding from usage logging — track the last non-null metadata across chunks and call LogUsage exactly once after the stream ends. The same file already does this pattern in GetStreamingChatMessageContentsOrPopulateStateForToolCallingAsync (tracking lastMetadata), so this is consistent:
private async IAsyncEnumerable<GeminiChatMessageContent> ProcessChatResponseStreamAsync(
Stream responseStream,
[EnumeratorCancellation] CancellationToken ct)
{
GeminiMetadata? lastUsageMetadata = null;
await foreach (var response in this.ParseResponseStreamAsync(responseStream, ct: ct).ConfigureAwait(false))
{
ValidateGeminiResponse(response);
var chatMessageContents = this.GetChatMessageContentsFromResponse(response);
// Track usage metadata from each chunk (Gemini sends cumulative counts every chunk)
if (chatMessageContents.Count > 0 &&
chatMessageContents[0].Metadata is GeminiMetadata metadata &&
metadata.TotalTokenCount > 0)
{
lastUsageMetadata = metadata;
}
foreach (var messageContent in chatMessageContents)
{
yield return messageContent;
}
}
// Emit usage metrics exactly once, using the final chunk's cumulative counts
if (lastUsageMetadata is not null)
{
s_promptTokensCounter.Add(lastUsageMetadata.PromptTokenCount);
s_completionTokensCounter.Add(lastUsageMetadata.CandidatesTokenCount);
s_totalTokensCounter.Add(lastUsageMetadata.TotalTokenCount);
}
}
The non-streaming path (GenerateChatMessageAsync → ProcessChatResponse) is already correct and does not need to change.
Happy to open a PR with this fix plus a unit test that verifies LogUsage is called exactly once per streaming call regardless of chunk count.
This issue is stale because it has been open for 90 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.