Fallbacks
Name the models to try in order when the first one fails.
1. Name the models in models
Send model as usual, and add models: a list of more models, tried in order when model fails or is limited. The first one to answer ends the search.
| You send | Holds |
|---|---|
model | the model you want |
models | the models to try after it, in order: up to 200 names, each lab/model |
import json
import os
import urllib.request
API_KEY = os.environ["OPENDUNES_API_KEY"]
ADDRESS = "https://opendunes.com/v1/chat/completions"
MODEL = "anthropic/claude-sonnet-4.5"
FALLBACKS = ["openai/gpt-5.4-mini", "google/gemini-2.5-flash"]
PROMPT = "Write a one-line welcome for a bakery in Tlemcen."
body = {
"model": MODEL,
"models": FALLBACKS,
"messages": [{"role": "user", "content": PROMPT}],
}
request = urllib.request.Request(
ADDRESS,
data=json.dumps(body).encode(),
headers={
"Authorization": "Bearer " + API_KEY,
"Content-Type": "application/json",
},
)
with urllib.request.urlopen(request) as response:
reply = json.load(response)
print(reply["model"])
print(reply["choices"][0]["message"]["content"])
print(reply["usage"]["cost"])Name fallbacks that can do the same job: the same kind of input, and an answer your code reads the same way.
Don't put a model the key may not use in models. The request is refused with a 403 that names it, even if the first model would have answered.
2. Read which one answered
The answer's model names the one that served, as lab/model. When it is not the model you sent first, a fallback answered.
A fallback is charged at its own price, never the first model's. usage.cost is the charge of the model that served.
A free model takes no fallbacks.
3. Set the same list for every request
To have the same fallbacks on every request without touching code, open the Gateway page of your dashboard and name up to three models. The list is kept for each API keyAPI keyThe key you create and send with every request that calls a model.More on API keys →.
A request that names its own models sets the dashboard's list aside for that request.
Fallbacks change the model. To change the company that serves the same model, see Provider routing.
Errors
The body says why. What to do on every status: Errors and retries.
| Status | Means |
|---|---|
| 400 | models is not a list of up to 200 model names |
| 402 | the balanceBalanceWhat you can spend, in DA.More on Balance and charges → cannot cover the request |
| 403 | a model you named is not one the key may use |
| 404 | no such model |