Skip to content

fix(billing): apply model pricing policies to account stats cost - #6394

Merged
Wei-Shaw merged 2 commits into
Wei-Shaw:mainfrom
qiujun8023:fix/account-stats-deepseek-peak
Sep 1, 2026
Merged

fix(billing): apply model pricing policies to account stats cost#6394
Wei-Shaw merged 2 commits into
Wei-Shaw:mainfrom
qiujun8023:fix/account-stats-deepseek-peak

Conversation

@qiujun8023

@qiujun8023 qiujun8023 commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Problem

For DeepSeek traffic served during the official peak window (01:00–04:00 and 06:00–10:00 UTC, weekdays only), usage_logs.account_stats_cost is recorded at off-peak rates and understates the real cost by up to 2x.

User-facing billing is unaffected — it goes through calculateTokenCost, where the peak multiplier is applied. Only account cost statistics and the dashboard's account_cost are wrong.

Root cause

Priority 3 of resolveAccountStatsCost (model pricing file) keeps its own simplified unit price × tokens implementation in tryModelFilePricing instead of reusing the billing pipeline:

cost := float64(tokens.InputTokens)*pricing.InputPricePerToken +
    float64(tokens.OutputTokens)*pricing.OutputPricePerToken + ...

Every pricing feature added on the billing side therefore has to be mirrored here by hand, and this has already been patched twice for exactly that reason — 25e6688a for long-context pricing, 9261dd77 for service tiers. The DeepSeek peak/off-peak rates introduced in b5827cfd live inside calculateTokenCost, and this raw-multiplication path never reaches that function, so they have never taken effect for account cost.

Fix

Route the branch through CalculateCostWithServiceTier and drop the duplicated implementation.

channelPricing stays nil, so priority-3 semantics are unchanged: model-file pricing only, no channel overrides. Off-peak results are identical to the previous arithmetic — with no interval pricing and no extra policies, calculateTokenCost reduces to the same multiplication. Returning nil for missing pricing or zero cost is preserved through the err and TotalCost <= 0 checks.

Verification

  • go build ./... passes.
  • go vet ./internal/service/ reports no issues.
  • go test ./internal/service/ passes for the whole package (124s), including the existing deepseek_pricing_test.go and account_stats_pricing_test.go cases.
  • gofmt reports no diff.

I did not run the backend integration tests, frontend suite, vulnerability scan or deployment checks — this touches a single backend file and no frontend or deployment configuration.

Tests

I did not add a regression test, and I want to be upfront about why. tryModelFilePricing takes no billing timestamp, so the peak multiplier falls back to time.Now(). Off-peak runs produce identical results before and after this change, which means any test I write would pass against the broken code for most of the day. Making it deterministic requires threading a pricing timestamp through this path, which grows the diff well beyond this one file. Happy to do that if you would rather the fix land with a test.

Searched open PRs before submitting and found nothing else fixing account cost resolution or DeepSeek peak pricing. #6353 (long-context tier pricing moving to a data-driven catalogue) is in the same billing area but changes billing_service.go, so there is no file overlap.

🤖 Generated with Claude Code

qiujun8023 and others added 2 commits August 30, 2026 22:57
resolveAccountStatsCost 的优先级 3 维护了第二份"单价 × token 数"实现,每加一个
定价特性都要手工镜像:service tier 与长上下文分别由 9261dd7、25e6688a 补齐,
而 b5827cf 引入的 DeepSeek 官方峰谷一直没有生效,高峰时段账号成本最多低估一半。

改为复用 CalculateCostWithServiceTier。channelPricing 仍为 nil,优先级 3 的语义
不变:只取模型定价文件、不引入渠道自定义定价;低谷时段结果与原算式完全一致。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
上一条改用统一定价管线后,TestTryModelFilePricing_WithImageOutput 挂在 0.3 vs
0.28:原算式把 ImageOutputTokens 当 OutputTokens 之外的额外量叠加,等于按文本
output 价和图片价各收一次,测试固化了这个重复计费。

ImageOutputTokens 本就是 OutputTokens 的子集(Gemini 取自 candidatesTokensDetails
的 IMAGE 档,OpenAI 取自 output_tokens_details),统一管线先扣除再按图片单价计,
未配图片价时回退到 output 价、总额不变,正是这个语义。期望改为 0.28。

影响:涉及图片输出的模型,账号统计成本会降低"图片 token × 文本 output 单价"。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Wei-Shaw
Wei-Shaw merged commit f218c8d into Wei-Shaw:main Sep 1, 2026
8 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants