Model fallbacks
Use the models array so that a request moves on to the next model automatically when the first one cannot answer.
MarkdownOn this page
Provider routing retries the same model at different providers. Sometimes that is not enough: every provider of a model can be busy at once, or the model can be removed from the catalog. The models array adds backup models to a request. If the first model cannot answer, Lykia moves to the next, and your request comes back with a response instead of an error.
The models array
Put the model you want in model and the backups in models, in order of preference. Lykia tries model first, then each entry of models in turn. If you leave model out, the project's default model comes first.
curl https://api.getlykia.com/v1/chat/completions \
-H "Authorization: Bearer $LYKIA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"models": ["anthropic/claude-sonnet-4.5", "google/gemini-2.5-flash"],
"messages": [{ "role": "user", "content": "Write a one-line commit message for a typo fix in README." }]
}'import os
from openai import OpenAI
client = OpenAI(base_url="https://api.getlykia.com/v1", api_key=os.environ["LYKIA_API_KEY"])
completion = client.chat.completions.create(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Write a one-line commit message for a typo fix in README."}],
extra_body={"models": ["anthropic/claude-sonnet-4.5", "google/gemini-2.5-flash"]},
)
# completion.model is the model that actually answered.
print(completion.model, completion.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.getlykia.com/v1", apiKey: process.env.LYKIA_API_KEY });
const completion = await client.chat.completions.create({
model: "openai/gpt-4o",
models: ["anthropic/claude-sonnet-4.5", "google/gemini-2.5-flash"],
messages: [{ role: "user", content: "Write a one-line commit message for a typo fix in README." }],
} as OpenAI.Chat.ChatCompletionCreateParamsNonStreaming);
// completion.model is the model that actually answered.
console.log(completion.model, completion.choices[0].message.content);{
"id": "gen-1795373696-K7G5NzGy74o2qGXG",
"object": "chat.completion",
"created": 1795373696,
"model": "anthropic/claude-sonnet-4.5",
"provider": "anthropic",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "docs: fix typo in README"
},
"finish_reason": "stop",
"logprobs": null
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 9,
"total_tokens": 29,
"cost": 0.000195,
"is_byok": false,
"cost_details": {
"upstream_cost": 0.000195,
"platform_fee": 0,
"is_byok": false
},
"prompt_tokens_details": {
"cached_tokens": 0,
"cache_write_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 0
}
}
}Here openai/gpt-4o could not answer, so the request moved on to the first backup, anthropic/claude-sonnet-4.5. The model field of the response is the model that actually served the request, and provider is where it ran. The request's record keeps both the model you asked for (model) and the one that answered (model_resolved), along with every attempt.
modelstringThe model to try first.
modelsstring[]Backup models, in the order they should be tried. Each entry is an author/model id from the catalog. Repeated ids are ignored, and Lykia sets a maximum number of backups; entries past it are ignored.
When Lykia moves to the next model
For each model, Lykia first works through that model's provider candidates (see Provider routing). Only when none of them can answer does it move to the next model. The triggers are the same as for switching providers:
- a connection error or a timeout,
- a rate limit (
429) or a server error (5xx), - a provider rejecting the credentials it was given,
- the model no longer being offered.
A backup that cannot be used at all, because it is not in the catalog, is not allowed for the project or cannot currently be served by any provider, is skipped. If the first model is unusable in that way but a backup is not, the backup serves the request.
4xx errors caused by the request itself do not trigger a fallback. An invalid parameter or a malformed message list fails the same way on every model, so Lykia returns that error right away.
Combining with provider preferences
models and provider can be used in the same request; the provider preferences are applied to every model in the list. The request below tries the Gemini model first and moves on to the Mistral model only if Gemini cannot answer anywhere; the provider in ignore is used for neither model.
curl https://api.getlykia.com/v1/chat/completions \
-H "Authorization: Bearer $LYKIA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemini-2.5-flash",
"models": ["mistralai/mistral-small-3.2-24b-instruct"],
"provider": { "ignore": ["deepinfra"] },
"messages": [{ "role": "user", "content": "Classify this ticket as bug, question or feature: The export button does nothing." }]
}'Project settings still apply. Listing a model in models does not unlock it if the project does not allow it, so keep the list to models your project permits.
Choosing backups
- Match capabilities. If you rely on tool calling or structured outputs, check that every backup supports them via
supported_parametersin theGET /modelsresponse. Falling back to a model without them gives you a response you did not plan for. - Mind the context window. For long prompts, the backups need a large enough
context_lengthtoo. - Watch the price. A backup can cost more than your first choice. Read prices from the catalog as described in Models.
- Log what answered. Store the response's
modelfield and you will see how often requests end up on a backup.
Next steps
- Provider routing: set the provider order within a model.
- Errors: what you get back when every backup fails.
POST /chat/completions: every field of the request body.