Skip to content

fix: count Gemini thoughtsTokenCount as output tokens - #154

Merged
lich0821 merged 1 commit into
lich0821:masterfrom
pcbeingused333:fix/gemini-thoughts-tokens
Oct 27, 2026
Merged

lich0821 merged 1 commit into
lich0821:masterfrom
pcbeingused333:fix/gemini-thoughts-tokens

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Gemini reports reasoning tokens in usageMetadata.thoughtsTokenCount, separate from candidatesTokenCount, and bills them as output. The converters only read candidatesTokenCount, so with a thinking model the output usage reported to the client (and recorded for stats) left the reasoning tokens out.

GeminiResponse/GeminiStreamChunk usage metadata now carries thoughtsTokenCount, and a small helper geminiOutputTokens returns candidates plus thoughts. It is used by the non-streaming Claude, OpenAI chat and OpenAI Responses conversions and by syncGeminiUsageMetadata for all the streaming ones. total_tokens is untouched: it already comes from Gemini's totalTokenCount, which includes the thoughts.

New tests send a Gemini response with 3 candidate and 7 thought tokens through GeminiRespToClaude, GeminiRespToOpenAI and GeminiStreamToClaude and expect 10 output tokens. All three fail on master and pass with the change, together with the rest of ./internal/transformer/.... Not run against the live Gemini API.

This change was prepared with AI assistance (Claude Code).

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@lich0821
lich0821 merged commit 1f9d3cd into lich0821:master Oct 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants