Skip to content
All patterns
Reliability

Fallback model chain

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.

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
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
  • Cost variance across the chain
Production checklist
  • Pin model versions, not 'latest'
  • Normalise response shapes across providers
  • Alert when fallback rate exceeds threshold
  • Track per-model latency and cost separately
  • Test fallback path in CI