Conversation
🤖 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>
Summary of ChangesHello @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
Using Gemini Code AssistThe 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
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 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
|
| if (typeof usage.thoughtsTokenCount === "number" && usage.thoughtsTokenCount > 0) { | ||
| result.output_tokens = (result.output_tokens ?? 0) + usage.thoughtsTokenCount; | ||
| hasAny = true; | ||
| } |
There was a problem hiding this comment.
这里存在一个严重的计费逻辑错误。当前代码在将 thoughtsTokenCount 累加到 result.output_tokens 之后,后续位于 1215 行的代码块会无条件地用 usage.output_tokens 的值覆盖 result.output_tokens。这将导致在 usage.output_tokens 存在的情况下,thoughtsTokenCount 的累加值被丢弃,从而造成计费错误。
为了修复此问题,建议将这个 thoughtsTokenCount 的处理逻辑移动到 output_tokens 赋值逻辑(第 1215-1218 行)之后,以确保 thoughtsTokenCount 是在最终的 output_tokens 基础上进行累加。
| ## [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) |
There was a problem hiding this comment.
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
thoughtsTokenCounthandling is correct. For Gemini responses:candidatesTokenCountsetsresult.output_tokensthoughtsTokenCountadds toresult.output_tokensvia(result.output_tokens ?? 0) + thoughtsTokenCount- The
output_tokensfield check won't trigger (Gemini API doesn't use this field)
- Result: Total billable output =
candidatesTokenCount + thoughtsTokenCount✅
Type Definitions:
GeminiTokenDetailinterface properly types the modality-based token details- Optional fields follow existing patterns in
GeminiUsageMetadata
Test Coverage:
- Note:
extractUsageMetricsfunction 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
There was a problem hiding this comment.
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
thoughtsTokenCounthandling is correct. For Gemini responses:candidatesTokenCountsetsresult.output_tokensthoughtsTokenCountadds toresult.output_tokensvia(result.output_tokens ?? 0) + thoughtsTokenCount- The
output_tokensfield check won't trigger (Gemini API doesn't use this field)
- Result: Total billable output =
candidatesTokenCount + thoughtsTokenCount✅
Type Definitions:
GeminiTokenDetailinterface properly types the modality-based token details- Optional fields follow existing patterns in
GeminiUsageMetadata
Test Coverage:
- Note:
extractUsageMetricsfunction 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
Summary
thoughtsTokenCount字段GeminiTokenDetail类型支持按 modality 分类的 token 详情thoughtsTokenCount累加到output_tokens进行计费(Gemini 思考 token 价格与输出 token 相同)promptTokensDetails、cacheTokensDetails、candidatesTokensDetails字段支持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函数中新增对 GeminithoughtsTokenCount字段的处理:thoughtsTokenCountoutput_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
thoughtsTokenCount字段)正常计费thoughtsTokenCount被正确累加到输出 tokenChecklist
Description enhanced by Claude AI