Files
magpie/internal/usage/tier_dated_test.go
T
yetone f1e2ada0f7 usage: GPT-6.2's Ultrafast is priced at OpenAI's 3x, then 6x from March 1st (mu33410 on X)
mu33410 on X asked whether the cost magpie shows is adjusted for GPT's
fast modes. Mostly it was: a call is priced at the tier its reply named
(else the one sent, which is what a Codex/ChatGPT row has) from
models.dev's experimental.modes, on every route (Responses, Chat
Completions, the Responses WebSocket, which goes through the same
handler, and Codex's backend), in the Usage page, a key's spend limit
and a subscription window's API value. Checked against OpenAI's pricing
page: Fast (priority) is 2x on every model that lists it, Flex 0.5x,
Ultrafast 6x on GPT-6 Astra and GPT-6.1 Sol, all matching.

The gap: OpenAI prices GPT-6.2 Astra's and Sol's Ultrafast at "3 times
the Standard rate through February 28, 2027" and "6 times the Standard
rate starting March 1, 2027 (Pacific time)", and models.dev lists their
ultrafast mode with no cost. TierPrice skipped it, so every GPT-6.2
Ultrafast call, on an OpenAI key or a Codex subscription, was priced at
the standard rate, below Fast.

catalog.TierTimes holds that dated multiple; TierPrice takes the call's
time and gives the standard price times the multiple of its day, every
part and the long-context rate with it (Price.Times). The price caches in
Pricer and apiPricer key on the day's rate (Record.tierKey), and the
request-index ask names it, so calls either side of March 1st price
apart. Scoped to the openai maker entry and those two models; Claude's
fast mode reads its own mode as before.

Run (WSL magpie-desktop, Linux, Go 1.26.8; mote overloaded by another
session's full suite, mica's disk full):
- TestDatedUltrafastPricedAtItsDay (new; Claude fast and standard
  unchanged among its cases): red on origin/main 78b6d47cc (Ultrafast
  0.11 for 0.33 and 0.66), green with the fix, also with TZ=UTC and
  TZ=Asia/Tokyo. TestFastCallsPricedAtTheirTier green.
- Broken by hand: tierKey without the day's rate -> red (the March call
  priced at the promotion's 3x through the cache).
- go test ./internal/catalog ./internal/usage: catalog ok; usage ok but
  TestRequestStateKeptAcrossACatalogFetchedAgain, red the same (-count=10)
  on untouched origin/main 78b6d47cc on Linux, filed as #6572.
- gofmt -l clean; go vet for linux, windows and darwin on both packages.
Not run before the push: the full ./... suite (runs after, on mote).
2026-11-18 23:18:27 +08:00

84 lines
4.6 KiB
Go

