# 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.

List backup models so an agent stays up

**Slug**: `agent-fallback`  
**URL**: https://anyrouter.dev/use-cases/agent-fallback  
**Markdown**: https://anyrouter.dev/use-cases/agent-fallback.md  
**Short link**: https://anyrouter.dev/u/agent-fallback  
**Other short links**: https://anyrouter.dev/u/fallback  

## 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.

## Set it up

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.

## Code

### curl · models fallback

```bash
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." }]
  }'
```

### AI SDK

```ts
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)
```

## Related

- [free-tier-keys](https://anyrouter.dev/use-cases/free-tier-keys)
- [virtual-presets](https://anyrouter.dev/use-cases/virtual-presets)
- [hermes-agent](https://anyrouter.dev/use-cases/hermes-agent)
