How a fallback chain keeps answering when a provider fails
Step 1 of 5
Primary model
The request goes to the first model in the ordered chain, with its own retry policy. Most requests are answered here.
Step 2 of 5
Provider error
After retries the call still fails with a rate limit or outage. The error is logged with the provider and model, and the chain falls through.
Step 3 of 5
Second provider
The same messages go to a model from a different provider, so one outage cannot take out both links. Response shapes are normalised so callers see one format.
Step 4 of 5
Cheap last resort
If that fails too, a cheaper model gives a degraded but working answer. Style and capability can differ, so this link is tested in CI like any other.
Step 5 of 5
Answer or alert
The first success is returned. If every link fails, the caller gets a clear error, and an alert fires whenever the fallback rate climbs above its threshold.
Each link has its own retries; only provider errors fall through, never bugs in your own code.
Problem
A single LLM provider has outages, rate limits or degradations. Your product should not go down with it.
Pattern
Define an ordered fallback chain of models. On failure (after retries), fall through to the next model in the chain. Each model has its own retry policy.
Implementation
fallback_chain.py
~306 tokens
import logging
from dataclasses import dataclass
import anthropic
import openai
log = logging.getLogger(__name__)
# Provider errors worth falling through on (outages, rate limits, timeouts).
# Bugs in your own code should still raise, not silently try the next model.
PROVIDER_ERRORS = (openai.APIError, anthropic.APIError)
@dataclass
class ModelConfig:
provider: str
model: str
max_tokens: int
temperature: float
# Model names change often; check your provider's model list (as of 28 October 2026). Replace <...-model> with a model id from that list.
FALLBACK_CHAIN = [
ModelConfig("openai", "<large-model>", 2048, 0.2),
ModelConfig("anthropic", "<other-provider-model>", 2048, 0.2),
ModelConfig("openai", "<small-model>", 2048, 0.2), # cheap last resort
]
def call_with_fallback(messages):
last_error = None
for cfg in FALLBACK_CHAIN:
try:
return call_model(cfg, messages)
except PROVIDER_ERRORS as e:
last_error = e
log.warning("Model %s/%s failed (%s): %s", cfg.provider, cfg.model, type(e).__name__, e)
raise RuntimeError(f"All models failed: {last_error}")
Trade-offs
Resilience to provider outages
Behavioural differences across models (temperature, style, capability)
Structured output schemas may not be supported uniformly