package usage
import (
"math"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// mu33410 on X asked whether Fast and the other modes are priced as such.
// OpenAI's pricing page prices GPT-6.2 Sol's and Astra's Ultrafast at 3
// times the standard rate through 2027-02-28 and 6 times from 2027-03-01
// (Pacific time), and models.dev lists the mode with no cost, so an
// Ultrafast call there was priced at the standard rate, below Fast's. The
// catalog is models.dev's own bytes for gpt-6.2-sol and claude-opus-5-5.
func TestDatedUltrafastPricedAtItsDay(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
if err := os.WriteFile(catalog.CachePath(), []byte(`{"openai":{"id":"openai","models":{`+
`"gpt-6.2-sol":{"id":"gpt-6.2-sol","cost":{"input":2,"output":10,"cache_read":0.1,"cache_write":2.5,"tiers":[{"input":4,"output":15,"cache_read":0.2,"cache_write":5,"tier":{"type":"context","size":272000}}],"context_over_200k":{"input":4,"output":15,"cache_read":0.2,"cache_write":5}},"experimental":{"modes":{"fast":{"cost":{"input":4,"output":20,"cache_read":0.2,"cache_write":5},"provider":{"body":{"service_tier":"priority"}}},"pro":{"provider":{"body":{"reasoning":{"mode":"pro"}}}},"ultrafast":{"provider":{"body":{"service_tier":"ultrafast"}}}}}}}},`+
`"anthropic":{"id":"anthropic","models":{`+
`"claude-opus-5-5":{"id":"claude-opus-5-5","cost":{"input":4,"output":20,"cache_read":0.2,"cache_write":5},"experimental":{"modes":{"fast":{"cost":{"input":8,"output":40,"cache_read":0.4,"cache_write":10},"provider":{"body":{"speed":"fast"},"headers":{"anthropic-beta":"fast-mode-2026-02-01"}}}}}}}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
pt, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
t.Fatal(err)
}
change := time.Date(2027, 3, 1, 0, 0, 0, 0, pt)
before, after := change.Add(-time.Second), change
in, out, cached := 40_000, 2_000, 100_000
sol := (float64(in)*2 + float64(out)*10 + float64(cached)*0.1) / 1e6
opus := (float64(in)*4 + float64(out)*20 + float64(cached)*0.2) / 1e6
calls := []struct {
name, prov, model, sent, served string
at time.Time
want float64
}{
{"API Ultrafast in the promotion", "openai", "gpt-6.2-sol", "ultrafast", "ultrafast", before, 3 * sol},
{"API Ultrafast from March 1st", "openai", "gpt-6.2-sol", "ultrafast", "ultrafast", after, 6 * sol},
{"Codex Ultrafast in the promotion", "codex", "gpt-6.2-sol", "ultrafast", "", before, 3 * sol},
{"Codex Ultrafast from March 1st", "codex", "gpt-6.2-sol", "ultrafast", "", after, 6 * sol},
{"API Ultrafast run at the default tier", "openai", "gpt-6.2-sol", "ultrafast", "default", before, sol},
{"API Ultrafast run at priority", "openai", "gpt-6.2-sol", "ultrafast", "priority", after, 2 * sol},
{"API Fast after the change", "openai", "gpt-6.2-sol", "priority", "priority", after, 2 * sol},
{"API standard", "openai", "gpt-6.2-sol", "", "", after, sol},
{"Claude fast, unchanged", "anthropic", "claude-opus-5-5", "fast", "", after, 2 * opus},
{"Claude standard", "anthropic", "claude-opus-5-5", "", "", before, opus},
}
// one Pricer and one Coster across every call: a cache keyed without
// the day would price the second Ultrafast call as the first
cost, api, priceOf := NewCoster(), apiPricer(), Pricer()
for _, c := range calls {
rec := Record{Time: c.at, Provider: c.prov, Model: c.model, TierSent: c.sent, TierServed: c.served, Input: in, Output: out, CacheRead: cached}
if pr := priceOf(rec); pr == nil || math.Abs(rec.CostAt(*pr)-c.want) > 1e-9 {
t.Errorf("%s: the ledger prices it %+v, want cost %v", c.name, pr, c.want)
}
if got, ok := cost(rec); !ok || math.Abs(got-c.want) > 1e-9 {
t.Errorf("%s: a key limit counts it at %v (%v), want %v", c.name, got, ok, c.want)
}
if c.prov == "codex" {
if pr := api(rec); pr == nil || math.Abs(rec.CostAt(*pr)-c.want) > 1e-9 {
t.Errorf("%s: API price %+v, want cost %v", c.name, pr, c.want)
}
}
}
// a long Ultrafast prompt: the standard long-context rate ($4 in, $15
// out) three times over
long := Record{Time: before, Provider: "openai", Model: "gpt-6.2-sol", TierSent: "ultrafast", TierServed: "ultrafast", Input: 300_000, Output: 1_000}
if got, want := func() float64 { c, _ := cost(long); return c }(), 3*(300_000*4.0+1_000*15.0)/1e6; math.Abs(got-want) > 1e-9 {
t.Errorf("a long Ultrafast prompt costs %v, want %v", got, want)
}
}