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.
- They are slow and variable. A call can take one second or forty, depending on input length, output length and how busy the provider is. A single fixed timeout is wrong for at least half your features.
- They get rate limited and overloaded. You will see 429s when you exceed your limits and 5xx or "overloaded" errors when the provider is under load. Both are normal, both are temporary, and both happen at your busiest moment, because that is also everyone else's.
- Success is not the same as correct. A 200 response can still contain truncated JSON, an empty field, or text in the wrong shape. HTTP status tells you the call worked, not that the output is usable.
- Every call costs money. A retry is not free. A runaway loop is not a performance bug, it is a billing incident.
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:
- Interactive, user is waiting: short. If a chat reply or a quick rewrite has not started arriving in a few seconds, something is wrong and I would rather fall back than wait.
- Background, user is not waiting: long. Summarising an hour of lecture transcript runs as a job, and the user gets notified when it is ready. That call can take its time, and the timeout is there to catch a hung request, not a slow one.
- Real time, a caller is on the line: very short. In a voice agent the whole turn has a latency budget, which I break down in the voice agent latency budget. A model call that blows it is a failure even if it eventually succeeds.
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:
- Retry: timeouts, connection errors, 429 rate limits, 5xx and overloaded errors. These are temporary by nature.
- Do not retry: 400 bad request, authentication errors, content too long, policy refusals. Sending the same request again will produce the same answer. These need a code fix or a different path, not patience.
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:
- Structure. If I asked for JSON, parse it against a schema and check the required fields are present and the types are right. Structured output modes help a lot, which is one reason I push for structured output in products like the one I describe in personalised AI plans, not a chatbot, but I still validate.
- Completeness. Check the stop reason. A response that ended because it hit the token limit is truncated, even if it parses.
- Basic sanity. An empty summary, a plan with zero days, a callback number field with no digits. Cheap checks, real catches.
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:
- A hard limit at the provider. Most providers let you set a monthly budget or usage limit. Set it below the number that would actually hurt, and keep separate API keys per product and per environment, so a runaway test script cannot drain production. I cover the key side of this in handling API keys and secrets on client projects.
- Limits inside the product. Per user and per day caps on expensive actions, and a maximum input size. A free user should not be able to trigger an hour of transcription and summarisation on loop.
- Alerts on the trend, not just the total. I want to know when daily spend jumps well above the usual level, not when the budget is gone. The same goes for error rates and latency: one alert per AI feature on failure rate, wired into the setup from error monitoring and alerting for a small product.
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:
- What is the timeout, and is it sized to whether the user is waiting?
- Which errors are retried, how many times, with what backoff, and is anything else also retrying?
- 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?
- Is the output validated for structure, completeness and sanity before anything uses it?
- Is there a provider spend limit, an in-product limit, and an alert on spend and failure rate?
- 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.