Skip to content

fix: Gemini 适配器补全 usage 字段映射#1233

Merged
claude-code-best merged 2 commits into
claude-code-best:mainfrom
Evsdrg:fix/gemini-cache-usage
May 16, 2026
Merged

fix: Gemini 适配器补全 usage 字段映射#1233
claude-code-best merged 2 commits into
claude-code-best:mainfrom
Evsdrg:fix/gemini-cache-usage

Conversation

@Evsdrg
Copy link
Copy Markdown
Contributor

@Evsdrg Evsdrg commented May 16, 2026

Summary

Gemini API 的 usageMetadata 包含 cachedContentTokenCount 字段,
但此前未映射到 Anthropic 格式的 cache_read_input_tokens,导致
cache 相关字段始终为 0。

同时 message_delta 事件此前只携带 output_tokens,缺失
input_tokenscache_creation_input_tokenscache_read_input_tokens
与 OpenAI/Anthropic 适配器不一致。下游代码(如 cacheWarning.ts
cacheStatsState.tsStatusLine.tsx)从 message_delta 读取
最终 token 计数时会获得不完整数据。

Changes

  1. 新增 cachedContentTokenCountcache_read_input_tokens 映射
  2. message_start 中的 cache_read_input_tokens 从固定 0 改为实际值
  3. message_delta 从只发送 output_tokens 改为发送完整四个字段

Gemini usageMetadata fields (per API docs)

Gemini field Anthropic mapping Status
promptTokenCount input_tokens ✅ 已有
candidatesTokenCount output_tokens (part) ✅ 已有
thoughtsTokenCount output_tokens (part) ✅ 已有
cachedContentTokenCount cache_read_input_tokens 新增
(无等价) cache_creation_input_tokens 始终为 0

Test plan

  • tsc --noEmit 零错误
  • biome format 零差异
  • biome lint 零违规

🤖 Generated with Claude Code Best

Summary by CodeRabbit

  • Bug Fixes
    • Token usage reporting now includes cached-content token reads in usage metrics.
    • Cached-read token counts are included in message start and message delta usage payloads, and final delta usage now reflects cached reads for more accurate reporting.

Review Change Stack

Gemini API 的 usageMetadata 包含 cachedContentTokenCount 字段,
但此前未映射到 Anthropic 格式的 cache_read_input_tokens,导致
cache_creation_input_tokens 和 cache_read_input_tokens 始终为 0。

同时 message_delta 事件此前只携带 output_tokens,缺失
input_tokens、cache_creation_input_tokens、cache_read_input_tokens,
导致下游从 message_delta 读取最终 token 计数时获取不完整数据。

修复:
- 新增 cachedContentTokenCount → cache_read_input_tokens 映射
- message_start 和 message_delta 携带完整四个 usage 字段
- cache_creation_input_tokens 保持为 0(Gemini API 无等价概念)

Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8066afa5-68f7-4e5c-ac27-b353792e8a41

📥 Commits

Reviewing files that changed from the base of the PR and between 61afe23 and 4d2e38a.

📒 Files selected for processing (1)
  • packages/@ant/model-provider/src/providers/gemini/types.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/@ant/model-provider/src/providers/gemini/types.ts

📝 Walkthrough

Walkthrough

The Gemini stream adapter now accumulates cached-content token reads from Gemini chunk usage metadata and reports them as cache_read_input_tokens in both initial (message_start) and final (message_delta) streamed usage payloads; the type for cachedContentTokenCount was added.

Changes

Gemini Cache Token Tracking

Layer / File(s) Summary
Cached-content token tracking through stream consumption
packages/@ant/model-provider/src/providers/gemini/types.ts, packages/@ant/model-provider/src/providers/gemini/streamAdapter.ts
Adds cachedContentTokenCount?: number to GeminiUsageMetadata, initializes and accumulates cachedReadTokens from chunk.usageMetadata.cachedContentTokenCount during stream processing, and includes that value as cache_read_input_tokens in message_start and message_delta usage payloads.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I nibble tokens in the stream, so fleet,
counting crumbs of cache beneath your feet,
I hop and add a number, neat and small —
now each usage tells the tale of all. 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: completing the mapping of usage fields in the Gemini adapter to include cached token reads, which is the core focus of the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/`@ant/model-provider/src/providers/gemini/streamAdapter.ts:
- Line 19: The code references a non-existent field cachedContentTokenCount on
GeminiUsageMetadata which causes a TypeScript error; update the
GeminiUsageMetadata type (add cachedContentTokenCount?: number) in types.ts so
the assignment in streamAdapter.ts that updates cachedReadTokens from
usage.cachedContentTokenCount compiles, or if the Gemini API never supplies
cached content counts, remove the usage.cachedContentTokenCount fallback
assignment and rely solely on existing fields (ensure to update the type or
remove the line referencing cachedContentTokenCount accordingly).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e047b224-3c03-4e23-a320-17458fa1c19e

📥 Commits

Reviewing files that changed from the base of the PR and between 5b941d4 and 61afe23.

📒 Files selected for processing (1)
  • packages/@ant/model-provider/src/providers/gemini/streamAdapter.ts

Comment thread packages/@ant/model-provider/src/providers/gemini/streamAdapter.ts
streamAdapter.ts 使用 usage.cachedContentTokenCount 但该字段未
在 GeminiUsageMetadata 类型中声明。CodeRabbit 审查发现此问题。

Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
@claude-code-best claude-code-best merged commit ecd3f9d into claude-code-best:main May 16, 2026
3 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