Skip to content

fix(core): include Gemini thinking tokens in output usage - #5334

Merged
sailist merged 1 commit into
mainfrom
fix/gemini-thinking-token-usage
Nov 24, 2026
Merged

sailist merged 1 commit into
mainfrom
fix/gemini-thinking-token-usage

Conversation

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Resolve #5228

使用 Gemini thinking 模型时,google-genai 解码器漏算 thoughtsTokenCount,导致 output usage、tokenBudget 和遥测少算生成的 tokens。本 PR 将 thinking tokens 计入 output usage。

关联反馈:cfdc64e9-e3f3-4e30-82f6-4667ea8a8748。

Bug Reproduction Steps

See linked issue.

Root Cause

Gemini 分开返回 candidatesTokenCount 和 thoughtsTokenCount,但解码器只把前者写入 usage.output,因此遗漏 thinking tokens。

这是根本修复:在现有 usage 解码入口合并两类 output tokens,不改变其他协议或上层计量逻辑。

Code Changes

  Gemini usageMetadata
- output = candidatesTokenCount
+ thoughts = numeric thoughtsTokenCount or 0
+ output = candidatesTokenCount + thoughts
  • 数值型 thoughtsTokenCount 计入 output;字段缺失或不是数值时按 0 处理。
  • raw 继续保留原始 usageMetadata,input 与 cache 计量逻辑不变。
  • 新增 thinking + cached prompt tokens、thinking 为 0 的解码回归;现有无 thinking 字段的用例继续验证原行为。
  • changeset 只包含 @moonshot-ai/core 的 patch 条目。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Gemini 响应包含数值型 thoughtsTokenCount output 只包含 candidates tokens output 包含 candidates + thinking tokens 使用 Gemini thinking 模型并读取 output usage 的 tokenBudget、用户用量展示和遥测;旧值漏算生成的 tokens 无需保留漏算行为
thoughtsTokenCount 缺失、非数值或为 0 output 只包含 candidates tokens 不变 未启用 thinking 或响应未返回该字段的 Gemini 调用方 不适用

受影响模块与覆盖:

  • packages/core 的 google-genai usage 解码:新增 thinking、cached prompt tokens 和总量一致性回归,并覆盖 thinking 为 0 的情况。
  • 现有无 thinking 字段的 decode fixture 保持通过,确认原行为不变。
验证命令与结果
  • 修复前:新增 thinking 用例失败,期望 output=940、实际 output=40;其余 14 项通过。
  • 修复后:pnpm exec vitest run --reporter=agent packages/core/test/llm/google-genai/google-genai.test.ts,15/15 通过。
  • pnpm exec tsc -p packages/core/tsconfig.json --noEmit:通过。
  • pnpm lint:通过。
  • pnpm run check:duplication 9ff76d8e1982e08c95dfea60a25e5fb077ddf402:通过。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue.
  • I have added tests that prove my fix works.
  • The behavior-change table above is complete; no removed behavior or flipped default requires an escape hatch.
  • Ran gen-changesets skill and included the confirmed core-only patch changeset.
  • No user-document update is needed: this corrects usage accounting without changing configuration or documented workflows.

@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e421876

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T06:06:58.033217Z e421876 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@e421876
npx https://pkg.pr.new/@moonshot-ai/kimi-code@e421876

commit: e421876

@sailist
sailist merged commit f56a260 into main Nov 24, 2026
24 checks passed
@sailist
sailist deleted the fix/gemini-thinking-token-usage branch November 24, 2026 06:15
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
Route file download and session media URL calls through a sessionSync
route that answers from the current owner without a session load (MoonshotAI#5336).
Add thoughtsTokenCount to the Google GenAI output usage with thinking and
zero-thinking fixtures (MoonshotAI#5334), update the desktop composer e2e specs for
the label-only slash menu (MoonshotAI#5274), and document the Bash session/workspace
env vars (MoonshotAI#5053).
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.

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

2 participants