All posts

LLM API fallback: how to keep your AI product running when a provider rate-limits or goes down

An LLM API fallback strategy is retries, a cooldown, and a second model or provider behind the first. How each layer works, what it costs in latency and quality, and what to decide before launch.

CodeKrypt 5 min read

Your AI feature works in the demo, then a provider returns a burst of 429s during your busiest hour and users see a spinner that never resolves. An LLM API fallback strategy is what turns that into a slower answer instead of a failed one. It is also one of the cheapest reliability decisions to make early and one of the most annoying to retrofit.

This post covers the layers, in the order you should build them, and the trade-offs each one introduces. It is about the domain, not any particular deployment: the sources are public docs and vendor write-ups, linked inline. Several of those vendors sell gateways, so read their framing accordingly.

What actually fails when you call an LLM API?

Three things, and they need different responses:

Layer 1: retries with backoff and jitter

Start here, because it costs nothing and fixes the common transient case. Wait longer after each failure, add randomness so a fleet of clients does not retry in lockstep, and read the Retry-After header when it is present. Maxim's sample code uses a cap of 5 retries, a 1-second base delay and up to 50% jitter; treat those as a starting point, not a standard.

Cap retries. A failed request still counts against your per-minute allowance, so unlimited retrying can keep you rate-limited longer.

Layer 2: a cooldown, so you stop hitting a failing endpoint

If a deployment has failed several times in a row, stop sending it traffic for a short while. LiteLLM's router docs expose this as allowed_fails (default 3) and cooldown_time (default 5 seconds), and note that rate-limit errors back off exponentially while generic errors retry immediately. Defaults change between versions, so check the current docs rather than copying numbers from a blog post, including this one.

Without a cooldown, every request pays the full timeout against a dead endpoint before it reaches the fallback.

Layer 3: the fallback chain itself

Once retries are exhausted, move to the next entry. There are four common shapes, summarised from Portkey's overview:

StrategyHow it worksCostMain risk
Sequential failoverTry primary, then backup on 429/5xx/timeoutNone extraAdds latency on the failing path
Status-code rulesDifferent errors route differently (429 to another key, 5xx to another provider)None extraRules need maintaining
Latency-threshold hedgingIf no answer within a set time, send to a second provider and take the first replyDuplicate requests billedHigher spend, more load
Proactive splitSpread traffic across providers or tiers all the timeDepends on mixMore surfaces to test

For most products, sequential failover with status-code rules is enough. Hedging is worth it only where latency matters more than the extra tokens.

Request flow: primary model, retries, cooldown, then fallback modelYour appPrimary modelRetry + backoffthen cooldownFallback modelFallback only after retries on the primary are exhausted

Is it safe to fall back to a different model?

This is the part vendors gloss over. A fallback that returns a worse or differently formatted answer can be a bigger problem than an error, because nothing flags it. Before a model goes into the chain:

  1. Run your existing evaluation set against it (see the AI agent evaluation framework).
  2. Check structured output, tool-call formats and context-window size. LiteLLM's enable_pre_call_checks exists because a fallback with a smaller context window will reject a long request outright.
  3. Decide what the user sees. Sometimes a visible "using backup model" state, or a degraded-but-honest answer, is better than a silent swap.
  4. Check data terms. A second provider is a second data processor, which matters for the compliance questions covered in the DPDP Act post.

What should you build yourself versus buy?

SituationReasonable choice
One model, one provider, early productRetry wrapper plus one backup model in your own code
Several models or teams, shared limitsAn open-source router or gateway such as LiteLLM
Need audit logs, budgets and per-customer routingA managed gateway, evaluated on price and lock-in

The same logic as build vs buy for AI features applies: buy the plumbing once it is costing you engineering time, not before. And note that routing between cheap and premium models for cost is a related but separate decision, covered in how to reduce LLM API costs.

What to do next

Before launch, answer five questions in writing:

  1. Which errors trigger a retry, and how many attempts?
  2. When does a failing endpoint go into cooldown, and for how long?
  3. What is the fallback chain, and has each entry passed your evaluation set?
  4. What is the user-facing timeout, and what do they see when it fires?
  5. Who is alerted when the fallback is carrying more than a small share of traffic?

If you are building an AI product and want this designed in rather than patched on after the first incident, that is the kind of work our AI SaaS service covers.

Sources

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

What is an LLM API fallback?
It is a rule that sends a failed model request somewhere else instead of showing the user an error. Usually that means retrying with backoff first, then moving to a second deployment, model or provider after a rate limit (429), a server error (500/503) or a timeout.
Should I retry or fall back when I get a 429?
Retry first, but respect the Retry-After header when the provider sends one and cap the number of attempts, because failed requests still count against your per-minute limit. If the limit is sustained rather than a burst, fall back to another key, model or provider.
Do I need an LLM gateway to do this?
No. A retry wrapper and a two-entry fallback list is a few dozen lines of your own code. A gateway such as LiteLLM or Portkey earns its place once you have several models, several teams, or need shared cooldown state and cost tracking.
Is falling back to a different model safe?
Only if you have tested it. A different model can follow your prompt differently, format structured output differently, or handle your tool definitions differently. Run your evaluation set against every model in the chain before it goes in production.

Related reading

Chat on WhatsApp