Account for Gemini reasoning tokens in usage details#551
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request updates the Gemini provider’s usage metadata conversion to correctly preserve Gemini “reasoning” (thoughts) tokens in the framework’s UsageDetails, aligning Gemini with the existing OpenAI providers’ behavior.
Changes:
- Map Gemini
ThoughtsTokenCountintomessage.UsageDetails.ReasoningTokenCountintoUsageDetails. - Add a regression test ensuring
thoughtsTokenCountis surfaced and reconciles withtotalTokenCount.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| provider/geminiprovider/agent.go | Populate UsageDetails.ReasoningTokenCount from Gemini usage metadata (ThoughtsTokenCount). |
| provider/geminiprovider/agent_test.go | Add test coverage verifying reasoning tokens are not dropped for Gemini responses. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
aedf423 to
77ad0cb
Compare
toUsageDetails dropped GenerateContentResponseUsageMetadata.ThoughtsTokenCount entirely. Per the genai docs, thoughtsTokenCount is a distinct bucket within totalTokenCount (alongside prompt, candidates, and tool-use prompt tokens), so for a thinking model the reasoning tokens vanished from accounting: input + output + reasoning no longer reconciled with the total, and the framework's dedicated ReasoningTokenCount field stayed zero. Map ThoughtsTokenCount to UsageDetails.ReasoningTokenCount, matching the OpenAI chat and Responses providers.
77ad0cb to
49f0779
Compare
|
Parity review complete - no issues found. See full analysis below. Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "awmgmcpg"See Network Configuration for more information.
|
When converting Gemini usage metadata,
toUsageDetailsmapped four fields but droppedThoughtsTokenCountentirely:Per the genai docs,
TotalTokenCountis "the sum ofprompt_token_count,candidates_token_count,tool_use_prompt_token_count, andthoughts_token_count" — sothoughtsTokenCountis a separate bucket, not included incandidatesTokenCount. For a thinking model, those reasoning tokens were silently lost:InputTokenCount + OutputTokenCount + ReasoningTokenCountno longer reconciled withTotalTokenCount, and the framework’s dedicatedUsageDetails.ReasoningTokenCountfield (summed byUsageDetails.Add) stayed0.Both OpenAI providers already populate
ReasoningTokenCount(chat.go,responses.go); Gemini was the outlier.Fix
Test
TestUsageContent_ReasoningTokensreturns a response withthoughtsTokenCount: 8(prompt 10, candidates 5, total 23) and assertsReasoningTokenCount == 8. It fails on the old code (0) and passes with the fix; the record now reconciles (10 + 5 + 8 = 23).