Skip to content

[Bug] google-genai: Gemini thinking tokens (thoughtsTokenCount) are dropped from output usage #5228

Done

Description

@thejesh23

What version of Kimi Code is running?

2.7.2 (source main at e12cad1, 2026-11-22). Reproduced with a decoder unit test.

Which open platform/subscription were you using?

None involved. This is in the google-genai protocol decoder (Gemini API and Vertex AI).

Which model were you using?

Any Gemini thinking model (for example Gemini 2.5 Pro/Flash with thinking enabled).

What platform is your computer?

Darwin 25.6.0 arm64 arm (the code path is platform-independent).

What issue are you seeing?

The google-genai decoder drops Gemini thinking tokens from the reported output usage.

parseUsage in packages/core/src/llm/builtin/protocol/google-genai/decode.ts sets output to candidatesTokenCount and never reads thoughtsTokenCount:

  const candidatesTokenCount =
    typeof usageMetadata['candidatesTokenCount'] === 'number'
      ? usageMetadata['candidatesTokenCount']
      : 0;
  return {
    inputOther: Math.max(promptTokenCount - (cachedContentTokenCount ?? 0), 0),
    output: candidatesTokenCount,

Gemini reports thinking tokens in a separate field. The @google/genai type docs say totalTokenCount is "the sum of prompt_token_count, candidates_token_count, tool_use_prompt_token_count, and thoughts_token_count", so candidatesTokenCount does not include thoughts. Thinking tokens are billed as output tokens.

The other decoders already count reasoning as output: openai uses completion_tokens and anthropic uses output_tokens, and both include reasoning or thinking tokens. On a Gemini thinking model, usage.output can be a small fraction of what the model generated. As a result:

  • goal tokenBudget accounting (which accumulates usage.output) under-counts, so a budget can be exceeded without being noticed
  • the usage shown to users and sent to telemetry under-reports output tokens

What steps can reproduce the bug?

Feed the decoder one Gemini chunk with thinking usage:

usageMetadata: {
  promptTokenCount: 1200,
  candidatesTokenCount: 40,
  thoughtsTokenCount: 900,
  totalTokenCount: 2140,
}

Observed: the decoder emits usage { inputOther: 1200, output: 40, ... }. That accounts for 1240 of the 2140 total tokens; the 900 thinking tokens are lost.

What is the expected behavior?

output should be candidatesTokenCount + thoughtsTokenCount (940 in the example). Responses that carry no thoughtsTokenCount keep their current output.

Additional information

The fix is a few lines in parseUsage plus a decode fixture with thoughtsTokenCount. I have it ready with a regression test that fails on current main, and can open the PR once this is approved.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions