gateway: /v1/models gives each priced model OpenRouter's pricing

cleary5779 on Discord builds an agent on magpie's gateway and wants the
list to say what a call costs. Each model magpie knows a price of now has

  "pricing": {"prompt": "<USD/token>", "completion": "<USD/token>",
              "input_cache_read": ..., "input_cache_write": ...}

as OpenRouter's /models writes it: decimal strings, the decimal point
moved rather than the float divided (perToken: $0.3/M is "0.0000003",
not 2.9999999999999997e-07). prompt and completion are always there for
a priced model (a price the user set at 0 included); the cache parts
only when they have a price. A model with no known price has no pricing
key at all, never a 0. Base price only: no tiers, no 1-hour write.

The price is the Usage page's (provider.EntryPricer, as EntryPriceIn):
the user's own, else the provider's list price or its maker's at the
price rate, a subscription's model at list price, a group only when
every member costs the same. It goes to the callers magpie_price goes to
(local or sharedWith), with or without X-Magpie-Prices; magpie_price
itself is still only for a list that asks. GET /v1/models/{id} carries
it too (pricedModelObject).

Siblings left without it: Claude Desktop's and Claude Science's lists
(Anthropic's shape, their apps read their own fields), Muse Code's
(models.dev metadata Muse reads), Codex's catalog, Ollama's tags, the
decision models (another magpie's System One), and the image and video
models, whose price isn't a token's.

Cost on the poll path (LESSONS: work added to a path every request or
poll takes): 755 models, 30 providers, the real models.dev catalog:
before the previous commit the pricing added ~30ms to a ~10ms list; with
it, the list is ~13-20ms against ~8-10ms without pricing, at load 65.

Run at 9afa86306 (both commits, on origin/main 40a0b442c), load 73-95:
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: clean.
- go test -tags nogui -timeout 45m ./...: exit 0, 70 packages ok
  (gateway 810s, agent 652s, provider 306s, catalog 29s); log kept whole
  and grepped, no FAIL. No other ./... suite ran meanwhile.
- go test ./internal/gateway ./internal/catalog ./internal/provider
  again beside it: ok.
- TestModelsListPricing is red with the pricing assignment taken out
  (both the list and GET /v1/models/{id} lose pricing), green with it.
Not tried: a real agent reading the list; Windows run (vet only).
This commit is contained in:
yetone
2026-10-25 02:50:12 +08:00
parent b094a577f5
commit 3133f63434
4 changed files with 179 additions and 11 deletions
+1
View File
@@ -209,6 +209,7 @@ recorded as a prediction failure (`noPrediction`).
- A tool call's arguments reach the client as the vendor writes them, translated too (echo_ts on Discord: Codex on Copilot about 10x slower, an apply_patch shown only once whole). The Responses decoder hands each `function_call_arguments.delta` and `custom_tool_call_input.delta` on as a `KToolArgs` delta (`argsDelta`), holding back only the one that would make the JSON whole until `.done`, which `endCall` checks against the final arguments; the Responses encoder streams a custom tool's input as `custom_tool_call_input.delta` (`customStream` in [`custom_tool.go`](../../internal/gateway/custom_tool.go)) before its `.done`. A Copilot account's Responses request is relayed with a `web_search` tool in it (`searchesItself`; checked on a real account 2026-10-17), and Copilot is never picked to search for another provider's model (`searchRank`). It is translated instead when a tool marks a parameter `"encrypted": true` (`asksSealing`, MultiAgentV2's `spawn_agent`): Copilot seals such an argument and can't read its own seal on the subagent's turn. `TestCallArgsStreamWhenTranslated`, `TestCustomInputStreams`, `TestResponsesCallArgsStream`, `TestCopilotRelayedUnlessAskedToSeal`, `TestCopilotUltraSubagentWithWebSearch`.
- A relayed Responses stream gives each output item the one id its `response.output_item.added` gave it, in every event after that carries an `item_id` or an `item`, and in the `output` of `response.completed`, `response.incomplete` or `response.failed` (`idTidy` in [`itemids.go`](../../internal/gateway/itemids.go), applied in `passthrough` before `phaseTidy`). GitHub Copilot's `/responses` gives an item another id in every event (checked 2026-10-19 on a real account, gpt-5.5 with web_search), and Codex starts an item by the added's id and ends one by the done's, so a commentary message was shown twice and a web search never ended (echo_ts on Discord, `TestCopilotItemKeepsItsID`). An upstream whose ids agree goes through byte for byte (`TestItemIDsThatAgreeGoAsTheyCame`); a translated reply's ids are magpie's own.
- A model's reply limit is published within the window beside it (#1438, `provider.OutputWithin`, `Entry.PublishedOutput`): `/v1/models`' `max_output_tokens` (`modelObject`), Cursor Private Inference's `capabilities.max_output_tokens` (`cursorLocalModel`), Muse Code's `limit.output` (`museModels`), the GUI's model and group rows (`providerInfo`, `providersState`), `magpie model output`, and every agent's file (`agent.maxTokens`, #338). The window and the output can come from different sources: Grok's backend lists grok-4.7's 256000 window, and models.dev fills its 500000 output (`catalog.Decorate` and `Provider.replyLimit` fill an output only where the vendor gives none). An unknown window or output is published as it is. The catalog keeps the unclamped `Entry.Output`, and a group's member is still asked for no more than that (`outputLimit`, `withMaxOutput`), so a request isn't lowered by the clamp. A limit the user set stays in their settings as they set it (`TestModelListsKeepOutputWithinTheWindow`). Outside a group, only a Max output the user set for the model (`Entry.OutputSet`) lowers a request (`userOutput`): Claude Code asks a claude-* model for its own full limit (128000 on Opus 5.5) and is told no figure of magpie's for it (`claudeOutput`), and a key whose quota is held per request as max_tokens times the price turned every turn away (YaCo on Discord, `TestUserMaxOutputLowersAPlainProvidersRequest`). A vendor's or models.dev's figure doesn't lower it, since it may be stale.
- `/v1/models` and `/v1/models/{id}` give each model magpie knows a price of its `pricing` in OpenRouter's shape, USD a token as decimal strings: `prompt` and `completion` always (a price set at 0 included), `input_cache_read` and `input_cache_write` only when that part has a price (`openRouterPricing`, `perToken` in [`gateway.go`](../../internal/gateway/gateway.go); cleary5779 on Discord, an agent showing what a call costs). The price is the one the Usage page counts the model at (`provider.EntryPricer`, as `EntryPriceIn`: the user's own, else the provider's list price or its maker's at its price rate; a subscription's model at its list price; a group only when every member costs the same), the base price only (no `Tiers`, no 1-hour write). A model no price is known for has no `pricing` key, never a 0. It is told to the callers `magpie_price` is (`local` or `sharedWith`), whether they ask or not. The list reads the settings, groups and providers once (`EntryPricer`), and `catalog.PricedBy` and `AtEndpoint` look a model up in indexes built once a catalog load (`priceIndexOf`, `endpoints`), not by walking every models.dev model a look-up: a 755-model list went from about 38ms to 18ms with pricing on, against about 10ms without, at load 65 (`TestModelsListPricing`, `TestPerToken`). Claude Desktop's, Claude Science's, Muse Code's, Codex's and Ollama's lists, the decision, image and video models carry no `pricing`: their clients read a shape of their own, and an image's price isn't a token's.
- A request that names no reply limit, translated to Anthropic's Messages API, which needs `max_tokens` (`forwardTranslated`), is sent the model's published limit (`askedOutput` in [`maxoutput.go`](../../internal/gateway/maxoutput.go)): the user's Max output, else the vendor's list or models.dev, within the window. `anthropicBody`'s `defaultMaxTokens` (16384) is left only for a model whose limit isn't known. Every Codex turn on a Claude went at 16384 before, and a long thinking and tool turn ran out of it: `stop_reason: max_tokens` reaches a Responses client as `incomplete_details.reason: "max_output_tokens"` (#1792, tensornull). A limit the client names goes as it is. The thinking budget fits under the filled limit as under one the client named (`TestAnthropicMaxTokensIsTheModels`).
- A restart to update on a Mac or Linux hands the listening socket to the
new version ([GUI shell](gui-shell.md#a-restart-to-update-hands-the-gateway-over),
+1 -1
View File
@@ -43,7 +43,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi
| Claude Code's proxy | Claude Code on its own subscription asks Anthropic itself, through the proxy its env names. While any Claude account has a proxy of its own (`AccountProxies`), the `env` of Claude Code's `settings.json` names the proxy of the account it is signed in to (#1807): `HTTPS_PROXY` and `HTTP_PROXY` set to that account's own, else the subscription's `Proxy` (`ProxyOfLogin`), and `NO_PROXY` to loopback; none for `direct` or a SOCKS proxy, which Claude Code can't use. It is written on every switch (`switchLogin`, the user's or `SwitchWhenSpent`), when the Claude subscription is saved, and at the gateway's look every `loginSwitchEvery` (a sign-in made in Claude Code itself). Claude Code reads its env at start, so only sessions started after the switch take it. A proxy key the user set (in either case) is theirs: magpie then writes none and takes out none of theirs. What magpie wrote is kept in `claude-proxy-env.json`, and a key is magpie's only while it holds that value. A `settings.json` that doesn't parse is left as it is. Its log lines name the proxy through `netproxy.Redact`, without its user and password (#1886); the env gets it whole. | [`claude_proxy_env.go`](../../internal/provider/claude_proxy_env.go) |
| Routing groups | User-made groups from the Routing view, and auto-found groups of the same model across providers (derived, never stored). The same model is the same id as vendors agree on it (`sameModel`): lowercase, without a vendor's prefix, a version's dot as `-`, a run of spaces, `_` and `-` as one `-` (a relay listing its display name as the id, `GPT-6 Sol`, joins `gpt-6-sol`, Chill on Discord: `TestSameModelDisplayNameAsID`), and without a YYYYMMDD or Ark YYMMDD snapshot date. Every word still counts (`gpt-6`, `gpt-6-mini` and `gpt-6-sol` stay apart), as do a `:variant`, a four-digit release and OpenAI's dashed date (`gpt-4o-2024-08-06`, a pinned snapshot whose group id agents may already name). A provider kept "Only through routing groups" joins them the same way. Members are `provider/model[:effort]`. A group can nest another (`group/<id>`); `SaveGroup` refuses loops. `Match` takes a glob or `re:` pattern (#766), both matched in any case (`compilePattern`; `(?-i)` inside a `re:` makes case matter). A found group follows the providers magpie has: one removed leaves it, while one switched off stays in it (`foundGroups`, `heldOffEntries`; one without a key, such as a plugin not signed in, finds no group), skipped by `membersIn` and said as switched off on the Routing page, so switching one of two providers off for a while doesn't take the group away from the agents, rules and groups set to it; it is back in its place once on again (#2671: `TestFoundGroupStaysWithAProviderOff`, `TestFoundGroupWithAProviderOff`, `TestReseatKeepsAFoundGroupWithAProviderOff`, `TestFoundGroupMemberOfASwitchedOffProvider`). A found group the user changed (an `auto-…` id stored) is read without the members of providers magpie no longer has (`withoutRemoved`, not written back, so a provider back under its id is back in it), except a manual group's pick, a rule's model, or all of them when none would be left. A switched-off provider's members stay in it, skipped by `membersIn`, and the Routing page says them as switched off (`memberJSON.providerOff`). The user's own groups keep their members. Three templates make a group in one click from the models the user has (`provider.Templates`, `AddTemplate`; the Routing page's cards before a first group and its Templates menu after, `magpie group template [kind] [name]`): `smart` sends a hard turn to the strongest model and an easy one or a compaction to a cheap one, asked by a classifier (the decision API when there is one, else the cheapest priced model that doesn't reason); `thrifty` starts on two cheap models of different providers and sends high reasoning to the strongest; `steady` fails over between the strongest model of up to three providers. A model is weighed by its maker's list price when known, else what its provider charges, else by its id's size words (`sizeOf`); one over `tooDear` (USD 100 a million output tokens) is never picked. A template that can't be made says what is missing (`Need`). It is saved under a name and id no group has (`freeGroupName`, shared with `CopyGroup`), never over one. A group is shared as text (`ShareGroup`, a group row's Copy to share): JSON marked `"magpie": "routing-group"` with a format `version` (`ShareVersion`), holding the group's members as it names them, patterns, routing, rules, classifier and settings. A group holds only `provider/model` ids, never a key, account or address, and the derived matches, found/hidden/off flags and the rules that hold by a folder (a path on the sharer's machine, `Rule.Folders`) stay behind; a pasted one is left out too. `ImportGroup` (the Routing page's Import, `POST /api/groups/import`) reads it with words or a code fence around it, refuses another text, a newer format or none (`ShareError`, said in the reader's language). A member whose provider the importer has is kept; else the same model (`AutoGroupOf`) under the first provider of theirs that serves it (`Instead`); else it is left out and named (`Missing`), with the rules that send to it, its place among the off and fast members, and a classifier gone takes the intent rules and auto effort with it. Nothing is added when no member is left; what is added gets a free name, never replacing a group. A group of patterns only that match nothing now (a plugin's models not listed) has no members: `/api/groups` says them, and an unmakeable template's, as `[]`, never `null`; the Routing page reads the response through `groupsFrom` and draws a group it still can't draw as one row saying so, so one bad group never hides the rest (#1585). | [`group.go`](../../internal/provider/group.go), [`grouptemplate.go`](../../internal/provider/grouptemplate.go), [`group_share.go`](../../internal/provider/group_share.go) |
| Allowance | `Quotas` asks every vendor at once: subscription windows (`SubscriptionUsage`), key-bought plans (`PlanQuotas`) and key balances (`KeyBalances`). The Usage page, `magpie quota --json` and `GET /v1/magpie/quotas` read it, each window with its whole-window estimate (`Holds`/`HoldsWhy`, `usage.WithWindowHolds`, which `quotaReport` reaches through the `ReckonHolds` hook since package provider can't import usage, #2333). A single card can be read again on its own (#840). A remote magpie's cards come from what it has kept (`CachedCards`, `GET /v1/magpie/quotas/cards`) and are never read there for this computer except by a card's refresh (`POST /v1/magpie/quotas/refresh`, throttled per card). What it has kept is the Usage page's cards with the readings made behind the requests since laid over them, a later one over an earlier by `readSeq` (`cards_kept.go`): each account's `LoginUsage` reading (the used-up switch, the Codex warm-up, routing's `Allowances`), and each key's `KeyAllowance` windows on its plan or balance card. A Claude account's windows Claude Code told as it answered (`NoteClaudeLimits`, kept in `claudeUsage` with their own `heardSeq`, which drops the account's `LoginUsage` reading) stand on its card over any reading started before them (`withHeardClaude`, #2206), so a magpie only requests reach still tells them. An account or key read that way and never on the page gets a card from that reading, and after a restart, before either is read again, the cards come from `quotas.json`, dated (`AsOf`). A magpie serving only other magpies has nobody on its page (#1313). Each key of a remote-magpie provider also gets a card of its own, under `<remote>/~keys`, one section per key (#1547, `remoteKeyCards` in `remote_key_limits.go`): the gateway key's limit as that magpie holds it, read from `GET /v1/magpie/limit` asked with that key alone, so a key never sees another's numbers. It shows a window per cap (tokens, spend) named for the period, used against the cap, with its reset, plus `GatewayKey` (what is left, calls, a cycle of days, used up); a key held to no limit shows `Unlimited`. It is kept for `remoteCardsFor` like the remote's cards and read again by the card's refresh (`?provider=<remote>/~keys&user=<key name>`). A read that fails keeps the last numbers, dated (`AsOf`); with none the card says why, never zero or no limit. A remote from before `/v1/magpie/limit` (404) says it is too old to tell (`errRemoteNoKeyLimit`). A remote that holds the key to a budget, models or accounts answers its cards 403 `gateway_key_held` (#1981, `errKeyHeld`): the cards kept from before are dropped and only the key's own card shows. Its owner can let the key be told the cards of what it may use (#2216, `magpie gateway-key quotas <id> on`); the remote then answers those, and 403 as before when that is none. A remote's cards are kept by provider and key (`remoteCardsID`), so a changed key never shows what the old one was told, and a read is kept only over one asked before it (`keepRemoteCards`). Cursor, Grok and a Kiro key keep the page's cards only. With Settings → Allowances → *Allowance reads* at *When I ask* (`QuotaReads == "asked"`, #1518: magpie started at login before the proxy app read by itself, direct), only a read the user asked for goes out: the Usage page or the tray panel's Allowances opened (no more than once in 30s, `quotasOpened`, #1588) or refreshed (`?asked=1`), a card's refresh (`RefreshUsage`), `magpie quota`, `magpie accounts`, `magpie quota wait` and the TUI's quotas (`Asked`). Any other read (`quotaReading` → `holdUnasked`) carries `netproxy.Hold`, so its requests fail before they leave, and every reader gives back what it last read, dated, or "not read yet" (`errNotAsked`): routing, the used-up switch, the menu bar and alerts go by that. An account refused for its quota (`StaleAllowance` → `markStaleRead`) or a key refused (`StaleKeyAllowance` → `refused`) is still read, once (`unheld`). Warm-up and check-in are not reads and are unchanged. | [`cards_kept.go`](../../internal/provider/cards_kept.go), [`quotas.go`](../../internal/provider/quotas.go), [`subscription_usage.go`](../../internal/provider/subscription_usage.go), [`planquota.go`](../../internal/provider/planquota.go), [`balance.go`](../../internal/provider/balance.go), [`quota_refresh.go`](../../internal/provider/quota_refresh.go), [`remote_quotas.go`](../../internal/provider/remote_quotas.go), [`remote_key_limits.go`](../../internal/provider/remote_key_limits.go), [`quota_asked.go`](../../internal/provider/quota_asked.go) |
| A remote magpie's prices | A local magpie asks a remote magpie's `/v1/models` with `X-Magpie-Prices` (`PricesHeader`, sent by `listHeaders`). The remote answers each model's `magpie_price`: what its own Usage page counts the model at (`EntryPriceIn`: the price its user set, else its provider's list price at its price rate; a routing group only when every member costs the same). It tells it only to a request from its own computer or one let in with a gateway key (`local` or `sharedWith`); a list that didn't ask gets no prices (a remote request without a valid key never reaches the list: `lanGuard` refuses it). The local keeps the price with the fetched list (`catalog.Model.Price`; a value that isn't a price, or has a negative part, is dropped, the model kept) and uses it as that provider's list price (`ListPrice` → `remotePrice`). A price the local user set (`provider/model`, `provider/*`, `*/model`) still comes first, and the local provider's own price rate still applies on top. A remote from before this sends no field and the models stay priced as before; an older local sends no header and is sent nothing new. The model editor says the greyed price is the other magpie's (`remoteList`). | [`remote_magpie.go`](../../internal/provider/remote_magpie.go), [`models.go`](../../internal/provider/models.go), [`gateway.go`](../../internal/gateway/gateway.go) (`models`), [`live.go`](../../internal/catalog/live.go) |
| A remote magpie's prices | A local magpie asks a remote magpie's `/v1/models` with `X-Magpie-Prices` (`PricesHeader`, sent by `listHeaders`). The remote answers each model's `magpie_price`: what its own Usage page counts the model at (`EntryPriceIn`: the price its user set, else its provider's list price at its price rate; a routing group only when every member costs the same). It tells it only to a request from its own computer or one let in with a gateway key (`local` or `sharedWith`); a list that didn't ask gets no `magpie_price` (a remote request without a valid key never reaches the list: `lanGuard` refuses it); every list told to those callers also carries the same price as OpenRouter's `pricing`, for agents ([gateway routing](gateway-routing.md)). The local keeps the price with the fetched list (`catalog.Model.Price`; a value that isn't a price, or has a negative part, is dropped, the model kept) and uses it as that provider's list price (`ListPrice` → `remotePrice`). A price the local user set (`provider/model`, `provider/*`, `*/model`) still comes first, and the local provider's own price rate still applies on top. A remote from before this sends no field and the models stay priced as before; an older local sends no header and is sent nothing new. The model editor says the greyed price is the other magpie's (`remoteList`). | [`remote_magpie.go`](../../internal/provider/remote_magpie.go), [`models.go`](../../internal/provider/models.go), [`gateway.go`](../../internal/gateway/gateway.go) (`models`), [`live.go`](../../internal/catalog/live.go) |
| A remote magpie's service tiers | A remote magpie's `/v1/models` gives each model the `service_tiers` its own Codex is offered on it (`codexTiers` → `codexcat.ServiceTiers`, from the same `CodexCatalog` and `codexcat.Entries` as its `/v1/codex/models`: Fast on a ChatGPT account's GPT model, the tiers of Codex's own entry on a relay's, none on a model it offers none). The local keeps the ids (`catalog.Model.Tiers` → `Entry.Tiers`), `codexListed` hands them on for the remote's models, and `codexcat.Entries` offers them to the local Codex with Codex's own names (`namedTiers`). The gateway sends the tier Codex asks for to a remote magpie as it was asked (`Request.OwnTier`, as for a provider added by its address; dropped and not sent again if refused), and the remote serves it as its own Codex's turn (#1234). A remote from before this sends no tiers, and its models are offered none, as before. The list gives every client the tiers, not only a magpie asking with `X-Magpie-Drawers`: Cindy shows its Fast switch on a model whose `service_tiers` hold `priority` (#1914, `TestModelsListTiersForEveryClient`). | [`codexcat.go`](../../internal/codexcat/codexcat.go), [`codex_models.go`](../../internal/provider/codex_models.go), [`codex_catalog.go`](../../internal/gateway/codex_catalog.go), [`gateway.go`](../../internal/gateway/gateway.go), [`live.go`](../../internal/catalog/live.go) |
| An endpoint's catalog | A key's provider with no catalog of its own (none named, none from a preset) is priced from the models.dev catalog whose `api` is one of its endpoints (`EndpointCatalog`, `catalog.AtEndpoint`; a trailing slash and case don't count, and an Anthropic base, kept as its root, is also matched with its `/v1`). Only when exactly one catalog is at that address, and it isn't a plan's (an id with `-plan`, or a `membershipCatalogs` one): a plan lists its membership's $0, and two catalogs at one address can't be told apart. A subscription's account never gets one. A provider at AMD's own endpoint is priced from `amd`, which lists Qwen3.8-27B at $0, a price, where its maker sells no such model and it had none (#2052); a relay's endpoint matches nothing and its model stays unpriced, never priced at a reseller's guess. `magpie model price` names the catalog, and says how to give a price when none is known. | [`models.go`](../../internal/provider/models.go), [`catalog.go`](../../internal/catalog/catalog.go) |
| A membership's $0 catalog | models.dev lists every model of Kimi Code's plans (`kimi-code-plan-global`, `kimi-code-plan-cn`) and of Xiaomi MiMo's Token Plans (`xiaomi-token-plan-cn`, `-sgp`, `-ams`) at $0: what the membership charges per token, not what the model costs. `membershipCatalogs` are never read for a price (`priceCatalogs`, `makerPriceCatalogs`), so a Xiaomi MiMo model is priced at `xiaomi`'s list price at every region, and each region of the preset names its own catalog, pay as you go `xiaomi` alone (#1873). A Kimi Code provider's model is priced at the Kimi API model it is (`kimiCodeAPI`, from Kimi Code's docs: k3 and k3-256k are kimi-k3, kimi-for-coding-highspeed is kimi-k2.7-code-highspeed), and kimi-for-coding (K2.8 Preview, not on the API) is unpriced rather than free (#1370). The alias is Kimi Code's alone (`kimiCodePlans`): a relay's own `k3` keeps its own price. A catalog other than these that lists a model at $0 still prices it at $0: models.dev has truly free models (nvidia, modelscope). The Requests list names the API model it was priced as (`PricedNameFor`). | [`models.go`](../../internal/provider/models.go) |
+71 -10
View File
@@ -31,6 +31,7 @@ import (
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/awake"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/gwbudget"
"github.com/yetone/magpie/internal/gwmw"
"github.com/yetone/magpie/internal/gwtool"
@@ -953,6 +954,56 @@ func modelObject(e provider.Entry) map[string]any {
return m
}
// pricedModelObject is modelObject with the model's pricing, as the list
// gives it, for the callers the list tells it to.
func pricedModelObject(r *http.Request, e provider.Entry) map[string]any {
m := modelObject(e)
if local(r) || sharedWith(r) {
if pr, ok := provider.EntryPriceIn(settings.Load(), provider.GroupFinder(), e); ok {
m["pricing"] = openRouterPricing(pr)
}
}
return m
}
// openRouterPricing is a price as OpenRouter's model list writes it, the
// shape agents and SDKs built on that list read: USD a token, as decimal
// strings. prompt and completion are always there, a price set at 0
// included, since the price is known; a cache read or write only when it
// has a price, as a part a list leaves at 0 is one it doesn't give. The
// base price only: a model billed more above a prompt size (Tiers) tells
// what it costs below it, and the 1-hour cache write has no field there.
func openRouterPricing(pr catalog.Price) map[string]string {
out := map[string]string{"prompt": perToken(pr.Input), "completion": perToken(pr.Output)}
if pr.CacheRead > 0 {
out["input_cache_read"] = perToken(pr.CacheRead)
}
if pr.CacheWrite > 0 {
out["input_cache_write"] = perToken(pr.CacheWrite)
}
return out
}
// perToken is a price of USD a million tokens as USD a token, written as
// a decimal: the decimal point moved, not the float divided, so $0.3 is
// "0.0000003" and not 2.9999999999999997e-07.
func perToken(perMillion float64) string {
s := strconv.FormatFloat(max(perMillion, 0), 'f', -1, 64)
whole, frac, _ := strings.Cut(s, ".")
if len(whole) < 7 {
whole = strings.Repeat("0", 7-len(whole)) + whole
}
cut := len(whole) - 6
whole, frac = strings.TrimLeft(whole[:cut], "0"), strings.TrimRight(whole[cut:]+frac, "0")
if whole == "" {
whole = "0"
}
if frac == "" {
return whole
}
return whole + "." + frac
}
// cursorLocalModel adds what Cursor Private Inference reads of a model
// (#299): api_types, the APIs it may ask it on — those the model is relayed
// on as it is, so a Claude served on Anthropic Messages is asked there and
@@ -1095,11 +1146,18 @@ func (s *Server) models(w http.ResponseWriter, r *http.Request) {
// there is counted at the same price: told only to this computer
// and to one this magpie is shared with by a gateway key, as the
// prices are the user's own settings
var st settings.Settings
var find func(string) (provider.Group, []provider.Member, bool)
prices := r.Header.Get(provider.PricesHeader) != "" && (local(r) || sharedWith(r))
if prices {
st, find = settings.Load(), provider.GroupFinder()
//
// and to any client, OpenRouter's pricing for each model it knows
// a price of (cleary5779 on Discord: an agent built on magpie
// shows what a call costs), told to the same callers: the price is
// what the Usage page counts the model at, the user's own included.
// The settings, groups and providers are read once for the list
// (EntryPricer), not once a model
told := local(r) || sharedWith(r)
prices := told && r.Header.Get(provider.PricesHeader) != ""
var priceOf func(provider.Entry) (catalog.Price, bool)
if told {
priceOf = provider.EntryPricer(settings.Load(), provider.GroupFinder())
}
// the tiers Codex here is offered on each model (#1234), for every
// client: another magpie offers them to its own Codex, and Cindy
@@ -1133,9 +1191,12 @@ func (s *Server) models(w http.ResponseWriter, r *http.Request) {
if ts := tiers[e.ID]; len(ts) > 0 {
m["service_tiers"] = ts
}
if prices {
if pr, ok := provider.EntryPriceIn(st, find, e); ok {
m["magpie_price"] = pr
if told {
if pr, ok := priceOf(e); ok {
m["pricing"] = openRouterPricing(pr)
if prices {
m["magpie_price"] = pr
}
}
}
// for another magpie: its name as the agents' lists here call
@@ -1202,14 +1263,14 @@ func (s *Server) model(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, scienceObject(e, provider.Labels([]provider.Entry{e})[0]))
return
}
writeJSON(w, 200, modelObject(e))
writeJSON(w, 200, pricedModelObject(r, e))
return
}
}
// by its bare id, as the list gives it with that setting on
if provider.BareModelIDs() {
if e, ok := provider.BareEntry(shown, id); ok {
writeJSON(w, 200, modelObject(e))
writeJSON(w, 200, pricedModelObject(r, e))
return
}
}
+106
View File
@@ -0,0 +1,106 @@
package gateway
import (
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// cleary5779 on Discord: an agent built on magpie reads /v1/models and
// shows what a call costs. Each model magpie knows a price of carries it
// as OpenRouter's list does, USD a token as decimal strings, at the price
// the Usage page counts it at; a model no price is known for has no
// pricing at all, never a 0.
func TestModelsListPricing(t *testing.T) {
fresh(t)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
if err := os.WriteFile(catalog.CachePath(), []byte(`{"anthropic":{"id":"anthropic","models":{
"claude-sonnet-4-5":{"id":"claude-sonnet-4-5","cost":{"input":3,"output":15,"cache_read":0.3,"cache_write":3.75}}}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "a", Name: "A", Key: "k", Models: []string{"claude-sonnet-4-5", "mystery-9"}, Anthropic: "http://127.0.0.1:9/v1"}); err != nil {
t.Fatal(err)
}
if _, ok := provider.EffectivePrice("a", "mystery-9"); ok {
t.Fatal("the fixture's unpriced model has a price; pick one no catalog prices")
}
get := func(path string) []byte {
t.Helper()
r := httptest.NewRequest("GET", path, nil)
r.RemoteAddr = "127.0.0.1:5000"
w := httptest.NewRecorder()
New().Handler().ServeHTTP(w, r)
if w.Code != 200 {
t.Fatalf("%s: %d %s", path, w.Code, w.Body.String())
}
return w.Body.Bytes()
}
var list struct {
Data []map[string]json.RawMessage `json:"data"`
}
if err := json.Unmarshal(get("/v1/models"), &list); err != nil {
t.Fatal(err)
}
byID := map[string]map[string]json.RawMessage{}
for _, m := range list.Data {
var id string
json.Unmarshal(m["id"], &id)
byID[id] = m
}
const want = `{"completion":"0.000015","input_cache_read":"0.0000003","input_cache_write":"0.00000375","prompt":"0.000003"}`
priced, ok := byID["a/claude-sonnet-4-5"]
if !ok {
t.Fatalf("a/claude-sonnet-4-5 isn't listed: %v", byID)
}
if got := string(priced["pricing"]); got != want {
t.Errorf("a/claude-sonnet-4-5's pricing is %s, want %s", got, want)
}
if _, ok := priced["magpie_price"]; ok {
t.Error("magpie_price told to a list that didn't ask for it")
}
unpriced, ok := byID["a/mystery-9"]
if !ok {
t.Fatalf("a/mystery-9 isn't listed: %v", byID)
}
if p, ok := unpriced["pricing"]; ok {
t.Errorf("a model no price is known for has pricing %s", p)
}
// the model asked for alone says the same
var one map[string]json.RawMessage
json.Unmarshal(get("/v1/models/a/claude-sonnet-4-5"), &one)
if got := string(one["pricing"]); got != want {
t.Errorf("GET /v1/models/a/claude-sonnet-4-5's pricing is %s, want %s", got, want)
}
one = nil
json.Unmarshal(get("/v1/models/a/mystery-9"), &one)
if len(one) == 0 {
t.Fatal("GET /v1/models/a/mystery-9 answered no model")
}
if p, ok := one["pricing"]; ok {
t.Errorf("GET /v1/models/a/mystery-9 has pricing %s", p)
}
}
// A price a million tokens is written a token by moving the decimal
// point: no float's rounding, no exponent.
func TestPerToken(t *testing.T) {
for in, want := range map[float64]string{0: "0", 3: "0.000003", 0.3: "0.0000003", 3.75: "0.00000375",
15: "0.000015", 1234567: "1.234567", 12345678.9: "12.3456789", 0.075: "0.000000075"} {
if got := perToken(in); got != want {
t.Errorf("perToken(%v) = %s, want %s", in, got, want)
}
}
// a user's free price is known: both parts there, at 0
got, _ := json.Marshal(openRouterPricing(catalog.Price{}))
if string(got) != `{"completion":"0","prompt":"0"}` {
t.Errorf("a free model's pricing: %s", got)
}
}