Skip to content
mentoor

Topic 5 of 5 · 5 min read

Falling back to another model or provider

Goal: Design a fallback chain that switches model or provider on overload and checks the fallback output still meets the contract.

A fallback sends a failed request to a second model or provider, but it only helps if the prompt works there and you check that the answer still meets the output contract. Fallbacks protect you from overload and outages, which retries on the same provider cannot fix.

Check yourself · 1 of 3

Which error is the best trigger for switching to a backup model?

Choose one answer

Why it matters

Providers have bad hours. Claude returns a 529 when it is overloaded, and any vendor can have an incident. If your product has one model behind it, the product is down with it. A second route keeps the feature working, perhaps at lower quality.

When to fall back

  • Fall back on overload, repeated 5xx, timeouts after your retries are used up, and an outage.
  • Do not fall back on a 400 or 413, since the second provider will reject the same request. Do not fall back on 401 either, because the key is the problem.
  • Treat 429 carefully. If your quota is spent, a second provider has its own quota and is a good route. Wait if the retry-after is short.

Design the chain

  1. List the order: primary model, a smaller model from the same vendor, then another vendor.
  2. Keep prompts portable. Prompts tuned to one model's habits may fail on another. System-prompt style, tool-call formats and structured-output settings differ between vendors. Keep a tested prompt version for each target.
  3. Check the contract. The fallback must return what your code expects: valid JSON that matches the schema, within length, in the right language. Validate every answer, and if it fails, move to the next step or return a clear error.
  4. Record which model answered. Log it with the request id, so you can see quality shifts and bill correctly.
  5. Limit the chain. Two or three steps with a cap on total time, so a user is not waiting through five failures.
  6. Test the fallback. Force the primary to fail in a test and run a sample set through the backup.
CHAIN = [
    ("claude-primary", call_claude),
    ("openai-backup", call_openai),
]

def generate(task):
    for name, call in CHAIN:
        try:
            out = call(task.prompt_for(name))
            if validate(out, task.schema):
                log(model=name, ok=True)
                return out
        except (Overloaded, ServerError, Timeout):
            log(model=name, ok=False)
    raise AllModelsFailed(task.id)

Before and after

Before: when the primary returns 529, the user sees an error for the length of the incident. After: the backup answers in a second or two. The structured output is validated, and one request in the log is marked as served by the backup.

What goes wrong

  • Untested backup. The first time it runs is during an incident, and it returns text instead of JSON.
  • Same prompt everywhere. Format and tool-call details break on a different model.
  • Silent downgrade. Users get weaker answers and nobody sees it in the logs.
  • Data rules ignored. A second provider may be outside the region or contract your customer data is allowed in.

Examples

Overload at peak time

At 9am the primary model returns 529 for 40 minutes. The chain retries twice, then calls a smaller model from the same vendor with the same prompt, then a second vendor with its own prompt. About one in ten answers from the backup fails schema validation and is retried on the next step, which the log shows.

A fallback that broke the contract

A summary feature expects a JSON object with a bullets array. The backup model wraps the JSON in a sentence of prose. The parser throws, and users see an error during an incident that fallback should have hidden. Validation plus a backup-specific prompt fixes it.

Key points

  • Fall back on overload, repeated 5xx errors and outages, not on 400, 401 or 413 errors that any provider would reject.
  • Keep a tested prompt for each model in the chain, because formats and tool-call details differ between vendors.
  • Validate every fallback answer against the same schema and length rules as the primary.
  • Log which model answered each request so quality shifts and costs are visible.
  • Test the fallback by forcing the primary to fail, and check data and region rules for every provider in the chain.

Common mistakes

  • Never running the fallback path until a real outage.
  • Sending the primary's prompt unchanged to a different vendor.
  • Falling back on 400 errors, which every provider will reject.
  • Not recording which model produced each answer.

Check yourself · 2 of 3

A backup model returns a friendly paragraph where your code expects a JSON object. What should have caught this?

Choose one answer

Check yourself · 3 of 3

Why should each model in a fallback chain have its own tested prompt?

Choose one answer

Practice items, not counted towards your mastery.

Start here