Routing
Most models are offered by more than one provider (the operator’s connections to upstream accounts). By default the gateway picks for you. You can steer per request with the provider object in the body, with a :provider suffix on the model name, or with a fallback model. The field names follow OpenRouter’s, so existing code carries over.
Every provider has a tag (openai, azure, …), set by the operator. Tags are matched case-insensitively. The tag of the provider that served a request is in the X-Gateway-Provider response header, and the model catalog lists the providers of each model with their prices.
How a request is routed
Section titled “How a request is routed”- Resolve the model.
modelis matched as a whole first (slug or alias). Only if that finds nothing is it split at the last:into a model and a provider tag. Unknown:404 model_not_found. - Collect candidates: providers that offer the model, are enabled and priced, support the endpoint, and cover what the request needs (tools, vision, structured output, and so on). Your
only,ignoreandmodel:providerfilters apply here. A provider whose circuit breaker is open after repeated failures is skipped; it is probed again with one request at a time. - Order them.
- Providers named in
provider.order, in the order given. - If
provider.sortis set: the rest by that metric. - Then by the operator’s priority (higher first), and among equal priorities by the operator’s weights, randomly.
- Providers named in
- Prefer the provider that served the conversation before. Only when you set neither
ordernorsort: a request that starts the same way as an earlier one (same model, same system prompt and first message) goes first to the provider that served the earlier one, if it saw traffic in the last 10 minutes. This keeps provider-side prompt caches warm. - Append the fallback model’s candidates, if you gave one.
- Try them in order until one answers. A failure before the response starts moves on to the next candidate, up to 3 attempts per request by default, counting every try on every provider and key. After the response has started there is no switching. A candidate whose rate or concurrency budget (set by the operator) is used up is skipped without counting as an attempt.
If no candidate is left the error is 503 no_capacity, or 429 upstream_rate_limited when they were all rate limited. See Errors and retries.
provider
Section titled “provider”{ "model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Which provider answers this?"}], "provider": { "order": ["azure", "openai"], "only": ["azure", "openai"], "ignore": ["acme"], "sort": "price", "allow_fallbacks": false }}| Key | Type | Effect |
|---|---|---|
order |
list of tags | Try these first, in this order. Other providers follow unless allow_fallbacks is false. |
only |
list of tags | Consider only these providers. If none of them offers the model: 400 provider_not_found. |
ignore |
list of tags | Never use these providers. |
sort |
price, latency, throughput |
Rank the providers that order does not name (see below). Any other value: 400. |
allow_fallbacks |
boolean | With false and an order (or a model:provider pin), use only the providers named there and fail if they all fail. Without an order or pin, false changes nothing. |
The provider object is read by the gateway and removed before the request is forwarded. It is accepted on every JSON endpoint, in every dialect.
sort |
Ranks first |
|---|---|
price |
The provider with the lowest sell price: the input price plus the output price of the model’s base price tier at that provider. |
latency |
The lowest recent time to first token. |
throughput |
The highest recent output tokens per second. |
Latency and throughput are measured by the gateway on real traffic (a moving average per provider). A provider without a measurement yet is ranked after the ones that have one. Providers that have the same value fall back to priority and weight.
Sorting turns off the prompt-cache preference of step 4: you asked for a ranking, so you get it, even if that means a cold cache.
Examples
Section titled “Examples”These run against a model offered by two providers, openai (the default choice) and azure (cheaper). The response header shows who served the request.
# No preference: the operator's priority decides.curl -s -D - -o /dev/null https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" -H "Content-Type: application/json" \ -d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Which provider answers this?"}]}' \ | grep -i '^x-gateway-provider'# expect: openai# provider.ordercurl -s -D - -o /dev/null https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" -H "Content-Type: application/json" \ -d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Which provider answers this?"}], "provider": {"order": ["azure"]}}' \ | grep -i '^x-gateway-provider'# expect: azure# provider.sort: cheapest firstcurl -s -D - -o /dev/null https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" -H "Content-Type: application/json" \ -d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Which provider answers this?"}], "provider": {"sort": "price"}}' \ | grep -i '^x-gateway-provider'# expect: azure# provider.ignorecurl -s -D - -o /dev/null https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" -H "Content-Type: application/json" \ -d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Which provider answers this?"}], "provider": {"ignore": ["openai"]}}' \ | grep -i '^x-gateway-provider'# expect: azuremodel:provider
Section titled “model:provider”Appending :<tag> to the model name pins the request to that provider. It is the same as "provider": {"only": ["<tag>"]}, and it works on every endpoint, including Gemini (models/gpt-4o-mini:azure:generateContent) and clients that cannot send extra body fields.
curl -s -D - -o /dev/null https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" -H "Content-Type: application/json" \ -d '{"model": "gpt-4o-mini:azure", "messages": [{"role": "user", "content": "Which provider answers this?"}]}' \ | grep -i '^x-gateway-provider'# expect: azureA tag the model does not have is your mistake, not a capacity problem: it answers 400 provider_not_found without calling anyone.
curl -s https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" -H "Content-Type: application/json" \ -d '{"model": "gpt-4o-mini:nowhere", "messages": [{"role": "user", "content": "Which provider answers this?"}]}'# expect: provider_not_foundFallback model
Section titled “Fallback model”models lists an alternative model to use when every provider of the first one has failed before the response started.
{ "model": "acme-large", "models": ["acme-large", "gpt-4o-mini"], "messages": [{"role": "user", "content": "Which provider answers this?"}]}- The first entry that differs from
modelis the fallback. Only one fallback is used; further entries are ignored. (On/v1/messagesyou may leavemodelout and put the primary first inmodels.) - The fallback is tried only after all candidates of the primary model are exhausted, and only for failures before the response starts.
- It is billed at its own price, and the response reports its slug as
model. - It must be something the request could have used directly: priced, allowed for your key, served on this endpoint, and able to handle what the request contains. A fallback that is not usable is dropped silently; the request then behaves as if you had not given one.
only,ignore,orderandmodel:providerfilters apply to the fallback’s providers as well.
# acme-large has no healthy provider in this example, so gpt-4o-mini answers.curl -s -D headers.txt https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" -H "Content-Type: application/json" \ -d '{"model": "acme-large", "models": ["acme-large", "gpt-4o-mini"], "messages": [{"role": "user", "content": "Which provider answers this?"}]}' \ | grep -o '"model": *"[^"]*"'grep -i '^x-gateway-attempts' headers.txt# expect: "model":"gpt-4o-mini"# expect: X-Gateway-Attempts: 2Price follows the provider
Section titled “Price follows the provider”A provider can have its own price for a model. The price you are charged is that of the provider that served the request, so sort: price and order change your bill, not only your latency. /v1/generation shows the unit prices that were applied. The balance hold of a request starts from the model’s price; when the provider about to be tried is dearer, the hold is raised first, and a provider whose price your balance cannot cover is skipped.