NNabeel Hassan

Blog · November 27, 2026 · 8 min read

Calling LLM APIs in Production: Timeouts, Retries, Fallbacks and Spend Caps

By Nabeel Hassan — AI Engineer · ICPC World Finalist

TL;DR: An LLM API call is a slow, expensive network request to a service you do not control, and it will fail in production in ways it never failed on your laptop. For a small AI product I wrap every model call in the same five things: a timeout sized to the job, retries with backoff only for errors that are worth retrying, a fallback (a second model, a cached answer or an honest "try again") decided per feature, validation of the output before anything downstream uses it, and a spend cap with alerts. None of it is clever. All of it is the difference between "the AI was down for an hour" and "nobody noticed".

The first version of every AI feature I have built looked the same: take the user's input, call the model, show the result. It works in the demo. Then real users arrive, and the model API has bad minutes, some responses come back malformed, long inputs take far longer than short ones, and a bug in a loop can spend a week's budget overnight.

I have hit versions of this on my own products, LectureNotes AI, where a recorded lecture goes through transcription and then summarisation, and Lifemaxxing AI, where the model builds a personalised plan. I have also seen it in client voice and chat agent work, where the model call sits in the middle of a live phone conversation. The products differ. The wrapper around the call ends up almost identical.

Why model calls fail differently

Most APIs a small product calls are fast and boring: a payment, a database write, an email. Model APIs break the assumptions those calls let you get away with.

So the question is never "will this call fail". It is "what does the user see when it does".

1. Timeouts sized to the job

The default timeout in most SDKs is generous, sometimes several minutes. That is the right default for a library and the wrong one for your product. A user staring at a spinner for ninety seconds has already left.

I set timeouts per feature, based on what the user is doing:

Streaming changes the picture in a useful way. When the response streams, I care about two numbers: time to first token and total time. A short timeout on the first token catches a stuck request quickly, while the full response is allowed to run longer.

2. Retries, but only for the right errors

Retries go wrong in two directions. With none, a single overloaded response becomes a user-facing error. With too many, an outage turns into a flood of repeated requests that worsens the rate limiting and multiplies the bill.

The rule I use:

Retries use exponential backoff with jitter: wait a little, then longer, with some randomness so a batch of failed requests does not all retry at the same instant. If the provider sends a Retry-After header, respect it. I cap retries at two or three for interactive features. Past that point the user is better served by the fallback than by a fourth attempt.

One trap worth naming: if your code calls a model inside a job runner or an automation tool that also retries, you now have retries stacked on retries. Three attempts inside a step that the workflow engine retries three times is nine calls. I check this on every n8n workflow and queue worker, the same failure family I cover in when post-call automations fail.

3. A fallback, decided per feature

When retries run out, something still has to happen. The right fallback depends on the feature, and deciding it ahead of time is most of the work.

Fall back to a second model

For features where any competent model will do, a second model from another provider is the strongest fallback. Provider outages are rarely simultaneous. The cost is real, though: a different model means a different prompt behaviour, so the fallback model has to pass the same test set as the primary, the approach I describe in testing LLM output in a small AI product. An untested fallback is a second, silent product.

I keep the provider and model as configuration, not code, so switching the primary during an incident is a config change. A kill switch or feature flag per AI feature does the same job when the right answer is to switch a feature off entirely.

Fall back to something cached or simpler

Not every feature needs a fresh generation. A plan generated yesterday is a fine thing to show while today's update fails. A summary can show the raw transcript with a note that the summary is being retried.

Fall back to an honest message

For some features there is no good substitute, and pretending otherwise is worse than saying so. "We could not generate your summary right now. We will retry automatically and notify you" is a fine product experience if the retry actually happens. Queue the work, retry it in the background, and tell the user when it lands.

In voice agents the fallback is a conversational one: the agent says a holding line, takes a message or transfers to a human. That design is its own topic, covered in voice agent outages and failover.

4. Validate the output before you trust it

A model call that returns 200 has only finished the first half of its job. Before anything downstream uses the output, I check it:

A validation failure is treated like a retryable error, usually with one retry, sometimes with a slightly firmer instruction about the format. If it fails again, it goes to the fallback path like any other failure. Validation that only logs and then passes the bad output through is not validation.

5. Cap the spend and watch it

Model calls are the one dependency where a bug has a direct, unbounded cost. Three things keep that bounded:

I also log tokens in and out per call, tagged by feature. It answers "which feature, since when" in a minute when the bill moves, and feeds straight into the numbers in AI app unit economics.

Put it in one wrapper

The practical upshot is that no feature in my AI products calls the provider SDK directly. Every call goes through one small internal function that takes the feature name, the prompt, the expected output schema and the fallback policy, and handles timeouts, retries, validation, logging and spend tagging in one place.

It pays for itself the first time a provider has a bad hour, because the fix is in one file. It also makes switching models a configuration decision, which keeps you out of the kind of lock-in I wrote about in voice agent platform lock-in.

A checklist for every model call

Before an AI feature ships, I want a clear answer to each of these:

  1. What is the timeout, and is it sized to whether the user is waiting?
  2. Which errors are retried, how many times, with what backoff, and is anything else also retrying?
  3. What does the user see when retries run out: a second model, a cached or simpler result, or an honest message with a background retry?
  4. Is the output validated for structure, completeness and sanity before anything uses it?
  5. Is there a provider spend limit, an in-product limit, and an alert on spend and failure rate?
  6. Is the call going through the shared wrapper, with model and provider as config?

If any answer is "I'm not sure", that is the feature that will break first in production.


I build AI products, voice and chat agents and the backends behind them for startups and agencies in the US, UK and Europe, and I design them to keep working on the days the model provider does not. If you want that on a new build, or a review of how your current AI features fail, read more about my background or book a call.

FAQ

Which LLM API errors should be retried?

Retry timeouts, connection errors, 429 rate limits and 5xx or overloaded errors, because they are temporary. Do not retry 400 bad requests, authentication errors, content-too-long errors or policy refusals, since the same request will fail the same way and needs a code fix or a different path.

How long should an LLM API timeout be?

It depends on whether a user is waiting. Interactive features need a short timeout, ideally on time to first token when streaming. Background jobs like summarising a long transcript can wait much longer. Real-time voice agents need the shortest of all, because the call sits inside a latency budget for each conversational turn.

Should I use a second model provider as a fallback?

For features where any competent model will do, yes, because provider outages are rarely simultaneous. But the fallback model must pass the same test set as the primary, and the provider and model should be configuration rather than code so you can switch during an incident.

How do I stop LLM API costs from running away?

Set a usage limit at the provider below the amount that would hurt, use separate keys per product and environment, add per-user and per-day limits inside the product, log tokens per call tagged by feature, and alert when daily spend jumps above its usual level rather than when the budget is gone.

Building something in this space?

I take on AI-agent, automation and product work directly — scoped fast, shipped fast.

Book a discovery call →

Keep reading