From 4bfeeecb05a193719cec5d676e268f23a4ede1d0 Mon Sep 17 00:00:00 2001 From: liuxiongfeng Date: Mon, 2 Feb 2026 12:50:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(billing):=20=E4=BF=AE=E5=A4=8D=20Gemini=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=BC=93=E5=AD=98=20token=20=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit extractGeminiUsage 函数未提取 cachedContentTokenCount, 导致计费时缓存读取 token 始终为 0。 修复: - 提取 usageMetadata.cachedContentTokenCount - 设置 CacheReadInputTokens 字段 - InputTokens 减去缓存 token(与 response_transformer 逻辑一致) --- .../internal/service/gemini_messages_compat_service.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/internal/service/gemini_messages_compat_service.go b/backend/internal/service/gemini_messages_compat_service.go index aea880c2..659cdf03 100644 --- a/backend/internal/service/gemini_messages_compat_service.go +++ b/backend/internal/service/gemini_messages_compat_service.go @@ -2522,9 +2522,13 @@ func extractGeminiUsage(geminiResp map[string]any) *ClaudeUsage { } prompt, _ := asInt(usageMeta["promptTokenCount"]) cand, _ := asInt(usageMeta["candidatesTokenCount"]) + cached, _ := asInt(usageMeta["cachedContentTokenCount"]) + // 注意:Gemini 的 promptTokenCount 包含 cachedContentTokenCount, + // 但 Claude 的 input_tokens 不包含 cache_read_input_tokens,需要减去 return &ClaudeUsage{ - InputTokens: prompt, - OutputTokens: cand, + InputTokens: prompt - cached, + OutputTokens: cand, + CacheReadInputTokens: cached, } }