diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 930764f3c..af65db1e5 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -289,6 +289,7 @@ the key's leaves the call as asked, with a log line (`compactRefused`). - A record is priced at its tier (`Record.billedTier`: `tier_served` when the reply named one, so an OpenAI API call asked `priority` and run at `default` is billed at the default; else `tier_sent`, which is what a ChatGPT backend row has, its `tier_served` being empty). `catalog.TierPrice` reads the price from the model's models.dev `experimental.modes`: the mode whose `provider.body.service_tier` is the tier, or whose `speed` is `fast` for Claude's `fast`, under `openai` or `anthropic`. That price stands only where the row's price is that entry's standard one, so a relay's rate or the user's own price is left as it is. The pricing pages agree: OpenAI's Fast (Priority processing until 2026-07-30) is twice gpt-6.1-sol's standard and Ultrafast six times, Flex half; Anthropic's fast mode is twice Claude Opus 5.5's. A mode lists no long-context price or 1-hour write, so the standard's are scaled by the mode's own ratio. The Usage page, a key's spend limit (`NewCoster`) and a subscription window's API value (`apiPricer`) all price through it (`TestFastCallsPricedAtTheirTier`, Archivemylife on X). Before this, every tier was priced at the standard rate. - An agent's fast pick (`settings.FastPicks`, the bolt in its picker, #954) may also be in `settings.UltraPicks` (#1954), offered by the GUI only where one of Codex's accounts lists Ultrafast on the model (`provider.CanUltra`, `UltraModels`). `fastPick` marks its candidates `ultra`; a try of one goes with `service_tier: "ultrafast"` on Chat and Responses, and an agent on Messages carries it in the context (`withUltra`) to `forwardTranslated`, which sets `Request.Ultrafast` for the ChatGPT backend. Either way the same check as a client's own Ultrafast ask applies, now on Chat as well as Responses: an account whose list doesn't offer it is put after a mate that does (`tierMate`), or, with none left, sent `priority`. A Chat body's own `service_tier: "ultrafast"` is parsed as Ultrafast too (`chat.go`). `TestUltraPickSentUltrafast`. - A group's member may be Ultrafast as well as fast (`Group.Ultra`, a subset of `Fast`, `Group.SetMemberTier`; the member's button steps Standard speed, Fast, Ultrafast where `ultraOK`; `ultra=` in `magpie group`), and a model has its own speed (`settings.ModelTiers`, "/" to `priority` or `ultrafast`, `provider.SetModelTier`, Model settings and `magpie model tier`; kdajenks on X). The gateway sends it on a direct pick for an agent with no fast pick of its own (`provider.ModelTier`, the branch after `IsFastPick`), through the same `fastPick`, so an account without Ultrafast is sent `priority`. A group member goes as the group says. On a provider the user added by its address (`OwnFast`, a GPT or o-series model on an OpenAI API) the member's button goes on to Ultrafast when OpenAI offers the tier on the model: one of Codex's accounts lists it, or Codex's own `models_cache.json` entry for the slug does (`codexcat.OwnOffers`, the entry `ownTiers` already offers Codex), and the tier is only asked for, as its Fast is: a provider that turns the field away is asked again without it (KoiLx0 on X). A fast member never goes slower than the provider's extraBody asks: its `service_tier` `ultrafast` (forced or not) makes a Fast member go Ultrafast (`extraUltra`), a slower one (`flex`) gives way to the member's Fast, and a forced `!service_tier` still goes as written over all of them (`TestFastMemberOwnProviderExtraTier`, `TestCanUltraOwnProvider`, `group-member-fast.test.cjs`). `TestModelTierSentToEveryAgent`, `TestGroupMemberUltrafast`, `model-speed.test.cjs`. +- OpenAI's own API serves `service_tier: "ultrafast"` on the models its [Ultrafast mode](https://developers.openai.com/api/docs/guides/ultrafast-mode) names, GPT-6.2 Astra, GPT-6.2 Sol, GPT-6 Astra and GPT-6.1 Sol (`provider.OpenAIUltra`, a dated snapshot too), on its Responses only (OoopsHh on X: in Docker an OpenAI key's GPT-6 Astra had Fast and no Ultrafast, since every Ultrafast offer came from a Codex account's or CLI's list). On 11-18 api.openai.com's `/v1/responses` took the tier for those four and answered `Invalid service_tier argument` for GPT-6 Sol and GPT-6.1 Terra; `/v1/chat/completions` answered that for every model. So `UltraOn` offers Ultrafast on an OpenAI key's Responses (and on a provider added by its address, `OwnFast`) for those models, `buildResponses` sends it to api.openai.com for them alone (another model goes `priority`), and a Chat Completions request to api.openai.com that would carry it, a client's own ask included, goes `priority` instead (`chatNoUltra`, in the `tierMate` check). Another vendor's model is untouched. `TestOpenAIKeyUltrafast`, `TestCanUltraOwnProvider`. A Codex (ChatGPT) account's list is asked with the newest Codex magpie knows (`codexVersion`), and with none at hand, as in Docker, with `codexClientVersion` or npm's latest (`codexPublished`); the backend lists GPT-6.2 and Ultrafast only from 0.166.0, so the fallback is kept at or above it (`TestCodexFallbackListsUltrafast`). - Codex's service tier (`settings.CodexServiceTier`, a row in Codex's settings, JackyFill on X) sets the `service_tier` of every request of Codex's, whatever Codex's own `/fast` asked: empty is as Codex asks (the default, nothing changed), `default` sends none (standard), `priority` is Codex's Fast and `ultrafast` its Ultra Fast (the request values in Codex's `ServiceTier`; Flex is not offered, since the ChatGPT backend's requests drop it in `codex_request.go`). `codexTiered` ([`codex_tier.go`](../../internal/gateway/codex_tier.go)) rewrites the body as it comes in, on both of Codex's ways in: `codexBackend` (Codex signed in, `/backend-api/codex`) and the main handler's Codex-on-Responses block, which a websocket turn reaches too. Everything after treats it as Codex's own ask: an account whose list lacks Ultrafast is put after a mate that offers it (`tierMate`) or sent `priority`, and a provider that refuses a tier drops it. A model with no provider (`gpt-…`, Codex's own sign-in, whose plan magpie doesn't know) is sent `ultrafast` only where one of magpie's Codex accounts lists it on the model (`UltraModels`), else `priority`. Another agent's request is never touched, and fast picks still apply after. `TestCodexServiceTierSet`, `codex-service-tier.test.cjs`. - A client's `service_tier: "fast"`, Codex's own name for its Fast mode, is read as `priority` on Chat and Responses as the main handler takes the body (`fastTier` in [`codex_tier.go`](../../internal/gateway/codex_tier.go), before the Codex and key tiers above and before `tier_asked` is noted), and `codexBody` sends it as `priority` too. Codex itself never sends `fast`: its `ServiceTier::from_request_value` reads `fast` and `priority` alike as Fast and `request_value` sends `priority` (openai/codex `codex-rs/protocol/src/config_types.rs`). Before, `fast` was dropped on the way to a ChatGPT account (`codexBody` kept only `priority` and `ultrafast`) and never set the IR's Fast (Archivemylife on X). Claude's equivalent is `speed: "fast"` (Anthropic's own `service_tier`, `auto`/`standard_only`, is about capacity, not speed), already read as Fast. The ChatGPT backend's reply says `auto` and then `default` whether priority ran or not (`servedTierAt`): on gpt-6.1-sol a `fast` ask decoded at about 29 tok/s against about 22 without, both answered `default`. `TestFastTierWordReachesCodex`, `TestCodexBodyServiceTier`. - The Codex client magpie presents to the ChatGPT backend (`settings.CodexClient`, a row in Codex's settings, ouhthuo on X) is Codex CLI by default (`originator: codex_cli_rs`, `User-Agent: codex_cli_rs/ (; ) `) or `desktop`: `originator: Codex Desktop` and `Codex Desktop/ (; ) unknown (Codex Desktop; )`, as codex-rs `default_client.rs` builds it from the originator the app sets. Only these two values are accepted. `codexClientHeaders` in `internal/provider/codex_request.go` is the one place both headers are set, so a model request (`codexSign`), the account calls (`/wham`, resets, token refresh, device sign-in) and a test asked as Codex all follow it; the sign-in's authorize URL keeps `codex_cli_rs`. A real Codex client's own originator never reaches the backend on a signed-in account: `codexSign` replaces it, which this setting now chooses. diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 3db161149..71a2eea06 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -4294,8 +4294,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // account whose own list doesn't is asked for priority (Fast) // instead, which every plan with Fast takes (yxinyu715 on X): asked // by the client in either of OpenAI's APIs, or by a pick (#1954) + // OpenAI's own Chat Completions turns Ultrafast away, which its + // Responses take (OoopsHh on X), so there it goes as Fast too openAI := from == provider.Chat || from == provider.Responses - if (openAI && askedTier(attemptBody) == "ultrafast" || ultra && from == provider.Anthropic) && !takesTier(c, "ultrafast") { + if (openAI && askedTier(attemptBody) == "ultrafast" || ultra && from == provider.Anthropic) && (!takesTier(c, "ultrafast") || from == provider.Chat && chatNoUltra(c.p)) { if j := tierMate(cands[i+1:], c, "ultrafast"); j >= 0 { j += i + 1 cs := slices.Clone(cands) diff --git a/internal/gateway/groupfast.go b/internal/gateway/groupfast.go index 5872197c4..8e9efc26b 100644 --- a/internal/gateway/groupfast.go +++ b/internal/gateway/groupfast.go @@ -74,6 +74,14 @@ func extraUltra(p provider.Provider) bool { return tier == "ultrafast" } +// chatNoUltra reports whether a Chat Completions request goes to p on +// OpenAI's own API, which answers service_tier "ultrafast" there with 400 +// "Invalid service_tier argument" (11-18), on every model; its Responses +// take it (provider.OpenAIUltra). +func chatNoUltra(p provider.Provider) bool { + return p.Chat != "" && provider.HostOf(p.Chat) == "api.openai.com" +} + // ultraKey marks a try of a pick sent Ultrafast (#1954) from an agent on // Claude's API: its speed "fast" goes to the ChatGPT backend as Ultrafast // (forwardTranslated), on an account that offers it. diff --git a/internal/gateway/openai_ultra_test.go b/internal/gateway/openai_ultra_test.go new file mode 100644 index 000000000..9454d8f27 --- /dev/null +++ b/internal/gateway/openai_ultra_test.go @@ -0,0 +1,86 @@ +package gateway + +import ( + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// OoopsHh on X: an OpenAI key's GPT-6 Astra had Fast and no Ultrafast. OpenAI's +// API serves service_tier "ultrafast" on its Responses for the models its +// Ultrafast mode names, so a pick sent Ultrafast is asked for it there, +// from its Responses or Claude's API; Chat Completions turns the tier away, +// so it goes as Fast there. A model it doesn't serve so (GPT-6 Sol) can't be picked +// Ultrafast, and another vendor's (Claude on an Anthropic key) keeps its own +// fast mode, with no service_tier. +func TestOpenAIKeyUltrafast(t *testing.T) { + s, up := fasted(t) + oa := provider.Provider{ID: "ok", Name: "OpenAI", Preset: "openai", Key: "ko", Chat: "https://api.openai.com/v1", Responses: "https://api.openai.com/v1", Models: []string{"gpt-6-astra", "gpt-6-sol"}} + if err := provider.Save(oa); err != nil { + t.Fatal(err) + } + p, _ := provider.Find("ok") + if !provider.CanUltra(*p, "gpt-6-astra") || !provider.CanUltra(*p, "gpt-6-astra-2026-09-14") { + t.Fatal("OpenAI's API offers no Ultrafast on GPT-6 Astra") + } + if provider.CanUltra(*p, "gpt-6-sol") { + t.Fatal("Ultrafast offered on GPT-6 Sol, which OpenAI's API turns away") + } + if err := provider.SetFastTier("codex", "ok/gpt-6-sol", "ultrafast"); err == nil { + t.Fatal("GPT-6 Sol picked Ultrafast") + } + if err := provider.SetFastTier("codex", "ok/gpt-6-astra", "ultrafast"); err != nil { + t.Fatal(err) + } + for _, a := range []struct{ name, path, body string }{ + {"responses", "/v1/responses", `{"model":"ok/gpt-6-astra","input":"hi"}`}, + {"anthropic", "/v1/messages", `{"model":"ok/gpt-6-astra","max_tokens":1024,"messages":[{"role":"user","content":"hi"}]}`}, + } { + r := postFrom(t, s, "codex", a.path, a.body) + host, path, _, body := up.last(t) + if host != "api.openai.com" || path != "/v1/responses" || body["service_tier"] != "ultrafast" || body["model"] != "gpt-6-astra" { + t.Errorf("%s: sent to %s%s %v", a.name, host, path, body) + } + if len(r.Tries) != 1 || !r.Tries[0].Fast { + t.Errorf("%s: traced %+v", a.name, r.Tries) + } + // another agent's request goes at the usual speed + postFrom(t, s, "opencode", a.path, a.body) + if _, _, _, body := up.last(t); body["service_tier"] != nil { + t.Errorf("%s: another agent's request: %v", a.name, body) + } + } + // Chat Completions turns Ultrafast away, so an agent speaking it is + // sent Fast there, its own ask for Ultrafast too + for _, body := range []string{ + `{"model":"ok/gpt-6-astra","messages":[{"role":"user","content":"hi"}]}`, + `{"model":"ok/gpt-6-astra","messages":[{"role":"user","content":"hi"}],"service_tier":"ultrafast"}`, + } { + postFrom(t, s, "codex", "/v1/chat/completions", body) + if host, path, _, sent := up.last(t); host != "api.openai.com" || path != "/v1/chat/completions" || sent["service_tier"] != "priority" { + t.Errorf("chat %s: sent to %s%s %v", body, host, path, sent) + } + } + // a client's own Ultrafast on OpenAI's Responses goes as asked + postFrom(t, s, "opencode", "/v1/responses", `{"model":"ok/gpt-6-astra","input":"hi","service_tier":"ultrafast"}`) + if _, _, _, body := up.last(t); body["service_tier"] != "ultrafast" { + t.Errorf("client's own ultrafast: %v", body) + } + + // another vendor's model is unchanged: Claude on an Anthropic key has + // no Ultrafast, and its fast pick goes in Claude's words + an, _ := provider.Find("an") + if provider.CanUltra(*an, "claude-opus-5-5") { + t.Fatal("Ultrafast offered on an Anthropic key's Claude") + } + if err := provider.SetFastTier("claude", "an/claude-opus-5-5", "ultrafast"); err == nil { + t.Fatal("Claude picked Ultrafast") + } + if err := provider.SetFastPick("claude", "an/claude-opus-5-5", true); err != nil { + t.Fatal(err) + } + postFrom(t, s, "claude", "/v1/messages", `{"model":"an/claude-opus-5-5","max_tokens":1024,"messages":[{"role":"user","content":"hi"}]}`) + if host, _, _, body := up.last(t); host != "api.anthropic.com" || body["service_tier"] != nil || body["speed"] != "fast" { + t.Errorf("Claude's fast pick: %s %v", host, body) + } +} diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go index 11d6c9edb..a966e7925 100644 --- a/internal/gateway/responses.go +++ b/internal/gateway/responses.go @@ -1569,8 +1569,9 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte { if r.Fast && (host == "chatgpt.com" || host == "api.openai.com") { out["service_tier"] = "priority" } - // Ultrafast only the ChatGPT backend offers - if r.Ultrafast && host == "chatgpt.com" { + // Ultrafast the ChatGPT backend offers, and OpenAI's API on the models + // its Ultrafast mode names (OoopsHh on X); elsewhere it goes as Fast + if r.Ultrafast && (host == "chatgpt.com" || host == "api.openai.com" && provider.OpenAIUltra(model)) { out["service_tier"] = "ultrafast" } if r.OwnTier && r.Tier != "" { diff --git a/internal/provider/codex_models_test.go b/internal/provider/codex_models_test.go index 6c2a1f023..9bdfeeb42 100644 --- a/internal/provider/codex_models_test.go +++ b/internal/provider/codex_models_test.go @@ -301,3 +301,12 @@ func TestCodexVersionNotRerun(t *testing.T) { t.Fatal("the npm codex was run") } } + +// A magpie with no Codex to ask (in Docker: OoopsHh on X) asks for an +// account's models as a Codex the ChatGPT backend lists GPT-6.2 and +// Ultrafast to, which it does from 0.166.0 on. +func TestCodexFallbackListsUltrafast(t *testing.T) { + if newerVersion(codexClientVersion, "0.166.0") != codexClientVersion { + t.Fatalf("models asked for as Codex %s, which is listed no Ultrafast", codexClientVersion) + } +} diff --git a/internal/provider/fastpick.go b/internal/provider/fastpick.go index 228e673bf..f88e3a080 100644 --- a/internal/provider/fastpick.go +++ b/internal/provider/fastpick.go @@ -48,11 +48,13 @@ func UltraPicks(agent string) []string { // Ultrafast: a ChatGPT model that one of Codex's accounts lists the tier // on (Account.Tiers), or a plugin's model, with fast mode, that the plugin // says its vendor serves at ultrafast (serviceTiers, #2514), or another -// magpie's model its list offers ultrafast on (#1234), or a GPT model on a +// magpie's model its list offers ultrafast on (#1234), or a model OpenAI's +// API serves at ultrafast on that API (OpenAIUltra), or a GPT model on a // provider the user added by its address (OwnFast) that OpenAI offers -// Ultrafast on: one of Codex's accounts lists the tier on the model, or -// Codex's own entry for it does, which Codex is offered on that provider -// as well (codexcat.OwnOffers; KoiLx0 on X, a relay that serves it). Like +// Ultrafast on: its API serves it so (OpenAIUltra), one of Codex's accounts +// lists the tier on the model, or Codex's own entry for it does, which +// Codex is offered on that provider as well (codexcat.OwnOffers; KoiLx0 on +// X, a relay that serves it). Like // its Fast, that is asked for, and a provider that turns the field away is // asked again without it. A list that doesn't say tiers offers none. func CanUltra(p Provider, model string) bool { @@ -75,9 +77,15 @@ func UltraOn(p Provider, model string, ultra map[string]bool) bool { } if OwnFast(p, model) { slug := modelSlug(model) - return ultra[slug] || codexcat.OwnOffers(slug, "ultrafast") + return ultra[slug] || codexcat.OwnOffers(slug, "ultrafast") || OpenAIUltra(slug) } - return p.Account != nil && p.Account.Agent == "codex" && CanFast(p, model) && ultra[model] + if p.Account == nil { + // OpenAI's own API, on its Responses, for the models its Ultrafast + // mode names (OpenAIUltra); a magpie in Docker has no Codex CLI + // whose list could say so (OoopsHh on X) + return HostOf(p.Responses) == "api.openai.com" && CanFast(p, model) && OpenAIUltra(model) + } + return p.Account.Agent == "codex" && CanFast(p, model) && ultra[model] } // UltraModels are the models one of Codex's accounts offers Ultrafast on. diff --git a/internal/provider/groupfast.go b/internal/provider/groupfast.go index e3c946d5f..490c6a65f 100644 --- a/internal/provider/groupfast.go +++ b/internal/provider/groupfast.go @@ -184,6 +184,24 @@ var ( // 5.2 and Mythos 5.2); Fable 5.1 and before never had it. var claudeFastModels = []string{"claude-opus-4-8", "claude-opus-5", "claude-opus-5-5", "claude-fable-5-2"} +// openAIUltraModels are the models OpenAI's API serves at service_tier +// "ultrafast" (OoopsHh on X): GPT-6.2 Astra, GPT-6.2 Sol, GPT-6 Astra and +// GPT-6.1 Sol, as its Ultrafast mode guide names them +// (developers.openai.com/api/docs/guides/ultrafast-mode), on its Responses +// API alone: on 11-18 api.openai.com's /v1/responses took the tier for these +// four and turned it away ("Invalid service_tier argument") for GPT-6 Sol +// and GPT-6.1 Terra, and /v1/chat/completions turned it away for all. +var openAIUltraModels = []string{"gpt-6.2-astra", "gpt-6.2-sol", "gpt-6-astra", "gpt-6.1-sol"} + +// openAIDated is a dated snapshot's suffix: gpt-6-astra-2026-09-14. +var openAIDated = lazyre.New(`-(\d{4}-\d{2}-\d{2}|\d{8})$`) + +// OpenAIUltra reports whether OpenAI's API serves model, or a dated +// snapshot of it, at service_tier "ultrafast". +func OpenAIUltra(model string) bool { + return slices.Contains(openAIUltraModels, openAIDated.ReplaceAllString(modelSlug(model), "")) +} + // ClaudeFast reports whether a Claude model has fast mode. func ClaudeFast(model string) bool { return slices.Contains(claudeFastModels, claudeDated.ReplaceAllString(strings.ToLower(model), "")) diff --git a/internal/provider/groupfast_test.go b/internal/provider/groupfast_test.go index 025fb3872..491e9e33b 100644 --- a/internal/provider/groupfast_test.go +++ b/internal/provider/groupfast_test.go @@ -144,18 +144,31 @@ func TestSaveGroupFast(t *testing.T) { // A GPT model on a provider the user added by its address can be sent // Ultrafast where OpenAI offers it on the model (KoiLx0 on X): Codex's own -// entry for it lists the tier, as Codex itself is offered it there. Not on -// a model Codex's entry gives Fast alone, nor one it has no entry for, nor -// on another vendor's model. +// entry for it lists the tier, as Codex itself is offered it there, or +// OpenAI's API serves it so (OoopsHh on X: in Docker there is no Codex CLI +// and no entry). Not on a model Codex's entry gives Fast alone, nor one it +// has no entry for, nor on another vendor's model. func TestCanUltraOwnProvider(t *testing.T) { fastHome(t) codexHome := t.TempDir() t.Setenv("CODEX_HOME", codexHome) rl, _ := Find("rl") oa, _ := Find("oa") - if UltraOn(*rl, "gpt-6.1-sol", nil) { + if UltraOn(*rl, "gpt-6.1-mini", nil) || UltraOn(*rl, "gpt-6-sol", nil) { t.Fatal("Ultrafast with no list saying so") } + // with no list at all: the models OpenAI's API serves at Ultrafast + if !UltraOn(*rl, "gpt-6.1-sol", nil) || !UltraOn(*oa, "gpt-6.1-sol", nil) { + t.Error("gpt-6.1-sol: no Ultrafast where OpenAI's API serves it") + } + for m, want := range map[string]bool{ + "gpt-6-astra": true, "gpt-6.2-astra": true, "openai/gpt-6.2-sol": true, "GPT-6-Astra-2026-09-14": true, "gpt-6.1-sol-20261001": true, + "gpt-6-sol": false, "gpt-6.1-terra": false, "gpt-6.2-astra-mini": false, "gpt-6.2": false, "claude-opus-5-5": false, + } { + if OpenAIUltra(m) != want { + t.Errorf("OpenAIUltra(%q) = %v", m, !want) + } + } cache := `{"etag":"e1","models":[ {"slug":"gpt-6.1-sol","description":"x","service_tiers":[{"id":"priority","name":"Fast"},{"id":"ultrafast","name":"Ultra Fast"}]}, {"slug":"gpt-6.1-mini","description":"x","service_tiers":[{"id":"priority","name":"Fast"}]}]}` @@ -172,8 +185,11 @@ func TestCanUltraOwnProvider(t *testing.T) { {*rl, "gpt-6.1-mini", false}, {*rl, "gpt-7", false}, {*rl, "claude-opus-5-5", false}, - // OpenAI's own API offers no Ultrafast - {*oa, "gpt-6.1-sol", false}, + // OpenAI's own API, on the models its Ultrafast mode names + {*oa, "gpt-6.1-sol", true}, + {*oa, "gpt-6-sol", false}, + {*oa, "gpt-6.1-mini", false}, + {*oa, "claude-opus-5-5", false}, } { if got := UltraOn(c.p, c.model, nil); got != c.want { t.Errorf("%s/%s: Ultrafast %v, want %v", c.p.ID, c.model, got, c.want)