Skip to content

fix: 支持 Gemini thoughtsTokenCount 计费#326

Closed
sususu98 wants to merge 2 commits intoding113:devfrom
sususu98:fix/gemini-thoughts-token-support
Closed

fix: 支持 Gemini thoughtsTokenCount 计费#326
sususu98 wants to merge 2 commits intoding113:devfrom
sususu98:fix/gemini-thoughts-token-support

Conversation

@sususu98
Copy link
Contributor

@sususu98 sususu98 commented Dec 12, 2025

Summary

  • 在 GeminiUsageMetadata 类型中添加 thoughtsTokenCount 字段
  • 新增 GeminiTokenDetail 类型支持按 modality 分类的 token 详情
  • thoughtsTokenCount 累加到 output_tokens 进行计费(Gemini 思考 token 价格与输出 token 相同)
  • 添加 promptTokensDetailscacheTokensDetailscandidatesTokensDetails 字段支持

Problem

Gemini 思考模型(如 gemini-2.5-flash、gemini-2.5-pro)在响应中会返回 thoughtsTokenCount 字段表示推理消耗的 token 数。由于思考 token 价格与输出 token 价格相同(参考 LiteLLM 的 output_cost_per_reasoning_token),直接累加到 output_tokens 一起计费是最简单的方式。

Related Issues:

Related PRs:

Solution

extractUsageMetrics 函数中新增对 Gemini thoughtsTokenCount 字段的处理:

  • 检测 usage 响应中是否包含 thoughtsTokenCount
  • 将其值累加到 output_tokens 中(因价格相同)
  • 同时扩展 GeminiUsageMetadata 类型以支持完整的 Gemini API 响应字段

Changes

Core Changes

  • src/app/v1/_lib/gemini/types.ts: 新增 GeminiTokenDetail 类型,扩展 GeminiUsageMetadata 类型支持 thoughtsTokenCount 和详细 token 分类字段
  • src/app/v1/_lib/proxy/response-handler.ts: 在 extractUsageMetrics 函数中添加 Gemini 思考 token 处理逻辑

Supporting Changes

  • CHANGELOG.md: 更新 v0.3.28 版本记录

Testing

Manual Testing

  • 测试 Gemini 非思考模型请求(无 thoughtsTokenCount 字段)正常计费
  • 测试 Gemini 思考模型请求(gemini-2.5-flash、gemini-2.5-pro),验证 thoughtsTokenCount 被正确累加到输出 token

Checklist

  • Code follows project conventions
  • Self-review completed
  • Tests pass locally
  • Documentation updated (CHANGELOG.md updated)

Description enhanced by Claude AI

github-actions bot and others added 2 commits December 10, 2025 12:03
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- 在 GeminiUsageMetadata 类型中添加 thoughtsTokenCount 字段
- 新增 GeminiTokenDetail 类型支持按 modality 分类的 token 详情
- 将 thoughtsTokenCount 累加到 output_tokens 进行计费
  (Gemini 思考 token 价格与输出 token 相同)
- 添加 promptTokensDetails、cacheTokensDetails、
  candidatesTokensDetails 字段支持

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @sususu98, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在增强对 Gemini 模型 "thoughtsTokenCount" 的计费支持。通过在 "GeminiUsageMetadata" 中引入 "thoughtsTokenCount" 字段和新的 "GeminiTokenDetail" 类型,系统现在能够更精细地捕获和处理 Gemini 模型在推理过程中产生的“思考”token。鉴于这些思考 token 的价格与输出 token 相同,本次更新将 "thoughtsTokenCount" 直接累加到 "output_tokens" 中,确保了计费逻辑的准确性和一致性,从而全面反映 Gemini 模型的实际资源消耗。

Highlights

  • Gemini 计费支持: 为 Gemini 模型引入 "thoughtsTokenCount" 字段,以支持其思考过程中的 token 计费。
  • Token 详情类型: 新增 "GeminiTokenDetail" 类型,用于按模态(如文本、图像)分类展示 token 消耗详情。
  • 计费逻辑调整: 将 Gemini 模型的 "thoughtsTokenCount" 累加到 "output_tokens" 中进行计费,因其价格与输出 token 相同。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次合并请求旨在为 Gemini 的 thoughtsTokenCount 添加计费支持。types.ts 中的类型定义更新是正确的。然而,在 response-handler.ts 中存在一个严重的计费逻辑错误:thoughtsTokenCount 被累加到输出 token 后,可能会被后续代码覆盖,导致计费不准。此外,CHANGELOG.md 的更新内容与本次修改不符,需要修正。请务必修复计费逻辑的 bug。

