Automatic LLM fallback and failover across multiple providers
A provider outage, a timeout or a dead local endpoint should not reach your users as an error. This guide builds a small multi-provider setup in Routerly 0.6.0 where a failing model is skipped, the request falls back to the next candidate, and a circuit breaker stops sending traffic to an endpoint that keeps failing. Every command below was run against a real Routerly 0.6.0 install and a real OpenAI model. The outputs are copied from those runs.
The example uses one working provider (OpenAI, model gpt-5-nano) and one that is switched off (a local Ollama endpoint that nothing listens on). Replace the OpenAI key and model with whatever you use.
What you need
- Routerly 0.6.0 running. The quick start covers the installer and the first login.
- The
routerlyCLI logged in as an admin. - An OpenAI API key in
OPENAI_API_KEY.
The examples call the service at http://localhost:3000. If you changed the port, change it in the curl commands too.
How a request is routed
When a request reaches a router, Routerly builds a ranked list of candidate models, tries the best one, and moves to the next if the attempt fails. Policies such as cheapest or health decide the ranking. The routing concepts page and the routing engine reference describe the full lifecycle.
Two mechanisms matter for failover:
- Fallback inside one request. If the first candidate returns an error, the next one is tried and the client still gets a normal response.
- Resilience state across requests. Repeated hard failures on the same provider open a circuit breaker, and the provider is excluded from the candidate list until it has had time to recover.
Step 1: register a real model and a dead one
Add the working OpenAI model:
routerly model add --id gpt-5-nano --provider openai --api-key "$OPENAI_API_KEY"
Add a local Ollama model whose endpoint is a closed port, to play the provider that is down:
routerly model add --id ollama/offline --provider ollama --endpoint http://127.0.0.1:9
routerly model edit ollama/offline --input-price 0 --output-price 0
The price is set to zero on purpose. The cheapest policy used below ranks free models first, so the dead model is always tried first. That makes the failover easy to observe. In a real setup the ranking comes from the policies you choose, and the order of attempts is not fixed (see the limits section).
Step 2: create a router with both models
routerly router create --name fallback-demo --timeout 6000
✓ Router "fallback-demo" created.
ID: f5a8003f-f385-4c89-8eb7-cec78fc1e70a
[...]
sk-rt-...
Save the token. It is the API key your clients use. The --timeout value is the time in milliseconds the router waits for the first byte of a streaming response from each model before it gives up on that attempt. The default is 2000. Reasoning models can need more, which is why this example uses 6000.
Add the models and the policy:
routerly router model add fallback-demo ollama/offline
routerly router model add fallback-demo gpt-5-nano
routerly router routing policy enable fallback-demo cheapest
routerly router show fallback-demo
Routing
Auto-routing: enabled
Routing model: (not set)
Fallback models: (none)
Policies: cheapest
Target Models
• ollama/offline
• gpt-5-nano
Step 3: send a request and watch the fallback
Use any OpenAI SDK, or plain curl, with the router token:
curl -s http://localhost:3000/v1/chat/completions \
-H "Authorization: Bearer $ROUTER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-5-nano","messages":[{"role":"user","content":"Reply with the single word: pong"}]}'
The response is a normal chat.completion from OpenAI with "content":"pong" and HTTP 200. The client never sees that the first candidate failed. Routerly returns the provider response unchanged.
Behind that response, the service log recorded the attempt on the dead model, then the success on the real one:
router:result {"final":[{"model":"ollama/offline","weight":2},{"model":"gpt-5-nano","weight":1}]}
model:error {"modelId":"ollama/offline","error":"Connection error.","latencyMs":2}
model:success {"modelId":"gpt-5-nano","inputTokens":13,"outputTokens":74,"latencyMs":1986, ...}
trace:recap {"outcome":"ok","durationMs":1993,"model":"gpt-5-nano","provider":"openai","attempts":2, ...}
Read it from the top. router:result is the ranked candidate list with weights. model:error is the failed attempt and its error. model:success is the attempt that answered. trace:recap is the summary: outcome ok, two attempts. The dashboard shows the same entries in the trace view of the Usage page, described in the usage documentation.
Step 4: let the circuit breaker take over
Send the same request five more times. Each one pays a failed attempt on the dead model first, then succeeds on gpt-5-nano. After five hard failures in 60 seconds, Routerly stops trying that provider:
routerly resilience status
┌──────────┬────────┬───────┬────────────┬──────────┬───────────────────────┐
│ Level │ ID │ State │ Last Fault │ Failures │ Opened At │
├──────────┼────────┼───────┼────────────┼──────────┼───────────────────────┤
│ provider │ ollama │ open │ server │ 5 │ 11/4/2026, 1:10:06 PM │
└──────────┴────────┴───────┴────────────┴──────────┴───────────────────────┘
The next request no longer touches the dead model. The log shows the exclusion and a single remaining candidate:
resilience:excluded {"excluded":[{"modelId":"ollama/offline","level":"provider","until":...}]}
router:result {"final":[{"model":"gpt-5-nano","weight":1}],"note":"single_candidate_bypass"}
trace:recap {"outcome":"ok","model":"gpt-5-nano","provider":"openai","attempts":1, ...}
attempts is now 1: the dead model costs no latency while the breaker is open.
The breaker is keyed by provider, not by model. In this example all models of the ollama provider are excluded together. Keep that in mind if you register several models behind one provider.
Recovery
An open breaker stays open for 60 seconds. After that, the next request is allowed through as a single probe. If the provider is still down, the probe fails and the breaker opens again. This is what happened here after waiting 62 seconds: the probe went to ollama/offline, failed with Connection error., the request fell back to gpt-5-nano and the status showed the breaker open again with one failure. When the provider is healthy again, the probe succeeds and normal ranking resumes.
You can clear the state by hand at any time:
routerly resilience reset
The Models dashboard page shows the health and resilience state of each model, with a per-row reset button.
Step 5: timeouts
The router timeout turns a hanging endpoint into a fast fallback, for streaming requests. Create a router with the default 2000 ms timeout whose first candidate is an address that never answers:
routerly model add --id custom/blackhole --provider custom --endpoint http://10.255.255.1:81/v1 --api-key sk-none
routerly model edit custom/blackhole --input-price 0 --output-price 0
routerly router create --name timeout-demo --timeout 2000
routerly router model add timeout-demo custom/blackhole
routerly router model add timeout-demo gpt-5-nano
routerly router routing policy enable timeout-demo cheapest
Send a streaming request ("stream": true). The stream completes with data: [DONE] after 3.9 seconds, and the log shows why:
model:error {"modelId":"custom/blackhole","error":"TTFT timeout after 2000ms","latencyMs":2000}
model:success {"modelId":"gpt-5-nano", ...}
trace:recap {"outcome":"ok","durationMs":3919,"model":"gpt-5-nano","provider":"openai","attempts":2, ...}
TTFT stands for time to first token. The first model got 2000 ms, then the request moved on.
Step 6: when every candidate fails
If no candidate can answer, the client gets an error instead of a hang. A router that contains only the dead model:
routerly router create --name dead-only --timeout 2000
routerly router model add dead-only ollama/offline
returns HTTP 503 to a chat completion request:
{"error":{"message":"All candidate models failed or are budget-exhausted.","type":"server_error"}}
Add at least one healthy model from a different provider to every router that must stay available.
Troubleshooting
The dead model is never tried and the first request already goes to the real one. The ranking decides the order. With no policy that separates the candidates, ties between models are broken at random, so either model can come first. Enable a policy such as cheapest (as in this guide) when you need a predictable order. See the routing engine reference.
routerly resilience status prints No resilience entries recorded yet. No hard failure has happened since the service started or since the last reset. This is the normal state of a healthy install.
A model I fixed is still skipped. The breaker stays open for 60 seconds and then allows one probe. Wait for the probe, or run routerly resilience reset.
A non-streaming request to a hanging endpoint takes many seconds before falling back. The router timeout applies to streaming requests only. In a non-streaming test against the unreachable custom/blackhole address with a 2000 ms router timeout, the failed attempt took 10.5 seconds before the request fell back to gpt-5-nano and returned HTTP 200 after 11.9 seconds. Use streaming when fast failover on a hung endpoint matters.
Streaming requests time out on reasoning models. The default 2000 ms can be shorter than the time a reasoning model needs to produce its first token. Raise it with routerly router edit <router> --timeout 6000.
503 All candidate models failed or are budget-exhausted. Every candidate failed or was excluded. Check routerly resilience status and the service log entries for the request.
Known limits
- This guide exercised connection errors and timeouts. It did not exercise provider rate limits (HTTP 429) or
retry-afterhandling, so it makes no claim about how long a rate-limited connection is paused. The routing engine reference and the Models page describe the resilience levels. - The
healthrouting policy, which excludes models by their recent error rate, was not part of this run. - The Usage trace view in the dashboard was not captured here. The trace lines above come from the service log.
- The
fallbackStrategyfield of routing profiles is documented as not wired into live retries. The fallback shown here is the next-best candidate in the ranked list. - The numbers (thresholds of five failures in 60 seconds and a 60 second open period) are those of Routerly 0.6.0 as observed in the runs above.
For the full command reference, see the CLI documentation. The routers concept page explains how routers, models and tokens fit together.