diff --git a/failover_test.go b/failover_test.go index 5293067..78dee97 100644 --- a/failover_test.go +++ b/failover_test.go @@ -716,7 +716,8 @@ func TestResolveUnifiedPricing_BuiltinTable(t *testing.T) { t.Parallel() p, ok := resolveUnifiedPricing("openai/gpt-5", "gpt-5", nil) require.True(t, ok, "the built-in pricing table must still resolve with no operator overrides configured") - assert.Equal(t, builtinPricing["gpt-5"], p) + assert.Equal(t, builtinPricing["gpt-5"].InputPerM, p.InputPerM) + assert.Equal(t, builtinPricing["gpt-5"].OutputPerM, p.OutputPerM) } // TestCandidateCostAllowed is the direct regression test for the diff --git a/pricing.go b/pricing.go index 791785d..0b79c06 100644 --- a/pricing.go +++ b/pricing.go @@ -153,6 +153,20 @@ func promptCacheSavedMicros(price ModelPricing, u usage) int64 { return roundedDivInt64(saturatingMulInt64(cached, diff), tokensPerMillion) } +// builtinCachePrices converts a table entry's cache prices; nil when unknown. +func builtinCachePrices(m builtinModelMeta) (*float64, *float64) { + var cached, write *float64 + if m.CachedInputCostPerMTokMicroUSD > 0 { + v := microUSDPerMTokToUSD(m.CachedInputCostPerMTokMicroUSD) + cached = &v + } + if m.CacheWriteCostPerMTokMicroUSD > 0 { + v := microUSDPerMTokToUSD(m.CacheWriteCostPerMTokMicroUSD) + write = &v + } + return cached, write +} + func cacheRateMicrosPerM(perM *float64, fallbackMicrosPerM int64) int64 { if perM == nil { return fallbackMicrosPerM @@ -284,6 +298,15 @@ func lookupPricingSource(model string, overrides map[string]*ModelPricing) (pric return *p, priceSourceOverride, true } if p, ok := builtinPricing[model]; ok { + if m, found := builtinModelMetaTable[model]; found { + cached, write := builtinCachePrices(m) + if p.CachedInputPerM == nil { + p.CachedInputPerM = cached + } + if p.CacheWritePerM == nil { + p.CacheWritePerM = write + } + } return p, priceSourceBuiltin, true } if m, ok := builtinModelMetaTable[model]; ok && (m.InputCostPerMTokMicroUSD > 0 || m.OutputCostPerMTokMicroUSD > 0) { @@ -291,14 +314,7 @@ func lookupPricingSource(model string, overrides map[string]*ModelPricing) (pric InputPerM: microUSDPerMTokToUSD(m.InputCostPerMTokMicroUSD), OutputPerM: microUSDPerMTokToUSD(m.OutputCostPerMTokMicroUSD), } - if m.CachedInputCostPerMTokMicroUSD > 0 { - v := microUSDPerMTokToUSD(m.CachedInputCostPerMTokMicroUSD) - p.CachedInputPerM = &v - } - if m.CacheWriteCostPerMTokMicroUSD > 0 { - v := microUSDPerMTokToUSD(m.CacheWriteCostPerMTokMicroUSD) - p.CacheWritePerM = &v - } + p.CachedInputPerM, p.CacheWritePerM = builtinCachePrices(m) return p, priceSourceLitellm, true } return ModelPricing{}, "", false diff --git a/pricing_cache_test.go b/pricing_cache_test.go index 383accb..34ad853 100644 --- a/pricing_cache_test.go +++ b/pricing_cache_test.go @@ -30,17 +30,21 @@ func TestComputeCostMicrosCachedClamp(t *testing.T) { t.Parallel() p := ModelPricing{InputPerM: 3, OutputPerM: 15, CachedInputPerM: f64(0.3), CacheWritePerM: f64(3.75)} uncached := computeCostMicros(ModelPricing{InputPerM: 3, OutputPerM: 15}, usage{prompt: 1_000_000}) - for _, u := range []usage{ - {prompt: 1_000_000, cached: 2_000_000}, - {prompt: 1_000_000, cached: 800_000, cacheWrite: 800_000}, - {prompt: 1_000_000, cached: -5, cacheWrite: -5}, - } { - got := computeCostMicros(p, u) - require.GreaterOrEqual(t, got, int64(0)) - require.LessOrEqual(t, got, uncached*2) + cases := []struct { + name string + u usage + want int64 + }{ + // 1M prompt priced entirely at the cached rate. + {"cached above prompt", usage{prompt: 1_000_000, cached: 2_000_000}, 300_000}, + // cached 800k at 0.3 + write clamped to 200k at 3.75. + {"write clamped to remainder", usage{prompt: 1_000_000, cached: 800_000, cacheWrite: 800_000}, 240_000 + 750_000}, + // negatives clamp to 0: whole prompt uncached at 3. + {"negative cache counts", usage{prompt: 1_000_000, cached: -5, cacheWrite: -5}, uncached}, + } + for _, c := range cases { + require.EqualValues(t, c.want, computeCostMicros(p, c.u), c.name) } - // cached clamped to prompt: all tokens priced at the cached rate. - require.EqualValues(t, 300_000, computeCostMicros(p, usage{prompt: 1_000_000, cached: 2_000_000})) } func TestPromptCacheSavedMicros(t *testing.T) { @@ -84,3 +88,25 @@ func TestBuiltinCachedPriceFlowsToLookup(t *testing.T) { require.Nil(t, p.CachedInputPerM) require.Nil(t, p.CacheWritePerM) } + +func TestBuiltinPricingGetsGeneratedCachePrices(t *testing.T) { + // Mutates package-global tables, so no t.Parallel. + const id = "claude-sonnet-4-5" + _, curated := builtinPricing[id] + require.True(t, curated) + + p, src, ok := lookupPricingSource(id, nil) + require.True(t, ok) + require.Equal(t, priceSourceBuiltin, src) + require.NotNil(t, p.CachedInputPerM) + require.NotNil(t, p.CacheWritePerM) + require.Equal(t, builtinPricing[id].InputPerM, p.InputPerM) + require.Nil(t, builtinPricing[id].CachedInputPerM, "shared map entry must not be mutated") + + // An override stays as written, with no merge. + o := map[string]*ModelPricing{id: {InputPerM: 1, OutputPerM: 2}} + p, src, _ = lookupPricingSource(id, o) + require.Equal(t, priceSourceOverride, src) + require.Nil(t, p.CachedInputPerM) + require.Nil(t, p.CacheWritePerM) +} diff --git a/pricing_test.go b/pricing_test.go index 753d4e5..1eb2aa3 100644 --- a/pricing_test.go +++ b/pricing_test.go @@ -2,6 +2,7 @@ package traefikllmgateway import ( "math" + "reflect" "strconv" "sync" "testing" @@ -307,7 +308,7 @@ func TestLookupPricingSource_MatchesLookupPricing(t *testing.T) { t.Fatalf("lookupPricingSource(%q) = (%+v, %q, %v), want source=%q ok=%v", c.model, price, source, ok, c.wantSource, c.wantOK) } wantPrice, wantOK := lookupPricing(c.model, overrides) - if wantOK != ok || wantPrice != price { + if wantOK != ok || !reflect.DeepEqual(wantPrice, price) { t.Errorf("lookupPricing(%q) = (%+v, %v) disagrees with lookupPricingSource = (%+v, %v)", c.model, wantPrice, wantOK, price, ok) } }) @@ -341,7 +342,7 @@ func TestBillingPriceSource_Table(t *testing.T) { {"modelMeta free wins over everything", "providerA/free-model", "free-model", priceSourceFree, ModelPricing{}}, {"override on canonical id", "providerA/canonical-model", "canonical-model", priceSourceOverride, ModelPricing{InputPerM: 1, OutputPerM: 2}}, {"override on bare id when canonical has none", "providerB/bare-override-model", "bare-override-model", priceSourceOverride, ModelPricing{InputPerM: 3, OutputPerM: 4}}, - {"builtin table", "providerC/gpt-5", "gpt-5", priceSourceBuiltin, builtinPricing["gpt-5"]}, + {"builtin table", "providerC/gpt-5", "gpt-5", priceSourceBuiltin, mustLookupPrice(t, "gpt-5", nil)}, {"generated table fallback", "providerC/deepseek-chat", "deepseek-chat", priceSourceLitellm, mustLookupPrice(t, "deepseek-chat", nil)}, {"free-tier-suffixed id with no real price is unpriced, not free", "providerD/some-model:free", "some-model:free", priceSourceUnpriced, ModelPricing{}}, {"totally unknown model", "providerD/unknown-xyz", "unknown-xyz", priceSourceUnpriced, ModelPricing{}}, @@ -352,7 +353,7 @@ func TestBillingPriceSource_Table(t *testing.T) { if source != c.wantSource { t.Errorf("billingPriceSource(%q, %q) source = %q, want %q", c.canonical, c.bare, source, c.wantSource) } - if price != c.wantPrice { + if !reflect.DeepEqual(price, c.wantPrice) { t.Errorf("billingPriceSource(%q, %q) price = %+v, want %+v", c.canonical, c.bare, price, c.wantPrice) } })