Repository navigation
fix(relaykit): report cached tokens in Gemini usageMetadata built from chat usage - #8333
Conversation
…m chat usage Gemini promptTokenCount includes cached content, but responses converted from chat usage (e.g. a Claude channel serving a Gemini-format request) always reported cachedContentTokenCount as 0 while billing_usage carried the cache read. Fill cachedContentTokenCount from the chat usage cache-read count in the non-stream, stream chunk, and stream state converters.
|
Caution Review failedThe pull request is closed. No actionable comments were generated in the recent review. WalkthroughGemini response conversion now reports cached-content token counts from OpenAI chat usage in non-streaming and streaming responses. Tests cover Claude and OpenAI chat usage across non-streaming responses, finalized streams, and stateless stream conversion. ChangesCached-token usage conversion
Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Suggested reviewers: Merge Risk:
|
…点、Kling 4.0 比例及定价接口防缓存 吸收 QuantumNous/new-api 窗口内 4 项修复与 1 项安全响应头: - a85f7e8 (QuantumNous#8412): relaykit Gemini 4 按 gemini-4- 前缀匹配并将非 neon/hydrogen 上限收紧至 high,保留本地 TestArgonReasoningDefaultsAndExplicitLevels - c53dcf3 (QuantumNous#8333): Chat 转 Gemini 非流式与流式响应补齐 cachedContentTokenCount - 51a1cee (QuantumNous#8296): DeepSeek 上报 OpenAI/Responses/Anthropic 三类端点元数据,保留 ChannelTypeCodex 退役 - ac1330c (QuantumNous#8348): Kling 4.0 映射 4:3 与 3:4 宽高比 - 8542c9d(仅防缓存头): 为 /api/pricing、/api/pricing/count、/api/perf-metrics 挂载 middleware.DisableCache();跳过已退役的业务分组过滤与 /api/data/perf-metrics/summary 测试:已运行 targeted relaykit/common/plugins/router 单测与 vet,以及根模块与 relaykit 的 GOWORK=off go vet ./...;未运行 Go -race(works 无 gcc)与全量档位。 Co-Authored-By: Claude Code <[email protected]>
…m chat usage (QuantumNous#8333) * fix(relaykit): report cached tokens in Gemini usageMetadata built from chat usage Gemini promptTokenCount includes cached content, but responses converted from chat usage (e.g. a Claude channel serving a Gemini-format request) always reported cachedContentTokenCount as 0 while billing_usage carried the cache read. Fill cachedContentTokenCount from the chat usage cache-read count in the non-stream, stream chunk, and stream state converters. * fix(relaykit): ignore non-positive cached token counts for Gemini usage --------- Co-authored-by: seefs001 <[email protected]> (cherry picked from commit c53dcf3)
…m chat usage (QuantumNous#8333) * fix(relaykit): report cached tokens in Gemini usageMetadata built from chat usage Gemini promptTokenCount includes cached content, but responses converted from chat usage (e.g. a Claude channel serving a Gemini-format request) always reported cachedContentTokenCount as 0 while billing_usage carried the cache read. Fill cachedContentTokenCount from the chat usage cache-read count in the non-stream, stream chunk, and stream state converters. * fix(relaykit): ignore non-positive cached token counts for Gemini usage --------- Co-authored-by: seefs001 <[email protected]> (cherry picked from commit c53dcf3)
…m chat usage (QuantumNous#8333) * fix(relaykit): report cached tokens in Gemini usageMetadata built from chat usage Gemini promptTokenCount includes cached content, but responses converted from chat usage (e.g. a Claude channel serving a Gemini-format request) always reported cachedContentTokenCount as 0 while billing_usage carried the cache read. Fill cachedContentTokenCount from the chat usage cache-read count in the non-stream, stream chunk, and stream state converters. * fix(relaykit): ignore non-positive cached token counts for Gemini usage --------- Co-authored-by: seefs001 <[email protected]> (cherry picked from commit c53dcf3)
本 PR 的代码由 AI 辅助生成(Claude Code)。
Agent
Links
User request
Verbatim: 按用户要求,以整理后的需求描述代替原话:
Claude 渠道处理 Gemini 格式请求时,new-api 返回的
usageMetadata.cachedContentTokenCount恒为 0,但缓存读已经计入promptTokenCount,和同一响应里billing_usage记录的缓存读不一致。按 Gemini 标准字段计费的下游会因此把缓存读按普通输入计费。需要修复这个转换,让标准字段如实反映缓存读;不能改变 new-api 自身的结算结果,也不能引入其他副作用。先提 issue,再提 PR。Later constraints or corrections from the user (quote, or none): 已并入上面的整理描述
Out of scope — refuse
Open gate — do not open unless all are satisfied
go buildor tests passed: yesKind
Issue facts
promptTokenCount=15035(包含缓存读 12981),cachedContentTokenCount=0;billing_usage里的cache_read_input_tokens=12981billing_usage对不上;本实例自己的结算不受影响Change
relaykit/relayconvert/internal/oai_chat/to_gemini_chat_resp.go:ResponseOpenAI2Gemini、StreamResponseOpenAI2Gemini、newGeminiStreamResponse三处补上cachedContentTokenCount,取 chat usage 里的缓存读:先读PromptTokensDetails.CachedTokens,为 0 时读InputTokensDetails.CachedTokens。流状态里保存的 usage 经过UsageFromChatUsage归一化,缓存读放在InputTokensDetails里;"finish 之后单独一个 usage chunk"和Finalize读的都是它。chat usage 的prompt_tokens本身就包含缓存读,和 GeminipromptTokenCount的语义一致,所以promptTokenCount不用改。billing_usage存在时,geminiBillingMetadataFromOpenAIUsage仍然整体覆盖为原始的 Gemini 用量,这一行为没变。Research
Duplicate / prior art
cachedContentTokenCount、billing_usage gemini、gemini cache usage、OpenAI2Gemini、claude to gemini、gemini cached tokensDocs and code
claudeInfo.Usage等)和billing_usage,不会回读这里生成的 Gemini 字段,所以本实例扣费不变。下游 new-api 有billing_usage时以它结算(service/billing_usage.go),也不变;没有billing_usage时,现在会按缓存读计费,这和 Gemini 字段本来的语义一致。Alternatives considered
cachedContentTokenCount截断到不超过promptTokenCount。没采用:正常输入下两者本来就满足这个关系;对 prompt 缺失的异常 usage 截断成 0,下游反而偏离更多。Usage()/SetUsage()的返回语义会跟着变,影响面更大。Files
relaykit/relayconvert/internal/oai_chat/to_gemini_chat_resp.gocachedContentTokenCount,新增取缓存读的函数geminiCachedContentTokenCountrelaykit/relayconvert/response_registry_test.goBehavior
promptTokenCount=15035, cachedContentTokenCount=0promptTokenCount=15035, cachedContentTokenCount=12981;本实例的消费日志不变(quota=5343, cache_tokens=12981)prompt_cache_hit_tokens没有读(非流式在relay/channel/openai/usage.go已经归一化);上游message_delta只带 output_tokens 时,最后一个 chunk 的promptTokenCount为 0,这是另一个问题,本 PR 不处理Verification
cd relaykit && GOWORK=off go build ./...→ 成功cd relaykit && GOWORK=off go test ./... -count=1→ 全部 okcd relaykit && GOWORK=off go vet ./relayconvert/ ./relayconvert/internal/oai_chat/→ 无输出go test ./relay/... ./service/... -count=1(根模块)→ exit 0TestConvertResponseToGeminiReportsCachedContentTokens:去掉本 PR 的修复后,两个子用例的非流式、流式、无状态断言全部失败(cached 实际为 0);只去掉InputTokensDetails那个兜底时,只有 chat 流式最后一个 chunk 的断言失败b388140ac和本分支构建,sqlite,新建 Anthropic 渠道指向本地模拟的 Anthropic Messages 服务(usage:input 2054 / cache_read 12981 / output 42,流式message_delta带累计 usage),用令牌请求POST /v1beta/models/claude-sonnet-4-5-20250929:generateContent和:streamGenerateContent?alt=sse:"promptTokenCount":15035,...,"cachedContentTokenCount":0"promptTokenCount":15035,...,"cachedContentTokenCount":12981,billing_usage和改动前一样prompt_tokens=2054, completion_tokens=42, cache_tokens=12981, cache_ratio=0.1, quota=5343, usage_billing_path=billing-usage-anthropicresponse_registry_test.go里新增一个表驱动测试Risks
prompt_tokens不包含缓存读、cached_tokens却有值(不符合 OpenAI 的语义),cachedContentTokenCount会大于promptTokenCount。new-api 自己构造的 chat usage 都包含缓存读。billing_usage时不变,没有时会按缓存读计费Scope check
Summary by CodeRabbit