Comment on lines +1210 to +1213
if (typeof usage.thoughtsTokenCount === "number" && usage.thoughtsTokenCount > 0) {
result.output_tokens = (result.output_tokens ?? 0) + usage.thoughtsTokenCount;
hasAny = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

这里存在一个严重的计费逻辑错误。当前代码在将 thoughtsTokenCount 累加到 result.output_tokens 之后,后续位于 1215 行的代码块会无条件地用 usage.output_tokens 的值覆盖 result.output_tokens。这将导致在 usage.output_tokens 存在的情况下,thoughtsTokenCount 的累加值被丢弃,从而造成计费错误。

为了修复此问题,建议将这个 thoughtsTokenCount 的处理逻辑移动到 output_tokens 赋值逻辑(第 1215-1218 行)之后,以确保 thoughtsTokenCount 是在最终的 output_tokens 基础上进行累加。

Comment on lines +7 to +21
## [v0.3.28](https://github.com/ding113/claude-code-hub/releases/tag/v0.3.28) - 2025-12-10

### 新增

- 日志页面新增快速日期筛选器(今日/昨日/近7天/近30天)和 CSV 导出功能 (#314)
- Session 监控页面新增分页功能,支持分别对活跃和非活跃 Session 进行分页浏览 (#314)

### 优化

- 每日排行榜改用滚动 24 小时窗口计算,替代原先基于日历日的统计方式 (#314)
- 上游 404 错误现在触发供应商故障切换而不计入熔断器,提升中转服务兼容性 (#314)

### 修复

- 修复 Anthropic SSE 流式响应中 output_tokens 提取问题,现在从 message_delta 事件正确获取 (#313)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

变更日志的内容与本次合并请求的修改不匹配。本次修改的核心是支持 Gemini 的 thoughtsTokenCount 计费,但 v0.3.28版本的日志条目描述的是其他功能(#314, #313)。请更新此部分,以准确反映本次 PR 的实际贡献。

@github-actions github-actions bot added the size/XS Extra Small PR (< 50 lines) label Dec 12, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

This PR adds support for Gemini thinking model token billing by accumulating thoughtsTokenCount into output_tokens. The implementation is clean, minimal, and correctly handles Gemini's API response format.

PR Size: XS

  • Lines changed: 35 (34 additions, 1 deletion)
  • Files changed: 3

Issues Found

Category Critical High Medium Low
Logic/Bugs 0 0 0 0
Security 0 0 0 0
Error Handling 0 0 0 0
Types 0 0 0 0
Comments/Docs 0 0 0 0
Tests 0 0 0 0
Simplification 0 0 0 0

Analysis

Code Logic Review:

  • The insertion point for thoughtsTokenCount handling is correct. For Gemini responses:
    1. candidatesTokenCount sets result.output_tokens
    2. thoughtsTokenCount adds to result.output_tokens via (result.output_tokens ?? 0) + thoughtsTokenCount
    3. The output_tokens field check won't trigger (Gemini API doesn't use this field)
  • Result: Total billable output = candidatesTokenCount + thoughtsTokenCount

Type Definitions:

  • GeminiTokenDetail interface properly types the modality-based token details
  • Optional fields follow existing patterns in GeminiUsageMetadata

Test Coverage:

  • Note: extractUsageMetrics function has no existing unit tests. This is a pre-existing condition - the PR maintains the same coverage pattern. The PR description includes a manual test plan.

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Adequate (manual test plan provided)
  • Code clarity - Good

Automated review by Claude AI

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code Review Summary

This PR adds support for Gemini thinking model token billing by accumulating thoughtsTokenCount into output_tokens. The implementation is clean, minimal, and correctly handles Gemini's API response format.

PR Size: XS

  • Lines changed: 35 (34 additions, 1 deletion)
  • Files changed: 3

Issues Found

Category Critical High Medium Low
Logic/Bugs 0 0 0 0
Security 0 0 0 0
Error Handling 0 0 0 0
Types 0 0 0 0
Comments/Docs 0 0 0 0
Tests 0 0 0 0
Simplification 0 0 0 0

Analysis

Code Logic Review:

  • The insertion point for thoughtsTokenCount handling is correct. For Gemini responses:
    1. candidatesTokenCount sets result.output_tokens
    2. thoughtsTokenCount adds to result.output_tokens via (result.output_tokens ?? 0) + thoughtsTokenCount
    3. The output_tokens field check won't trigger (Gemini API doesn't use this field)
  • Result: Total billable output = candidatesTokenCount + thoughtsTokenCount

Type Definitions:

  • GeminiTokenDetail interface properly types the modality-based token details
  • Optional fields follow existing patterns in GeminiUsageMetadata

Test Coverage:

  • Note: extractUsageMetrics function has no existing unit tests. This is a pre-existing condition - the PR maintains the same coverage pattern. The PR description includes a manual test plan.

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Adequate (manual test plan provided)
  • Code clarity - Good

Automated review by Claude AI

@sususu98 sususu98 closed this Dec 12, 2025
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 12, 2025
@github-actions github-actions bot mentioned this pull request Dec 12, 2025
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Extra Small PR (< 50 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant

Comments