Prompt
How do I set fallback rules for LLM requests when a provider fails?
Latest observation
Nov 2, 2026
OpenAI APIWeb search: off
A good way to set fallback rules is to define a routing policy that decides:
- When to retry
- When to switch providers/models
- How many attempts are allowed
- What counts as a failure
- Whether to fail open or fail closed
Practical fallback strategy
1) Classify failures
Not every error should trigger a fallback.
Retry same provider/model
- transient 5xx errors
- rate limits
- network timeouts
- connection resets
Fallback to another provider/model
- repeated 5xx errors
- provider outage
- quota exhausted
- unsupported model/context size
- persistent latency beyond threshold
Do not fallback automatically
- authentication failures
- invalid request schema
- safety/policy refusals
- bad prompt formatting
2) Use a priority chain
Define providers in order of preference:
Primary: OpenAI GPT-4.1
Secondary: Anthropic Claude
Tertiary: Google Gemini
Backup: smaller local model
Then add rules like:
- Try primary first
- Retry primary up to 2 times on transient errors
- If still failing, move to secondary
- If secondary also fails, continue down the chain
- Stop after max attempts or return a graceful error
3) Set thresholds
Common thresholds:
- Retries per provider: 1–3
- Timeout per request: 10–30 seconds
- Total time budget: 30–60 seconds
- Circuit breaker: temporarily disable a failing provider after repeated errors
- Cooldown window: e.g. 5 minutes before trying it again
Example logic:
- 1st attempt: primary
- 2nd attempt: primary retry
- 3rd attempt: secondary
- 4th attempt: tertiary
- Final: return fallback response or error
4) Keep request compatibility in mind
Fallback only works if the backup model can handle the request.
Check:
- context length
- tool/function calling support
- structured output compatibility
- multimodal support
- streaming support
- JSON/schema adherence
If the fallback model is weaker, you may need to:
- shorten the prompt
- remove optional context
- relax output format requirements
- post-process output
5) Decide on response policy
When all providers fail, choose one:
- return an explicit error
- return a cached/stale answer
- return a simplified heuristic response
- queue for later processing
- notify the user and ask to retry
Example fallback policy
providers:
- name: primary
model: gpt-4.1
retries: 2
retry_on:
- timeout
- rate_limit
- server_error
- name: secondary
model: claude-sonnet
retries: 1
retry_on:
- timeout
- rate_limit
- server_error
- name: tertiary
model: gemini-pro
retries: 0
global:
request_timeout_ms: 15000
max_total_attempts: 4
fallback_on:
- timeout
- rate_limit
- server_error
do_not_fallback_on:
- invalid_request
- auth_error
- policy_refusal
Pseudocode
providers = [primary, secondary, tertiary]
for provider in providers:
for attempt in range(provider.retries + 1):
try:
return provider.call(request, timeout=15)
except TransientError:
if attempt == provider.retries:
break
continue
except FatalError:
raise
raise RuntimeError("All providers failed")
Best practices
- Log the failure reason and provider used
- Track latency and success rate per provider
- Prefer idempotent requests if retries are enabled
- Use exponential backoff for retries
- Avoid retry storms during outages
- Test fallback behavior regularly
If you want, I can also give you:
- a Python implementation
- a Node.js implementation
- or a YAML config pattern for provider fallback rules