Files
zeekay 1569f5288b usage: one prompt on every path, and cache writes billed as writes
prompt_tokens meant two things. The family and relay paths recorded the
part of the prompt read fresh, the table path and the providers the
whole prompt, so a cached enso-auto row read cache_read > prompt. No
path recorded a cache write: Anthropic's cache_creation_input_tokens
was folded into the fresh read and billed at the input rate, though a
write is priced above it, and OpenRouter's cache_write_tokens was not
read at all. cache_write_tokens was 0 on every row.

A record's prompt is now every input token, with the cache reads and
writes parts of it, whatever shape the upstream reported: OpenAI's
cached_tokens inside prompt_tokens, OpenRouter's cache_write_tokens
beside them, Anthropic's two counts beside input_tokens. Pricing bills
the input rate on the part read fresh and each cache part at its own
rate: a family SKU's cache_read and cache_write (a resold model's
input_cache_read and input_cache_write plus the vendor's fee), else the
input rate. The Anthropic provider and the OpenAI adapter over
Anthropic now read both cache counts, and the providers' settles pass
them.
2026-10-09 03:59:41 -07:00

63 lines
2.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"testing"
"github.com/hanzoai/decimal"
)
// A stated cost of zero or less never credits the caller: a variable SKU bills its
// ceiling.
func TestResaleIgnoresNonPositiveCost(t *testing.T) {
tier := zenTier{MaxCtx: 1 << 20, In: decimal.New(3, 0), Out: decimal.New(15, 0)}
m := zenModel{Margin: decimal.New(12, 1), Variable: true, Base: tier, Tiers: []zenTier{tier}}
ceiling := m.bill(nil, tokens{fresh: 1000, cached: 0, completion: 100})
if ceiling <= 0 {
t.Fatalf("ceiling = %d, want a positive charge", ceiling)
}
for _, c := range []int64{0, -600_000_000} {
c := c
if nano := m.bill(&c, tokens{fresh: 1000, cached: 0, completion: 100}); nano != ceiling {
t.Fatalf("cost %d: nano=%d, want the ceiling %d", c, nano, ceiling)
}
}
}
// A SKU we resell bills its tokens at retail, and never less than what the vendor
// says the call cost us times the margin: a cache write or a search the token rates
// do not price is still paid for. A SKU we do not resell bills its tokens alone.
func TestAResoldCallNeverBillsBelowItsStatedCost(t *testing.T) {
tier := zenTier{MaxCtx: 1 << 20, In: decimal.New(12, 2), Out: decimal.New(6, 1)} // $0.12 and $0.60 per MTok
m := zenModel{Margin: decimal.New(12, 1), Base: tier, Tiers: []zenTier{tier}}
retail := m.bill(nil, tokens{fresh: 1000, cached: 0, completion: 100}) // 1000 × 0.12e-6 + 100 × 0.6e-6 = $0.00018
if retail != 180_000 {
t.Fatalf("retail = %d nano, want 180000", retail)
}
under := int64(100_000) // the vendor charged $0.0001: retail covers it
if got := m.bill(&under, tokens{fresh: 1000, cached: 0, completion: 100}); got != retail {
t.Errorf("a cost retail covers billed %d, want retail %d", got, retail)
}
over := int64(200_000) // a cache write made it $0.0002: billed at 1.2 × that
if got := m.bill(&over, tokens{fresh: 1000, cached: 0, completion: 100}); got != 240_000 {
t.Errorf("a cost above retail billed %d, want 240000", got)
}
own := zenModel{Base: tier, Tiers: []zenTier{tier}}
if got := own.bill(&over, tokens{fresh: 1000, cached: 0, completion: 100}); got != retail {
t.Errorf("a SKU we do not resell billed %d, want its tokens %d", got, retail)
}
}