fix(billing): apply model pricing policies to account stats cost - #6394
Merged
Wei-Shaw merged 2 commits intoSep 1, 2026
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_costis 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'saccount_costare wrong.Root cause
Priority 3 of
resolveAccountStatsCost(model pricing file) keeps its own simplifiedunit price × tokensimplementation intryModelFilePricinginstead of reusing the billing pipeline: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 —
25e6688afor long-context pricing,9261dd77for service tiers. The DeepSeek peak/off-peak rates introduced inb5827cfdlive insidecalculateTokenCost, and this raw-multiplication path never reaches that function, so they have never taken effect for account cost.Fix
Route the branch through
CalculateCostWithServiceTierand drop the duplicated implementation.channelPricingstaysnil, 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,calculateTokenCostreduces to the same multiplication. Returningnilfor missing pricing or zero cost is preserved through theerrandTotalCost <= 0checks.Verification
go build ./...passes.go vet ./internal/service/reports no issues.go test ./internal/service/passes for the whole package (124s), including the existingdeepseek_pricing_test.goandaccount_stats_pricing_test.gocases.gofmtreports 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.
tryModelFilePricingtakes no billing timestamp, so the peak multiplier falls back totime.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