fix: cost ledger charges fresh input only (cache read is part of input tokens)

input_tokens includes the cached subset under Anthropic and OpenAI usage semantics, so pricing input and cache read at their full prices double-counted the cache. The fresh share is now input minus cache reads, clamped at zero for gateways reporting disjoint buckets.
This commit is contained in:
BaaRai
2026-10-12 08:53:20 +02:00
parent e36d0730da
commit c96c3ea3a9
2 changed files with 10 additions and 2 deletions
+9 -1
View File
@@ -371,7 +371,15 @@ func applyCostEur(d *DB, out []ProfileUsage) error {
if !ok || (pr.in == 0 && pr.outp == 0 && pr.cr == 0 && pr.cw == 0) {
continue
}
cost := float64(out[i].InputTokens)/1e6*pr.in + float64(out[i].OutputTokens)/1e6*pr.outp +
// InputTokens includes the cached subset (Anthropic and OpenAI usage
// semantics), so the fresh share is input minus cache reads; charging
// both buckets at full input price double-counts the cache. Clamped at
// zero for gateways that report the buckets disjointly.
fresh := out[i].InputTokens - out[i].CacheReadTokens
if fresh < 0 {
fresh = 0
}
cost := float64(fresh)/1e6*pr.in + float64(out[i].OutputTokens)/1e6*pr.outp +
float64(out[i].CacheReadTokens)/1e6*pr.cr + float64(out[i].CacheWriteTokens)/1e6*pr.cw
c := cost
out[i].CostEur = &c
+1 -1
View File
@@ -54,7 +54,7 @@ func TestCostLedgerEurOnUsage(t *testing.T) {
if got == nil {
t.Fatal("priced profile has no cost_eur")
}
want := 2.0*1.5 + 0.5*4.0 + 1.0*0.15 // 3.0 + 2.0 + 0.15 = 5.15
want := 1.0*1.5 + 0.5*4.0 + 1.0*0.15 // fresh input 1M (2M incl. 1M cached) + 0.5M out + 1M cache-read = 3.65
if d := *got - want; d > 0.001 || d < -0.001 {
t.Fatalf("cost_eur = %v, want %v", *got, want)
}