fix: preserve cache usage across protocol conversion

This commit is contained in:
Jason Xu committed 2026-08-23 22:12:22 +08:00
1 parent c4c1f36d61
commit 4f0f26711a
3 files changed
+40 -18

No files matched your search

+1 -1
View File
@@ -70,7 +70,7 @@ Token 页面展示用量覆盖率、每分钟趋势、模型排行与 Zen/Go Tie
| `upstream` | 上游尝试的 `lifetime`、`last_hour` 与 `recent`。按 Tier、匿名/Key 通道、Key 指纹聚合。 |
| `resources` | 模型目录、Key 冷却、脱敏代理节点、匿名开关和 models.dev metadata 状态。 |
`usage.*.tokens` 与模型/Tier 项均包含 `input_tokens`、`output_tokens`、`cached_tokens`、`reasoning_tokens`、`total_tokens`。`metrics.series` 的每分钟点也包含这些 Token 字段和 `usage_reported`。普通 JSON、同协议 SSE 与跨协议 SSE 响应都会解析上游 usage;`coverage` 是收到 usage 的推理请求数除以已建立上游路由的推理请求数。上游未提供 usage 时不会估算 Token。
`usage.*.tokens` 与模型/Tier 项均包含 `input_tokens`、`output_tokens`、`cached_tokens`、`reasoning_tokens`、`total_tokens`。其中 `input_tokens` 统一表示包含缓存读写的总输入,`cached_tokens` 单独表示缓存读取量。`metrics.series` 的每分钟点也包含这些 Token 字段和 `usage_reported`。普通 JSON、同协议 SSE 与跨协议 SSE 响应都会解析上游 usage;`coverage` 是收到 usage 的推理请求数除以已建立上游路由的推理请求数。上游未提供 usage 时不会估算 Token。
每个 `upstream.recent` 项包含时间、Request ID、模型、Tier、尝试序号、匿名标记、通道、Key 指纹、`proxy_node`、HTTP 状态、耗时、成功标记和结果分类。匿名请求的 Key ID 固定显示为 `anonymous`;真实 Key 只显示 SHA-256 稳定短指纹。代理 URL 的认证信息会被移除,字段名称明确为代理节点而非出口 IP。
+34 -15
View File
@@ -66,11 +66,15 @@ type bridgeRequest struct {
}
type bridgeUsage struct {
Input int
Output int
Total int
Cached int
Reasoning int
// Input is the total prompt input, including cache reads and writes. This
// matches OpenAI's prompt_tokens semantics and lets each output encoder
// split the provider-specific cache fields exactly once.
Input int
Output int
Total int
Cached int
CacheCreation int
Reasoning int
}
type bridgeResponse struct {
@@ -1351,11 +1355,7 @@ func encodeBridgeResponse(protocol Protocol, response bridgeResponse) map[string
"content": content,
"stop_reason": anthropicStop(response.Stop),
"stop_sequence": nil,
"usage": map[string]any{
"input_tokens": response.Usage.Input,
"output_tokens": response.Usage.Output,
"cache_read_input_tokens": response.Usage.Cached,
},
"usage": anthropicUsage(response.Usage),
}
default:
return map[string]any{}
@@ -1370,18 +1370,28 @@ func decodeOpenAIUsage(usage map[string]any) bridgeUsage {
total = input + output
}
cached := firstNonZero(intAt(usage, "prompt_tokens_details", "cached_tokens"), intAt(usage, "input_tokens_details", "cached_tokens"))
cacheCreation := firstNonZero(
intAt(usage, "cache_creation_input_tokens"),
intAt(usage, "prompt_tokens_details", "cache_creation_input_tokens"),
intAt(usage, "prompt_tokens_details", "cache_write_tokens"),
intAt(usage, "input_tokens_details", "cache_creation_input_tokens"),
)
reasoning := firstNonZero(intAt(usage, "completion_tokens_details", "reasoning_tokens"), intAt(usage, "output_tokens_details", "reasoning_tokens"))
return bridgeUsage{Input: input, Output: output, Total: total, Cached: cached, Reasoning: reasoning}
return bridgeUsage{Input: input, Output: output, Total: total, Cached: cached, CacheCreation: cacheCreation, Reasoning: reasoning}
}
func decodeAnthropicUsage(usage map[string]any) bridgeUsage {
input := intAt(usage, "input_tokens")
cached := intAt(usage, "cache_read_input_tokens")
cacheCreation := intAt(usage, "cache_creation_input_tokens")
output := intAt(usage, "output_tokens")
input += cached + cacheCreation
return bridgeUsage{
Input: input,
Output: output,
Total: input + output,
Cached: intAt(usage, "cache_read_input_tokens"),
Input: input,
Output: output,
Total: input + output,
Cached: cached,
CacheCreation: cacheCreation,
}
}
@@ -1399,6 +1409,15 @@ func openAIUsage(usage bridgeUsage) map[string]any {
}
}
func anthropicUsage(usage bridgeUsage) map[string]any {
return map[string]any{
"input_tokens": max(usage.Input-usage.Cached-usage.CacheCreation, 0),
"output_tokens": usage.Output,
"cache_creation_input_tokens": usage.CacheCreation,
"cache_read_input_tokens": usage.Cached,
}
}
func firstNonZero(values ...int) int {
for _, value := range values {
if value != 0 {
+5 -2
View File
@@ -608,7 +608,7 @@ func (emitter *bridgeStreamEmitter) start() error {
"content": []any{},
"stop_reason": nil,
"stop_sequence": nil,
"usage": map[string]any{"input_tokens": 0, "output_tokens": 0},
"usage": anthropicUsage(bridgeUsage{}),
},
})
case ProtocolResponses:
@@ -890,7 +890,7 @@ func (emitter *bridgeStreamEmitter) Finish() error {
if err := emitter.sse("message_delta", map[string]any{
"type": "message_delta",
"delta": map[string]any{"stop_reason": anthropicStop(emitter.stop), "stop_sequence": nil},
"usage": map[string]any{"input_tokens": emitter.usage.Input, "output_tokens": emitter.usage.Output},
"usage": anthropicUsage(emitter.usage),
}); err != nil {
return err
}
@@ -1026,6 +1026,9 @@ func mergeBridgeUsage(destination *bridgeUsage, source bridgeUsage) {
if source.Cached != 0 {
destination.Cached = source.Cached
}
if source.CacheCreation != 0 {
destination.CacheCreation = source.CacheCreation
}
if source.Reasoning != 0 {
destination.Reasoning = source.Reasoning
}