I'm Gopi Gugan, a software engineer building AI features with FastAPI, Postgres/pgvector, and React/TypeScript. You can find more of my work at gopigugan.ca. LLM APIs fail. Not if — when. Timeouts, rate limits, a provider outage at the worst possible moment. The difference between a feature that survives that and one that falls over is decided before you ship, not during the incident. Here's how I handle it.

Failures are part of the design, not an edge case

Every LLM call I make sits behind the assumption that it can fail. That changes how I design the feature from the start.

  • I list the failure modes up front: timeouts, 429 rate limits, 500s from the provider, malformed responses, and empty responses that technically "succeed."
  • I decide what the user sees for each one before I write the happy path.
  • I never let an LLM failure become a blank screen or a stack trace. A degraded answer with an honest label beats a crash every time.
  • I treat the LLM as an unreliable dependency, the same way I'd treat any third-party API.

Retries: small, bounded, and only for the right errors

Retrying everything is how a small outage becomes a big one. I retry only transient failures, and I cap it hard.

  • Retry on timeouts, 429s, and 5xx errors. Never retry on 400s or bad input — the next attempt will fail the same way.
  • Three attempts maximum, with exponential backoff and a bit of jitter so a fleet of retries doesn't hammer the provider in sync.
  • Set a total deadline for the whole operation, not just per attempt. A user waiting 30 seconds is a failure too.
  • Log every retry with the attempt number and the error. If retries are climbing, that's a signal, not background noise.
import asyncio, random async def call_llm_with_retry(call, max_attempts=3, deadline_s=20): started = asyncio.get_event_loop().time() for attempt in range(1, max_attempts + 1): try: return await call() except TransientLLMError as e: elapsed = asyncio.get_event_loop().time() - started wait = min(2 ** attempt + random.uniform(0, 1), deadline_s - elapsed) if attempt == max_attempts or wait <= 0: raise await asyncio.sleep(wait) 

Fallbacks: a cheaper model beats no answer

When retries are exhausted, I fall back instead of giving up. The fallback doesn't have to be as good — it has to be available.

  • My fallback chain is usually: primary model, then a smaller, cheaper model from the same provider, then a cached or template response.
  • A cached answer from a similar past query is a legitimate fallback for factual, repeatable questions. I label it as cached so the user knows.
  • For anything where a wrong answer is worse than no answer — summaries of a user's own documents, for example — the fallback is a clear message: "I couldn't generate this right now, here's the source material instead."
  • I test the fallback path deliberately. If it only runs during a real outage, it will be broken when it matters most.

Degrade the experience honestly, don't hide it

The UI is part of the failure handling. Users can tell when something is wrong; what they can't forgive is being misled about it.

  • In React, I keep the partial state visible. If a summary failed but the document list loaded, show the list and say the summary is unavailable.
  • I show a short, plain message — "The AI summary is temporarily unavailable" — not a technical error code and not a fake answer.
  • I never silently serve a stale or fallback answer as if it were fresh. A small "cached from earlier" label costs nothing and buys trust.
  • I add a retry button for the user. Sometimes the best recovery is letting them ask again in ten seconds.

What I don't do

  • I don't retry forever, and I don't queue failed requests to replay later unless the user asked for it. A surprise answer arriving minutes later is confusing.
  • I don't fall back to a different provider mid-conversation without thinking about it — context, cost, and output format all change.
  • I don't treat a fallback as a permanent fix. Every fallback event goes into the same log I review weekly, and if one model keeps failing, I switch the primary.
  • I don't promise reliability I haven't measured. If I haven't run the failure path in staging, I don't claim it works.

LLM features fail in boring, predictable ways, and that's good news — boring failures can be planned for. A small retry loop, a cheaper fallback model, and an honest UI message turn an outage from a broken feature into a brief inconvenience. The model is the exciting part of the system. The error handling is the part users actually remember.