Merge pull request #6394 from qiujun8023/fix/account-stats-deepseek-peak

fix(billing): apply model pricing policies to account stats cost
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-09-01 08:58:49 +08:00
commit f218c8d40a
2 files changed
+11 -22

No files matched your search

@@ -62,29 +62,17 @@ func resolveAccountStatsCost(
}
// tryModelFilePricing 使用模型定价文件(LiteLLM/fallback)中的价格计算费用。
// 与用户计费共用同一条定价管线,避免这里维护第二份"单价 × token 数"实现后,
// 每加一个定价特性都要手工镜像一次。channelPricing 为 nil,保持优先级 3 的
// 语义:只取模型定价文件,不引入渠道自定义定价。
func tryModelFilePricing(billingService *BillingService, model string, tokens UsageTokens, serviceTier string) *float64 {
pricing, err := billingService.GetModelPricing(model)
if err != nil || pricing == nil {
breakdown, err := billingService.CalculateCostWithServiceTier(
model, tokens, 1, normalizeBillingServiceTier(serviceTier),
)
if err != nil || breakdown == nil || breakdown.TotalCost <= 0 {
return nil
}
normalizedTier := normalizeBillingServiceTier(serviceTier)
if normalizedTier == "priority" || normalizedTier == "fast" || normalizedTier == "flex" ||
billingService.shouldApplySessionLongContextPricing(tokens, pricing) {
breakdown, err := billingService.CalculateCostWithServiceTier(model, tokens, 1, normalizedTier)
if err != nil || breakdown == nil || breakdown.TotalCost <= 0 {
return nil
}
return &breakdown.TotalCost
}
cost := float64(tokens.InputTokens)*pricing.InputPricePerToken +
float64(tokens.OutputTokens)*pricing.OutputPricePerToken +
float64(tokens.CacheCreationTokens)*pricing.CacheCreationPricePerToken +
float64(tokens.CacheReadTokens)*pricing.CacheReadPricePerToken +
float64(tokens.ImageOutputTokens)*pricing.ImageOutputPricePerToken
if cost <= 0 {
return nil
}
return &cost
return &breakdown.TotalCost
}
// tryCustomRules 遍历自定义规则,按数组顺序先命中为准。
@@ -594,8 +594,9 @@ func TestTryModelFilePricing_WithImageOutput(t *testing.T) {
}
result := tryModelFilePricing(bs, "claude-sonnet-4", tokens, "")
require.NotNil(t, result)
// 100*0.001 + 50*0.002 + 10*0.01 = 0.1 + 0.1 + 0.1 = 0.3
require.InDelta(t, 0.3, *result, 1e-12)
// ImageOutputTokens 是 OutputTokens 的子集,先扣除再按图片单价计。
// 100*0.001 + (50-10)*0.002 + 10*0.01 = 0.1 + 0.08 + 0.1 = 0.28
require.InDelta(t, 0.28, *result, 1e-12)
}
func TestTryModelFilePricing_WithCacheTokens(t *testing.T) {