需求:增加thinking 的token数量计算
由于最近的gpt降智问题想增加这一部分的记录用于监控防止516 thinking问题
感谢你的建议。这属于功能请求(增加 thinking / 推理 token 的独立观测能力)。我先基于代码库梳理了现状,方便后续讨论实现方式。
现状分析(thinking token 目前并未被单独记录)
当前系统对 token 的提取与存储只区分 input / output / 各类 cache,没有任何地方单独保存「思考 / 推理」token。
1. 提取层(src/app/v1/_lib/proxy/response-handler.ts)
UsageMetrics 类型(response-handler.ts:425-436)只包含以下字段,没有 thinking / reasoning 字段:
input_tokens/output_tokenscache_creation_input_tokens/cache_creation_5m_input_tokens/cache_creation_1h_input_tokenscache_read_input_tokens/cache_ttlinput_image_tokens/output_image_tokens
extractUsageMetrics(response-handler.ts:3374-3604)对各供应商的 thinking token 处理如下:
- Gemini:
thoughtsTokenCount在response-handler.ts:3489-3490被直接累加进output_tokens(注释说明「思考价格与输出价格相同」),没有单独保留。 - OpenAI / GPT 推理模型:
output_tokens_details.reasoning_tokens完全没有被提取(类型定义在src/app/v1/_lib/codex/types/response.ts:91-93和src/actions/providers.ts:3295中存在,但提取逻辑忽略了它)。 - Claude:上游 API 把 thinking 计入
output_tokens,本仓库也未单独拆分。
2. 存储层(src/drizzle/schema.ts、src/types/message.ts)
usage_ledger 表(schema.ts:1028-1061)与 MessageRequest(message.ts:285-291)的 token 列只有:input_tokens、output_tokens、cache_creation_input_tokens、cache_read_input_tokens、cache_creation_5m_input_tokens、cache_creation_1h_input_tokens —— 没有 thinking / reasoning 列。
3. 计费层(src/lib/utils/cost-calculation.ts)
该文件内的 UsageMetrics 类型(cost-calculation.ts:7-18)同样没有 thinking 字段,目前推理 token 实际是按 output 价格隐式计费的。
实现这个监控需要改动的环节
要让「thinking token 数量」可被监控,至少需要把字段打通四个环节:
- 提取:在
extractUsageMetrics(response-handler.ts:3374)中新增对output_tokens_details.reasoning_tokens(OpenAI Responses/Chat)、GeminithoughtsTokenCount(改为单独保留而非并入 output)、以及 Claude thinking 的识别,并在UsageMetrics类型(response-handler.ts:425)中新增字段。 - 存储:按本仓库的迁移规范(
CLAUDE.md中的 Database Migration Workflow),在src/drizzle/schema.ts的usageLedger和message.ts的MessageRequest上新增列(例如reasoning_tokens),再用bun run db:generate生成迁移。 - 写入:在消息写入路径(
src/repository/message.ts/message-write-buffer.ts)与 ledger 写入路径补上该字段。 - 展示:在 usage 日志 / dashboard 上增加该列与可能的聚合图表。
需要明确的点
- 计费语义:是否需要把 reasoning token 从 output 中拆出来按不同价格计费?还是仅用于观测、仍按 output 价格计费?两种方案的实现复杂度差别较大。
- 「516 thinking 问题」的具体含义:issue 中提到的「516」在代码库中没有对应,能否补充一下指的是哪类错误或现象(例如某个上游返回的错误码、还是某种 thinking 异常的表现)?这会帮助确定监控指标的具体形态。
- 供应商差异:Claude / OpenAI / Gemini 暴露 thinking token 的方式不同(见上文),统一指标需要明确归一化口径。
Automated response from Claude AI
仅用于观测、仍按 output 价格计费.516指的是openai 在部分情况下会固定进行短思考 参考 https://github.com/openai/codex/issues/30364
This issue has been automatically marked as stale because it has not had any activity in the last 30 days.
If this issue is still relevant:
- Please comment to keep it open
- Add any new information that might help resolve it
This issue will be automatically closed in 14 days if there is no further activity.
This issue has been automatically marked as stale because it has not had any activity in the last 30 days.
If this issue is still relevant:
- Please comment to keep it open
- Add any new information that might help resolve it
This issue will be automatically closed in 14 days if there is no further activity.
This issue has been automatically closed due to inactivity.
If you believe this issue is still relevant, please feel free to reopen it with additional information.