Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

catalog: a price and an endpoint's catalog are looked up in indexes, and a list is priced at one read of the providers

yetoneb094a577f520
diff --git a/internal/agent/route.go b/internal/agent/route.go
index 905a159f2..7d9234e8e 100644
--- a/internal/agent/route.go
+++ b/internal/agent/route.go
@@ -205,6 +205,7 @@ func magpieModels(agent string) []catalog.Model {
// a model magpie describes images to takes them (provider.Described)
seen := provider.Described != nil && provider.Described()
st, find := settings.Load(), provider.GroupFinder()
+ priceOf := provider.EntryPricer(st, find)
members := func(id string) []provider.Member {
_, ms, _ := find(id)
return ms
@@ -217,7 +218,7 @@ func magpieModels(agent string) []catalog.Model {
// answers from the settings it is given
m.Compact = provider.CompactFor(st, e, find)
// what a call costs the user (Pi's cost, #781)
- if pr, ok := provider.EntryPriceIn(st, find, e); ok {
+ if pr, ok := priceOf(e); ok {
m.Price = &pr
}
if seen && !e.Images {
diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go
index 5d23360c1..93f883f04 100644
--- a/internal/catalog/catalog.go
+++ b/internal/catalog/catalog.go
@@ -493,6 +493,13 @@ func Source() string {
}
func load() map[string]mdProvider {
+ all, _ := loadGen()
+ return all
+}
+
+// loadGen is load with the Reset count it was loaded at, read under the
+// same lock, so what is built of it is kept for that load alone.
+func loadGen() (map[string]mdProvider, int) {
loadMu.Lock()
defer loadMu.Unlock()
if !loaded {
@@ -623,7 +630,7 @@ func load() map[string]mdProvider {
}
}()
}
- return mdev
+ return mdev, gen
}
// Reset forgets the loaded catalog so the next call re-reads the cache,
@@ -725,7 +732,7 @@ func PriceOf(providerID, modelID string) (Price, bool) {
// for a dash, then as the model an entry names as its canonical_model_id
// (DeepSeek's deepseek-flash is deepseek-v4.1-flash).
func PricedBy(providers []string, id string) (Price, bool) {
- all := load()
+ all, at := loadGen()
for _, pid := range providers {
if m, ok := all[pid].Models[id]; ok && m.Cost != nil {
return *m.Cost, true
@@ -737,15 +744,8 @@ func PricedBy(providers []string, id string) (Price, bool) {
}
for _, want := range []string{b, cutAt(b, '('), cutAt(b, ':')} {
for _, pid := range providers {
- keys := make([]string, 0, len(all[pid].Models))
- for key, m := range all[pid].Models {
- if m.Cost != nil && bareID(key) == want {
- keys = append(keys, key)
- }
- }
- if len(keys) > 0 {
- slices.Sort(keys)
- return *all[pid].Models[keys[0]].Cost, true
+ if key, ok := priceIndexOf(all, at, pid).bare[want]; ok {
+ return *all[pid].Models[key].Cost, true
}
}
}
@@ -755,15 +755,8 @@ func PricedBy(providers []string, id string) (Price, bool) {
// lists the id as given, at a price of its own, is still the one asked.
want := dashed(b)
for _, pid := range providers {
- keys := []string{}
- for key, m := range all[pid].Models {
- if m.Cost != nil && dashed(bareID(key)) == want {
- keys = append(keys, key)
- }
- }
- if len(keys) > 0 {
- slices.Sort(keys)
- return *all[pid].Models[keys[0]].Cost, true
+ if key, ok := priceIndexOf(all, at, pid).dashed[want]; ok {
+ return *all[pid].Models[key].Cost, true
}
}
// and by what models.dev says an entry is (canonical_model_id): DeepSeek
@@ -771,7 +764,7 @@ func PricedBy(providers []string, id string) (Price, bool) {
// deepseek/deepseek-v4.1-flash, the id WorkBuddy and the relays serve it
// under, and lists no deepseek-v4.1-flash of its own (#1520). Only where
// nothing above priced it, so an id a vendor lists keeps its own price.
- if pr, ok := canonicallyPriced(all, providers, b); ok {
+ if pr, ok := canonicallyPriced(all, at, providers, b); ok {
return pr, true
}
// and a relay's rename of a model none of them lists as given
@@ -782,6 +775,72 @@ func PricedBy(providers []string, id string) (Price, bool) {
return Price{}, false
}
+// priceIndex is what PricedBy looks a model up by in one models.dev
+// provider, built once a load rather than from every model of the
+// provider at each look-up: a model no catalog prices went through every
+// model of every vendor asked, lower-cased, five times over, and a model
+// list priced row by row (/v1/models' pricing) spent 30ms on it for 250
+// models.
+type priceIndex struct {
+ // bare and dashed are the first key, in key order, of the priced
+ // entries with that bare id, and with that bare id's dots as dashes
+ bare, dashed map[string]string
+ // canon and canonDashed are the price the vendor's own entries under a
+ // canonical id (bare, and dashed) give, nil where they disagree
+ canon, canonDashed map[string]*Price
+}
+
+var priceIndexes struct {
+ sync.Mutex
+ gen int
+ by map[string]*priceIndex
+}
+
+// priceIndexOf is pid's priceIndex in all, the catalog loaded at Reset
+// count at.
+func priceIndexOf(all map[string]mdProvider, at int, pid string) *priceIndex {
+ priceIndexes.Lock()
+ defer priceIndexes.Unlock()
+ if priceIndexes.by == nil || priceIndexes.gen != at {
+ priceIndexes.gen, priceIndexes.by = at, map[string]*priceIndex{}
+ }
+ if x, ok := priceIndexes.by[pid]; ok {
+ return x
+ }
+ x := &priceIndex{bare: map[string]string{}, dashed: map[string]string{}, canon: map[string]*Price{}, canonDashed: map[string]*Price{}}
+ first := func(by map[string]string, k, key string) {
+ if was, ok := by[k]; !ok || key < was {
+ by[k] = key
+ }
+ }
+ seen := map[*map[string]*Price]map[string]bool{&x.canon: {}, &x.canonDashed: {}}
+ agree := func(by *map[string]*Price, k string, p *Price) {
+ if !seen[by][k] {
+ seen[by][k] = true
+ (*by)[k] = p
+ } else if was := (*by)[k]; was != nil && !was.Same(*p) {
+ (*by)[k] = nil
+ }
+ }
+ for key, m := range all[pid].Models {
+ if m.Cost == nil {
+ continue
+ }
+ b := bareID(key)
+ first(x.bare, b, key)
+ first(x.dashed, dashed(b), key)
+ maker, _, ok := strings.Cut(m.Canonical, "/")
+ if !ok || !strings.EqualFold(maker, pid) || m.Cost.Same(Price{}) {
+ continue
+ }
+ c := bareID(m.Canonical)
+ agree(&x.canon, c, m.Cost)
+ agree(&x.canonDashed, dashed(c), m.Cost)
+ }
+ priceIndexes.by[pid] = x
+ return x
+}
+
// canonicallyPriced is the list price the first of providers gives the
// model whose canonical_model_id is b (a bare id), by that id, then with a
// dot and a dash taken for the same. Only a vendor's own model counts — an
@@ -793,27 +852,14 @@ func PricedBy(providers []string, id string) (Price, bool) {
// its id is priced at its maker's, as it was. A vendor listing several
// entries under one canonical at different prices names none of them:
// which one was called isn't known.
-func canonicallyPriced(all map[string]mdProvider, providers []string, b string) (Price, bool) {
- for _, same := range []func(c string) bool{
- func(c string) bool { return bareID(c) == b },
- func(c string) bool { return dashed(bareID(c)) == dashed(b) },
+func canonicallyPriced(all map[string]mdProvider, at int, providers []string, b string) (Price, bool) {
+ for _, by := range []func(*priceIndex) (*Price, bool){
+ func(x *priceIndex) (*Price, bool) { p, ok := x.canon[b]; return p, ok },
+ func(x *priceIndex) (*Price, bool) { p, ok := x.canonDashed[dashed(b)]; return p, ok },
} {
for _, pid := range providers {
- var got *Price
- agree := true
- for _, m := range all[pid].Models {
- maker, _, ok := strings.Cut(m.Canonical, "/")
- if !ok || !strings.EqualFold(maker, pid) || m.Cost == nil || m.Cost.Same(Price{}) || !same(m.Canonical) {
- continue
- }
- if got != nil && !got.Same(*m.Cost) {
- agree = false
- break
- }
- got = m.Cost
- }
- if got != nil && agree {
- return *got, true
+ if p, ok := by(priceIndexOf(all, at, pid)); ok && p != nil {
+ return *p, true
}
}
}
@@ -1488,14 +1534,32 @@ func AtEndpoint(base string) []string {
if want == "" {
return nil
}
- var ids []string
- for id, p := range load() {
- if endpointKey(p.API) == want {
- ids = append(ids, id)
+ all, at := loadGen()
+ endpoints.Lock()
+ defer endpoints.Unlock()
+ if endpoints.by == nil || endpoints.gen != at {
+ // every provider's API, keyed once a load rather than each
+ // look-up: a provider's price asks for it, and a model list
+ // priced row by row asked it once a model (/v1/models' pricing)
+ endpoints.gen, endpoints.by = at, map[string][]string{}
+ for id, p := range all {
+ if k := endpointKey(p.API); k != "" {
+ endpoints.by[k] = append(endpoints.by[k], id)
+ }
+ }
+ for _, ids := range endpoints.by {
+ slices.Sort(ids)
}
}
- slices.Sort(ids)
- return ids
+ return slices.Clone(endpoints.by[want])
+}
+
+// endpoints are the models.dev providers by the API they serve at
+// (endpointKey), for the load of Reset count gen.
+var endpoints struct {
+ sync.Mutex
+ gen int
+ by map[string][]string
}
func endpointKey(u string) string {
diff --git a/internal/provider/models.go b/internal/provider/models.go
index a56113fd4..03cbed114 100644
--- a/internal/provider/models.go
+++ b/internal/provider/models.go
@@ -2113,7 +2113,15 @@ func EffectivePriceIn(s settings.Settings, providerID, model string) (catalog.Pr
// key under a display name counts only for a caller naming that name
// too. Nothing writes such a key — SetModelPrice re-keys the way
// SetModelName does — which is what keeps the two from drifting.
- p, known := byIDOrWas(providerID)
+ return priceIn(s, All(), providerID, model)
+}
+
+// priceIn is EffectivePriceIn with the providers read already, for a
+// caller pricing many models at once: All() copies every provider each
+// time it is read, so a list read it once a model, 30 providers' copies
+// for each of hundreds of models (EntryPricer).
+func priceIn(s settings.Settings, all []Provider, providerID, model string) (catalog.Price, bool) {
+ p, known := byIDOrWasIn(all, providerID)
if !known {
p = Provider{ID: providerID}
}
@@ -2161,22 +2169,32 @@ func priceOf(s settings.Settings, p Provider, known bool, model string) (catalog
// when every member costs the same, none in a fast mode, since which of
// them answers isn't known beforehand. find is GroupFinder's.
func EntryPriceIn(st settings.Settings, find func(string) (Group, []Member, bool), e Entry) (catalog.Price, bool) {
- if e.Group == "" {
- return EffectivePriceIn(st, e.Provider.ID, e.Model)
- }
- _, ms, ok := find(e.ID)
- if !ok || len(ms) == 0 {
- return catalog.Price{}, false
- }
- var first catalog.Price
- for i, m := range ms {
- pr, ok := EffectivePriceIn(st, m.Provider.ID, m.Model)
- if !ok || m.Fast || i > 0 && !pr.Same(first) {
+ return EntryPricer(st, find)(e)
+}
+
+// EntryPricer is EntryPriceIn for a whole list of entries, at the one
+// read of the providers it makes: what a /v1/models or an agent's model
+// file prices row by row.
+func EntryPricer(st settings.Settings, find func(string) (Group, []Member, bool)) func(Entry) (catalog.Price, bool) {
+ all := All()
+ return func(e Entry) (catalog.Price, bool) {
+ if e.Group == "" {
+ return priceIn(st, all, e.Provider.ID, e.Model)
+ }
+ _, ms, ok := find(e.ID)
+ if !ok || len(ms) == 0 {
return catalog.Price{}, false
}
- first = pr
+ var first catalog.Price
+ for i, m := range ms {
+ pr, ok := priceIn(st, all, m.Provider.ID, m.Model)
+ if !ok || m.Fast || i > 0 && !pr.Same(first) {
+ return catalog.Price{}, false
+ }
+ first = pr
+ }
+ return first, true
}
- return first, true
}
// PriceRateOK says what is wrong with a provider's price rate, "" when
@@ -2197,7 +2215,11 @@ func PriceRateOK(r float64) string {
// both outlive the provider they were set for, so the spelling the user typed
// is the one that has to be checked before a name is resolved at all.
func byIDOrWas(id string) (Provider, bool) {
- all := All()
+ return byIDOrWasIn(All(), id)
+}
+
+// byIDOrWasIn is byIDOrWas among the providers given.
+func byIDOrWasIn(all []Provider, id string) (Provider, bool) {
if p, ok := find(all, id); ok {
return p, true
}