diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 056f8d537..f3f83116e 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -205,7 +205,8 @@ the range, with that reading's percentage and bounds, the cached card's model scope, and only calls and readings before that reading. A card that is gated live (a balance, a remote, an error) is gated for past cycles too, and a window no longer on the cached card is left out. Past prices aren't kept, so -the cost is at today's API list prices. See +the cost is at today's API list prices. A call whose own id has no API price +is costed as the model the reply named (Served), as the ledger is (#1498). See `TestWindowHoldsDryAgainAfterReset`, `TestWindowHoldsUsesObservedRefillStart` and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`. diff --git a/internal/usage/window_holds.go b/internal/usage/window_holds.go index 532e163dd..ea5b68d47 100644 --- a/internal/usage/window_holds.go +++ b/internal/usage/window_holds.go @@ -298,29 +298,41 @@ func windowHolds(qs []provider.SubscriptionQuota, now, first time.Time, priceOf // apiPricer is each call's model at its API list price: its maker's, else // the provider's own list, else the maker's of the model the user said it // is the same as (provider.SamePriceIn), never a price the user set for the -// subscription (which may well be nothing) nor a rate it bills at. A price -// of nothing at all is no API price. +// subscription (which may well be nothing) nor a rate it bills at. A model +// none of those prices is priced as the model the reply named (Served), as +// the ledger is (#1498). A price of nothing at all is no API price. func apiPricer() func(Record) *catalog.Price { st := settings.Load() - prices := map[[2]string]*catalog.Price{} - return func(r Record) *catalog.Price { - k := [2]string{r.Provider, r.Model} - if pr, ok := prices[k]; ok { - return pr - } - var pr *catalog.Price - v, ok := provider.MakerPrice(r.Model) + prices := map[[3]string]*catalog.Price{} + one := func(providerID, model string) *catalog.Price { + v, ok := provider.MakerPrice(model) if !ok || v.Cost(1e6, 1e6, 0, 0) == 0 { ok = false - if p, err := provider.Find(r.Provider); err == nil { - v, ok = p.ListPrice(r.Model) + if p, err := provider.Find(providerID); err == nil { + v, ok = p.ListPrice(model) } } if !ok || v.Cost(1e6, 1e6, 0, 0) == 0 { // the model the user said it is (#2344) - v, ok = provider.SamePriceIn(st, r.Provider, r.Model) + v, ok = provider.SamePriceIn(st, providerID, model) } - if ok && v.Cost(1e6, 1e6, 0, 0) > 0 { - pr = &v + if !ok || v.Cost(1e6, 1e6, 0, 0) == 0 { + return nil + } + pr := v + return &pr + } + return func(r Record) *catalog.Price { + served := strings.TrimSpace(r.Served) + if served == r.Model { + served = "" + } + k := [3]string{r.Provider, r.Model, served} + if pr, ok := prices[k]; ok { + return pr + } + pr := one(r.Provider, r.Model) + if pr == nil && served != "" { + pr = one(r.Provider, served) } prices[k] = pr return pr diff --git a/internal/usage/window_holds_test.go b/internal/usage/window_holds_test.go index ad4d6de8b..0baba2ad9 100644 --- a/internal/usage/window_holds_test.go +++ b/internal/usage/window_holds_test.go @@ -457,3 +457,38 @@ func TestWindowHoldsReckonsAntigravityPools(t *testing.T) { } } } + +// The ledger prices a call whose own id has no price as the model the +// reply named (Served, #1498). A window's whole is priced the same way: +// one such call otherwise has no API price, and that drops the dollar +// whole for every other call in the window. +func TestHoldsPriceTheModelTheReplyNamed(t *testing.T) { + holdsHome(t) + priceOf := apiPricer() + named := Record{Provider: "relay", Model: "moonshot-kimi-k3", Served: "gpt-5.5"} + if pr := priceOf(named); pr == nil || pr.Input != 1.25 { + t.Fatalf("the model the reply named wasn't its API price: %+v", pr) + } + same := named + same.Served = named.Model + if pr := priceOf(same); pr != nil { + t.Fatalf("a reply that names the same unpriced id was priced: %+v", pr) + } + own := Record{Provider: "relay", Model: "claude-opus-4-7", Served: "gpt-5.5"} + if pr := priceOf(own); pr == nil || pr.Input != 5 { + t.Fatalf("the asked model's own price lost: %+v", pr) + } + + now := holdClock(t, time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)) + reset := now.Add(2 * time.Hour) + Append(Record{Time: now.Add(-8 * 24 * time.Hour), Agent: "codex", Provider: "codex", ProviderAccount: "sam@example.com", + Model: "gpt-5.5", Input: 1000, Status: 200}) + Append(Record{Time: now.Add(-time.Hour), Agent: "codex", Provider: "codex", ProviderAccount: "sam@example.com", + Model: "moonshot-kimi-k3", Served: "gpt-5.5", Input: 1000, Status: 200}) + qs := WithWindowHolds([]provider.SubscriptionQuota{{Provider: "codex", User: "sam@example.com", Windows: []provider.QuotaWindow{ + {Name: "5 hours", Used: 10, ResetsAt: &reset, Span: 5 * time.Hour}, + }}}, now) + if h := qs[0].Windows[0].Holds; h == nil || !h.Priced || h.Cost <= 0 { + t.Fatalf("5 hours: %+v, a priced reply left the dollar whole out", h) + } +}