UptimeSDKs
Fall back when a provider goes down
List backup models in one request. If the primary model's providers are down or rate-limited, AnyRouter tries the next model.
Set it up
curl · models fallback
$ curl https://anyrouter.dev/api/v1/chat/completions \
-H "Authorization: Bearer $ANYROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "z-ai/glm-4.7-flash",
"models": ["deepseek/deepseek-v4-flash", "google/gemma-4-31b"],
"messages": [{ "role": "user", "content": "Plan the next step." }]
}'- 1.Keep your primary model in model.
- 2.Add backup ids in models, in the order you want them tried. Fallbacks run on /api/v1/chat/completions.
- 3.Read the model field in the response to see which one answered. Or use anyrouter/auto and let AnyRouter pick a healthy model.
- 4.On your own keys, use anyrouter/byok: a 429 or 5xx on one key moves the request to your next key, and it never spends credits.
Why use this
An agent pinned to one model stops mid-task during a provider outage or a 429 burst. Retry loops in your code add latency and still hit the same provider.
How the request flows
Your agent
AnyRouter
Primary modelmodel
Backup modelmodels[0]
Other ways to set it up
AI SDK
import { createAnyRouter } from "@anyr/ai-sdk-provider"
import { generateText } from "ai"
const anyrouter = createAnyRouter()
const { text } = await generateText({
model: anyrouter("z-ai/glm-4.7-flash"),
prompt: "Plan the next step.",
providerOptions: {
anyrouter: {
extraBody: { models: ["deepseek/deepseek-v4-flash", "google/gemma-4-31b"] },
},
},
})
console.log(text)Endpoints and key
- OpenAI-compatible
- https://anyrouter.dev/api/v1
- Anthropic-compatible
- https://anyrouter.dev/api
- Key
- ANYROUTER_API_KEY (sk-ar-…)
Using your own provider keys? Add each one once, then call anyrouter/byok. Bring your keys →