mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(pricing): fill flagship cache prices from the generated table
A curated builtinPricing entry returned before the generated table, so its cache prices were never reached. lookupPricingSource now fills nil cache fields of a curated hit from the generated table. Base prices are unchanged, and an operator override is not merged. The clamp test now asserts exact values. Tests that compared prices with == now use reflect.DeepEqual, because the price struct holds pointers.
This commit is contained in:
+2
-1
@@ -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
|
||||
|
||||
+24
-8
@@ -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
|
||||
|
||||
+36
-10
@@ -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)
|
||||
}
|
||||
|
||||
+4
-3
@@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user