From 25e6688af6ead61f2e9ed491b098733ea756e2ba Mon Sep 17 00:00:00 2001 From: wucm667 Date: Tue, 14 Jul 2026 22:31:39 +0800 Subject: [PATCH] [verified] fix: apply long-context pricing to account cost --- .../internal/service/account_stats_pricing.go | 7 +++++++ .../service/account_stats_pricing_test.go | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/backend/internal/service/account_stats_pricing.go b/backend/internal/service/account_stats_pricing.go index 221021d85c..1098fff9e5 100644 --- a/backend/internal/service/account_stats_pricing.go +++ b/backend/internal/service/account_stats_pricing.go @@ -65,6 +65,13 @@ func tryModelFilePricing(billingService *BillingService, model string, tokens Us if err != nil || pricing == nil { return nil } + if billingService.shouldApplySessionLongContextPricing(tokens, pricing) { + breakdown, err := billingService.CalculateCost(model, tokens, 1) + 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 + diff --git a/backend/internal/service/account_stats_pricing_test.go b/backend/internal/service/account_stats_pricing_test.go index 36e5eb7400..57ace01c22 100644 --- a/backend/internal/service/account_stats_pricing_test.go +++ b/backend/internal/service/account_stats_pricing_test.go @@ -459,6 +459,26 @@ func TestTryModelFilePricing_Success(t *testing.T) { require.InDelta(t, 0.2, *result, 1e-12) } +func TestTryModelFilePricing_AppliesLongContextPricing(t *testing.T) { + bs := newTestBillingServiceWithPrices(map[string]*ModelPricing{ + "gpt-5.6-sol": { + InputPricePerToken: 0.001, + OutputPricePerToken: 0.002, + CacheReadPricePerToken: 0.0001, + LongContextInputThreshold: 100, + LongContextInputMultiplier: 2, + LongContextOutputMultiplier: 1.5, + }, + }) + tokens := UsageTokens{InputTokens: 101, OutputTokens: 10, CacheReadTokens: 5} + + result := tryModelFilePricing(bs, "gpt-5.6-sol", tokens) + + require.NotNil(t, result) + // Input and cache-read use the 2x input tier; output uses the 1.5x tier. + require.InDelta(t, 0.233, *result, 1e-12) +} + func TestTryModelFilePricing_PricingNotFound(t *testing.T) { // "nonexistent-model" does not match any fallback pattern bs := newTestBillingServiceWithPrices(map[string]*ModelPricing{})