Merge pull request #8003 from Yukinoshita-Yukino/codex/fix-deepseek-pro-pricing

fix(billing): retain DeepSeek V4 Pro pricing after September 14
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-10-27 20:42:28 +08:00
commit 3f1ae7ee76
2 files changed
+34 -58

No files matched your search

+12 -35
View File
@@ -267,9 +267,8 @@ var ErrModelPricingUnavailable = errors.New("pricing not found")
// ---- DeepSeek 官方低谷价($/token)----
// 2026-09-10 官方公告:DeepSeek-V4.1-Flash(新名 deepseek-flash)大幅降价,
// Flash 低谷价降为 $0.15/$0.60/$0.003 per MTok(输入缓存未命中/输出/缓存命中);
// deepseek-v4-pro 名义价格暂不变,但自北京时间 2026-09-14 12:00(04:00 UTC)起
// 其请求被上游路由到 V4.1-Flash 并按 Flash 价计费(见 deepseekProBilledAsFlash)。
// Source: https://api-docs.deepseek.com/news/news260910
// 官方后续确认 2026-09-14 后继续提供 deepseek-v4-pro,计费方式保持不变。
// Source: https://api-docs.deepseek.com/updates/
//
// https://api-docs.deepseek.com/quick_start/pricing
//
@@ -311,21 +310,6 @@ func deepseekPeakMultiplierAt(now time.Time) float64 {
return 1.0
}
// deepseekProRoutesToFlashAt:官方公告自北京时间 2026-09-14 12:00(04:00 UTC)起,
// 所有 deepseek-v4-pro 请求被上游路由到 V4.1-Flash 并按 Flash 价计费(直至未来
// V4.1 Pro 上线)。Source: https://api-docs.deepseek.com/news/news260910
var deepseekProRoutesToFlashAt = time.Date(2026, 9, 14, 4, 0, 0, 0, time.UTC)
// deepseekProBilledAsFlash 报告指定计费时点 deepseek-v4-pro 是否已按 Flash 价
// 计费:计费时点到达或晚于切换时点返回 true;零值时点回退当前时刻(与峰谷
// 倍率的取时点方式一致,见 calculateTokenCost)。
func deepseekProBilledAsFlash(pricingAt time.Time) bool {
if pricingAt.IsZero() {
pricingAt = timezone.Now()
}
return !pricingAt.Before(deepseekProRoutesToFlashAt)
}
// isDeepSeekProModel 判断模型名是否归入 deepseek-v4-pro 档(含版本化名称,
// 如 deepseek-v4-pro-0813)。
func isDeepSeekProModel(model string) bool {
@@ -1400,7 +1384,6 @@ func (s *BillingService) HasIdentifiedTokenPricing(model string) bool {
// GetModelPricing 获取模型价格配置
func (s *BillingService) GetModelPricing(model string) (*ModelPricing, error) {
// 无显式计费时点,DeepSeek pro→Flash 切换按当前时刻判定。
return s.getModelPricingAt(model, timezone.Now())
}
@@ -1416,9 +1399,8 @@ func (s *BillingService) CacheStoragePricePerTokenHour(model string) float64 {
return builtinGeminiCacheStoragePrice(model)
}
// getModelPricingAt 是 GetModelPricing 的带计费时点内部变体:pricingAt 显式
// 驱动 DeepSeek pro→Flash 切换判定(切换点前 Pro 价、之后 Flash 价),使
// 展示/估算路径可与历史补账同刻复算,测试也能用固定时点钉住断言。
// getModelPricingAt 保留带计费时点的内部调用接口。DeepSeek 默认低谷价
// 仅由模型决定,峰谷倍率由统一计费入口按请求时点处理。
func (s *BillingService) getModelPricingAt(model string, pricingAt time.Time) (*ModelPricing, error) {
// 标准化模型名称(转小写)
model = strings.ToLower(model)
@@ -1645,8 +1627,7 @@ func (s *BillingService) calculateTokenCost(resolved *ResolvedPricing, input Cos
return nil, fmt.Errorf("no pricing available for model: %s: %w", input.Model, ErrModelPricingUnavailable)
}
// 计费时点:优先请求级 PricingAt(历史补账与 DeepSeek pro→Flash 切换判定
// 同源),零值回退当前时刻。
// 峰谷计费时点:优先请求级 PricingAt,零值回退当前时刻。
pricingAt := input.PricingAt
if pricingAt.IsZero() {
pricingAt = timezone.Now()
@@ -1922,17 +1903,15 @@ func (s *BillingService) calculateCostInternalWithPolicy(
// 强制覆盖;GPT-5.6 缺 cache_write 价时按官方规则补 1.25 倍输入价;Fast/priority
// 档按业务倍率改写(本地/远程目录的 priority 价可能沿用官方旧口径)。长上下文
// 阶梯不在此处补齐:一律由目录数据(above_XXXk 折算或显式 long_context_* 字段)
// 驱动。强制 DeepSeek 官方价且无显式计费时点(pro→Flash 切换按当前时刻判定),
// 供无既有时点的策略修正场景与测试使用;计费/展示主路径分别经
// calculateTokenCost 与 getModelPricingAt 显式传时点,分组/渠道自定义定价
// 用 applyModelSpecificPricingPolicyEx 关闭强制,保留运营者配置。
// 驱动。分组/渠道自定义定价用 applyModelSpecificPricingPolicyEx 关闭
// DeepSeek 官方价强制覆盖,保留运营者配置。
func (s *BillingService) applyModelSpecificPricingPolicy(model string, pricing *ModelPricing) *ModelPricing {
return s.applyModelSpecificPricingPolicyEx(model, pricing, true, time.Time{})
}
// applyModelSpecificPricingPolicyEx 与 applyModelSpecificPricingPolicy 相同,
// 但由调用方控制是否强制 DeepSeek 官方价(forceDeepSeekRates),并显式传入
// 计费时点 pricingAt(零值表示按当前时刻判定)。
// 但由调用方控制是否强制 DeepSeek 官方价(forceDeepSeekRates)。
// 保留计费时点参数以兼容现有调用;模型价卡不再按日期切换。
// calculateTokenCost 对分组/渠道自定义定价(Source 非 LiteLLM)传 false:
// 强制覆盖会把运营者配置的售价盖回官方价,违反自定义定价语义。
func (s *BillingService) applyModelSpecificPricingPolicyEx(model string, pricing *ModelPricing, forceDeepSeekRates bool, pricingAt time.Time) *ModelPricing {
@@ -1945,20 +1924,18 @@ func (s *BillingService) applyModelSpecificPricingPolicyEx(model string, pricing
// 共享 fallbackPrices 指针。
// 档位判定:含 "deepseek-v4-pro" 的版本化名称(如 deepseek-v4-pro-0813)归 pro 档,
// 其余 deepseek-*(含已停服的 chat/reasoner 与未知型号)统一归 flash 档。
// 2026-09-14 04:00 UTC 起上游把 pro 请求路由到 V4.1-Flash,pro 档改按
// Flash 三档价计费;历史时点(早于切换时刻)仍按 Pro 价。
// 官方后续保留了 Pro 服务及其计费方式,Pro 不按日期切换成 Flash 价。
// 高峰时段倍率不在本函数处理,由 calculateTokenCost 按 deepseekPeakMultiplierAt
// 对默认价卡另行叠加(分组/渠道自定义定价不叠加)。
if forceDeepSeekRates && isDeepSeekModel(model) {
cloned := *pricing
if isDeepSeekProModel(model) && !deepseekProBilledAsFlash(pricingAt) {
if isDeepSeekProModel(model) {
cloned.InputPricePerToken = deepseekProOffPeakInputPrice
cloned.OutputPricePerToken = deepseekProOffPeakOutputPrice
cloned.CacheReadPricePerToken = deepseekProOffPeakCacheRead
} else {
// deepseek-flash(= V4.1-Flash)、deepseek-v4-flash /
// deepseek-v4-flash-vision-exp 与其余 deepseek-* 共用 flash 价;
// 切换时点之后的 pro 请求同样按 flash 价计费。
// deepseek-v4-flash-vision-exp 与其余 deepseek-* 共用 flash 价。
cloned.InputPricePerToken = deepseekFlashOffPeakInputPrice
cloned.OutputPricePerToken = deepseekFlashOffPeakOutputPrice
cloned.CacheReadPricePerToken = deepseekFlashOffPeakCacheRead
@@ -262,13 +262,13 @@ func TestGetModelPricing_DeepseekForcesOfficialRatesOverJSON(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.model, func(t *testing.T) {
// flash 档三档价与 pro→Flash 切换无关,GetModelPricing 断言不随时间翻转。
// Flash 默认价格不随日期切换。
pricing, err := bs.GetModelPricing(tt.model)
require.NoError(t, err)
require.InDelta(t, tt.input, pricing.InputPricePerToken, 1e-15)
require.InDelta(t, tt.output, pricing.OutputPricePerToken, 1e-15)
require.InDelta(t, tt.cacheRead, pricing.CacheReadPricePerToken, 1e-15)
// 固定时点(切换点之后的 2026-10-01)复核:仍走 Flash 新价。
// 固定时点 2026-10-01 复核:仍走 Flash 新价。
atPricing, err := bs.getModelPricingAt(tt.model, time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC))
require.NoError(t, err)
require.InDelta(t, tt.input, atPricing.InputPricePerToken, 1e-15)
@@ -279,10 +279,10 @@ func TestGetModelPricing_DeepseekForcesOfficialRatesOverJSON(t *testing.T) {
}
// pro 档(含版本化名称):断言经固定时点的 getModelPricingAt,不依赖墙上时钟。
// 2026-08-01 早于切换点 2026-09-14 04:00 UTC → Pro 价。
// 官方确认 2026-09-14 后继续提供 Pro,计费方式保持不变。
for _, model := range []string{"deepseek-v4-pro", "deepseek-v4-pro-0813"} {
t.Run(model+"/before-cutoff", func(t *testing.T) {
pricing, err := bs.getModelPricingAt(model, time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC))
t.Run(model+"/after-september-14", func(t *testing.T) {
pricing, err := bs.getModelPricingAt(model, time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC))
require.NoError(t, err)
require.InDelta(t, 6.6e-7, pricing.InputPricePerToken, 1e-15)
require.InDelta(t, 1.98e-6, pricing.OutputPricePerToken, 1e-15)
@@ -290,7 +290,7 @@ func TestGetModelPricing_DeepseekForcesOfficialRatesOverJSON(t *testing.T) {
})
}
// 半开边界钉死:2026-09-14 03:59:59 仍 Pro 价,04:00:00 整起 Flash 新价。
// 原计划切换时点前后均保持 Pro 价。
proBefore, err := bs.getModelPricingAt("deepseek-v4-pro", time.Date(2026, 9, 14, 3, 59, 59, 0, time.UTC))
require.NoError(t, err)
require.InDelta(t, 6.6e-7, proBefore.InputPricePerToken, 1e-15)
@@ -298,12 +298,12 @@ func TestGetModelPricing_DeepseekForcesOfficialRatesOverJSON(t *testing.T) {
require.InDelta(t, 2.2e-8, proBefore.CacheReadPricePerToken, 1e-15)
proAtCutoff, err := bs.getModelPricingAt("deepseek-v4-pro", time.Date(2026, 9, 14, 4, 0, 0, 0, time.UTC))
require.NoError(t, err)
require.InDelta(t, 1.5e-7, proAtCutoff.InputPricePerToken, 1e-15)
require.InDelta(t, 6e-7, proAtCutoff.OutputPricePerToken, 1e-15)
require.InDelta(t, 3e-9, proAtCutoff.CacheReadPricePerToken, 1e-15)
require.InDelta(t, 6.6e-7, proAtCutoff.InputPricePerToken, 1e-15)
require.InDelta(t, 1.98e-6, proAtCutoff.OutputPricePerToken, 1e-15)
require.InDelta(t, 2.2e-8, proAtCutoff.CacheReadPricePerToken, 1e-15)
// 版本化名称(不在 JSON / fallbackPrices 精确表中):按子串归档计价。
// flash-0731 归 flash 档,三档价与切换无关,GetModelPricing 断言稳定。
// flash-0731 归 flash 档,GetModelPricing 断言稳定。
versioned := []struct {
model string
input, output, cacheRead float64
@@ -381,7 +381,7 @@ func TestDeepseekPricingFileMatchesOfficialRates(t *testing.T) {
// ---------------------------------------------------------------------------
// 2026-09-10 官方降价:deepseek-flash(V4.1-Flash 新名)与旧名同价;
// 2026-09-14 04:00 UTC 起 deepseek-v4-pro 按上游路由改按 Flash 价计费
// 官方确认 2026-09-14 后 deepseek-v4-pro 继续按 Pro 价计费
// ---------------------------------------------------------------------------
func TestCalculateCostUnified_DeepseekFlashAndLegacyFlashShareNewRates(t *testing.T) {
@@ -393,7 +393,7 @@ func TestCalculateCostUnified_DeepseekFlashAndLegacyFlashShareNewRates(t *testin
offPeakTotal := 1000*1.5e-7 + 500*6e-7 + 1000*3e-9
// deepseek-flash 与 deepseek-v4-flash 都取 Flash 新价。
// 时点取切换日 2026-09-14(周一)12:00 UTC 低谷,峰谷倍率不影响断言。
// 时点取 2026-09-14(周一)12:00 UTC 低谷,峰谷倍率不影响断言。
for _, model := range []string{"deepseek-flash", "deepseek-v4-flash"} {
cost, err := bs.CalculateCostUnified(CostInput{
Ctx: context.Background(), Model: model, Tokens: tokens,
@@ -405,15 +405,14 @@ func TestCalculateCostUnified_DeepseekFlashAndLegacyFlashShareNewRates(t *testin
}
}
func TestCalculateCostUnified_DeepseekProRoutesToFlashAtCutoff(t *testing.T) {
func TestCalculateCostUnified_DeepseekProRetainsRatesAfterSeptember14(t *testing.T) {
bs := newTestBillingService()
resolver := NewModelPricingResolver(nil, bs)
tokens := UsageTokens{InputTokens: 1000, OutputTokens: 500, CacheReadTokens: 1000}
proTotal := 1000*6.6e-7 + 500*1.98e-6 + 1000*2.2e-8 // 切换前 Pro 价
flashTotal := 1000*1.5e-7 + 500*6e-7 + 1000*3e-9 // 切换后 Flash 价
proTotal := 1000*6.6e-7 + 500*1.98e-6 + 1000*2.2e-8
// 切换时点之前(2026-09-13 周日,北京周末全天低谷):仍按 Pro 价。
// 原计划切换时点之前(2026-09-13 周日,北京周末全天低谷):按 Pro 价。
before, err := bs.CalculateCostUnified(CostInput{
Ctx: context.Background(), Model: "deepseek-v4-pro", Tokens: tokens,
RateMultiplier: 1.0, Resolver: resolver,
@@ -421,25 +420,25 @@ func TestCalculateCostUnified_DeepseekProRoutesToFlashAtCutoff(t *testing.T) {
})
require.NoError(t, err)
require.InDelta(t, proTotal, before.TotalCost, 1e-10,
"deepseek-v4-pro must use Pro rates before the 2026-09-14 04:00 UTC cutoff")
"deepseek-v4-pro must use Pro rates before September 14")
// 到达切换时点(2026-09-14 04:00 UTC 整,周一低谷窗口边界外):按 Flash 价。
// 到达原计划切换时点(2026-09-14 04:00 UTC 整,周一低谷):仍按 Pro 价。
after, err := bs.CalculateCostUnified(CostInput{
Ctx: context.Background(), Model: "deepseek-v4-pro", Tokens: tokens,
RateMultiplier: 1.0, Resolver: resolver,
PricingAt: time.Date(2026, 9, 14, 4, 0, 0, 0, time.UTC),
})
require.NoError(t, err)
require.InDelta(t, flashTotal, after.TotalCost, 1e-10,
"deepseek-v4-pro must use Flash rates at/after the 2026-09-14 04:00 UTC cutoff")
require.InDelta(t, proTotal, after.TotalCost, 1e-10,
"deepseek-v4-pro must retain Pro rates on September 14")
// 版本化名称同口径:切换后 deepseek-v4-pro-0813 也按 Flash 价。
// 版本化名称同口径:deepseek-v4-pro-0813 也保持 Pro 价。
versioned, err := bs.CalculateCostUnified(CostInput{
Ctx: context.Background(), Model: "deepseek-v4-pro-0813", Tokens: tokens,
RateMultiplier: 1.0, Resolver: resolver,
PricingAt: time.Date(2026, 9, 14, 4, 0, 0, 0, time.UTC),
})
require.NoError(t, err)
require.InDelta(t, flashTotal, versioned.TotalCost, 1e-10,
"versioned pro names must also route to Flash rates after the cutoff")
require.InDelta(t, proTotal, versioned.TotalCost, 1e-10,
"versioned pro names must retain Pro rates on September 14")
}