Skip to content
mentoor

Topic 6 of 6 · 4 min read

Fallback routing across providers

Goal: Configure a fallback chain across two providers and test that its outputs still pass the eval set.

A fallback chain sends a request to a second model when the first one fails, and it only helps if the second model has been tested as carefully as the first. An untested fallback turns an outage into a quality incident.

Check yourself · 1 of 3

Which error should normally trigger a fallback to a second model?

Choose one answer

Why it matters

Providers have outages and rate limits. A single-model feature goes down with them. A second model, ideally from another provider, keeps the feature answering. Gateways such as OpenRouter let you list several models and try them in order when the first errors, is rate-limited or is moderated, and you can also build the same logic in your own code.

How to set it up

  1. Choose what triggers a fallback. Timeouts, 5xx errors, 429 rate limits and provider outage are clear triggers. A 400 error is your bug, so do not fall back, fix the request.
  2. Pick the chain. Primary, then a secondary on another provider, so one outage does not take out both. Keep the chain short; every extra hop adds latency.
  3. Adapt the request per model. Parameters, tool formats and token limits differ, so each model in the chain needs its own settings, not a copy of the primary's.
  4. Test every link. Run the golden test set on the fallback alone. If it scores below your bar, either improve its prompt, pick another model, or accept degraded mode and show it clearly.
  5. Record which model answered. Log the model on every response. Otherwise a day on the fallback looks like an unexplained quality drop.
  6. Control refusals on purpose. Falling back after a content refusal sends the same request to another provider with different rules. Decide whether that is acceptable, per feature.
  7. Drill it. Force the primary to fail in staging and watch the chain work.

Before and after

Before, one model, no way out:

resp = call(model="provider-a/model-1", messages=msgs)

After, a tested chain with limited retry and logging:

CHAIN = [
    {"model": "provider-a/model-1-2025-06-01", "timeout": 20},
    {"model": "provider-b/model-2-2025-05-14", "timeout": 25, "prompt": "v2"},
]
for link in CHAIN:
    try:
        resp = call(**link, messages=msgs)
        log_model(resp.model)
        break
    except (Timeout, RateLimited, ServerError):
        continue
else:
    raise AllModelsFailed()

What goes wrong

  • Untested secondary. It formats JSON differently, and the parser fails the first time it is used in anger.
  • Same provider twice. Both models go down together.
  • Falling back on every error. A malformed request is retried on three models and bills three times.
  • Silent degradation. Users get weaker answers for a week and nobody sees it in the logs.

Examples

Fallback that returned bad JSON

A team added a secondary model but never ran the test set on it. During the first real outage it answered with prose around the JSON and the parser failed on every call. Running the test set earlier would have shown a 40% parse failure and led to a prompt change.

A drill in staging

Every month the team blocks the primary's hostname in staging, sends 100 requests and checks that the secondary answers within the time limit and the logs show the model name. The same run scores the answers against the golden set.

Key points

  • Fall back on timeouts, server errors and rate limits, but not on 400 errors, which mean the request itself is wrong.
  • Put the second model on a different provider so one outage does not take out the whole chain.
  • Each model in the chain needs its own parameters and prompt, tested on the golden set.
  • Log which model answered each request so a quality dip on the fallback is visible.
  • Decide per feature whether a content refusal should trigger a fallback, and drill the chain in staging.

Common mistakes

  • Adding a fallback model and never running the test set on it.
  • Choosing a fallback from the same provider, so a provider outage hits both.
  • Copying the primary model's parameters and prompt to the fallback unchanged.
  • Not logging which model answered, so degraded answers go unnoticed.

Check yourself · 2 of 3

A fallback chain has two models, both from the same provider. What risk does this leave?

Choose one answer

Check yourself · 3 of 3

Your chain's secondary model has never been scored. Before relying on it, what should you do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic