Production AI Architecture
Intermediate 11 min read Production AI Architecture

Resilience and Fallbacks

Failing without failing the user

Model calls fail in colorful ways: 429s, 5xx provider outages, long hangs, malformed JSON. Resilience means timeouts tuned to streaming, retries with jitter, provider fallback, output validation retry loops, and graceful degradation to a non-model path.

fallbacktimeoutsretriesdegradationJSON repair
Prerequisites: GenAI Gateway
Before we start

What you will learn

  • Tune timeouts and retries for streams
  • Wire provider fallback with circuit breaking
  • Degrade gracefully to non-model paths
Introduction

What is this?

LLM resilience keeps the product working when a model call doesn't: request timeouts that respect streaming, exponential-retry with jitter on 429/5xx, provider failover through the gateway, validation that retries malformed output once with a repair prompt, and a last-resort degrade so the user gets something useful.

Motivation

Why do you need it?

  • Provider regions and models can all 500 at once; a single chain is fragile.
  • Hung streams without timeout produce silent user wait and cost leakage.
  • JSON/garbage outputs are a reliability event just like an error code.
Think about it

Mental model

Imagine

Fallbacks

Think of

A road with detours

You plan a primary route, a marked detour (provider B), signs when to slow (timeouts), and finally a footpath into town (degraded answer) instead of stranding the traveler.

Mechanics

How it works

  • Set connect/read timeouts sized above p99 latency; stream-aware, not total-only.
  • Retry 429/5xx with jittered exponential backoff and a cap cheap enough to survive.
  • Gateway fallback to a second provider or a retry with the repair step on bad output.
  • Degrade path: cache hit → small model → deterministic rule/policy answer.

Resilience ladder.

Call
Fallback
Repair
Degrade
Real world

In production

  • A chat app fails over openai → anthropic → small cached answers on outage.
  • A summarizer validates JSON and re-prompts once with the parse error included.
  • A copilot returns 'here's the plan' from cached snippets when the model tier is 429ing.
Guidelines

Best practices

  • Make every fallback real: test it by killing the primary, not in theory.
  • Use short retries and cap the total budget; a long retry storm is worse than one timeout.
  • Return degraded-but-useful results rather than empty failures where possible.
  • Log which fallback served each request — the trace tells you the health ladder worked.
Watch out

Common mistakes

  • No timeouts → hung streams burn users and budget.
  • Retry loops on 4xx-like prompt errors (they won't heal by retrying).
  • Fallback to a weaker model with no quality gate — a 'successful' response that's worse than failure.
When things break

Debugging tips

  • Fallbacks never fire → circuit breaker always open; test with fault injection.
  • Latency spikes look like provider issues → check gateway retry storms colliding.
  • Degraded answers surprise users → surface the fallback as a 'lite' response in the UI/trace.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

The first thing to tune for LLM resilience is?

2

Fallbacks should be periodically tested by failing the primary.

Go deeper

Official documentation