mediumAI Engineering

How do you build resilient LLM calls with rate limits, retries and fallbacks?

1.2k views
01

Understand the problem

429s, overloaded errors and provider outages: the reliability layer every LLM app needs.

rate-limitsretriesfallbacksreliability
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Resilience wrapper with fallback chain
const TIERS = [
  { name: "primary",  call: (r: Req) => anthropicUsEast(r) },
  { name: "region2",  call: (r: Req) => anthropicEuWest(r) },
  { name: "smaller",  call: (r: Req) => anthropicUsEast({ ...r, model: SMALL_MODEL }) },
];

async function completeResilient(req: Req) {
  for (const tier of TIERS) {
    if (breaker.isOpen(tier.name)) continue;              // fail fast past dead tiers
    try {
      return await retry(() => tier.call(req), {
        attempts: 3, backoff: expJitter(500), retryOn: [429, 500, 502, 503, "ETIMEDOUT"],
      });
    } catch (e) { breaker.record(tier.name, e); }
  }
  return degradedResponse(req);                            // cached answer + notice
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.