How I keep LLMs from taking my app down in production
Every LLM in production fails you in one of three ways: it goes down, it hangs, or it costs more than you expected. I learned this on a side project called Raio-X de VizinhanΓ§a. You type any Brazilian ZIP code and it bu
Every LLM in production fails you in one of three ways: it goes down, it hangs, or it costs more than you expected.
I learned this on a side project called Raio-X de VizinhanΓ§a. You type any Brazilian ZIP code and it builds a report about the neighborhood: demographics from IBGE, points of interest from OpenStreetMap, the address from ViaCEP, climate data and Wikipedia history. Language models turn all of that into text a person actually wants to read.
That last step is the fragile one. Here is what broke, and what I put in front of the models so the app stays up.
The day the model disappeared
Raio-X used Gemini 2.5 models through OpenRouter. One day they were retired. No outage page, no drama: the model names just stopped working.
In a lot of apps that is a stack trace in front of a user. In Raio-X the site kept answering, because no feature calls a model directly. Every call goes through a chain:
fast profile:
1. NVIDIA Nemotron (lightning) timeout 30s
2. Gemini Flash via OpenRouter
3. Gemini Flash Lite via OpenRouter
4. StableHorde (last resort)
If the first link fails, times out or returns an empty answer, the next one takes over. The same afternoon I moved NVIDIA Nemotron to the top of the chain. I changed config, not screens, and AI responses started arriving in about 3 seconds.
Five controls I now put in front of every model call
1. Fallback between models
A "profile" is an ordered list of models, each with its own timeout. A fast profile for short answers, a reasoning profile with longer timeouts for the heavy text. The code that needs an answer asks for a profile, never for a model:
$response = LlmRelay::chat($prompt, profile: 'fast');
echo $response->model; // who actually answered
echo $response->latencyMs;
Models will change names, prices and terms. Your features should not care.
2. Timeouts that mean something
A model that hangs is worse than a model that fails: it holds a worker, the user stares at a spinner, and nothing gets logged. Every link in the chain has an explicit timeout, sized to the job. A one-sentence summary that takes 40 seconds is a failure, even if it eventually arrives.
3. Key rotation with cooldown
Free and cheap tiers have rate limits. With several keys per provider, the least recently used key goes first. A key that fails sits out for a while, longer after an HTTP 429, instead of being hammered by every request.
4. Cache
Many questions repeat. Same ZIP code, same prompt, same answer. A cache hit comes back in about 2 ms and costs nothing. This is the cheapest performance win in the whole stack, and the one most people skip.
5. Cost per call and a daily budget
Each call records tokens and cost. When the day's spend reaches the limit, calls stop before they hit the API:
try {
$response = LlmRelay::chat($prompt);
} catch (BudgetExceeded $e) {
// today's budget is spent: degrade gracefully
} catch (AllTargetsFailed $e) {
// $e->attempts says what each model and key did
}
A runaway loop calling a paid model at 3 a.m. is a bug you only want to pay for once.
The part nobody talks about: the queue
Fallback solved the model side. Then reports started getting stuck on "processing".
The text step takes around 70 seconds for a full report. I was running it from a web request that nudged the queue, and the web server killed that request long before the job finished. The job vanished, the report waited forever.
At the same time, the queue had piled up about 500 duplicate jobs. Every new report waited behind all of them, one job per poll.
What fixed it, none of it AI-specific:
-
A priority queue. Jobs for a report someone is looking at go to
high; batch reprocessing goes todefault. - Idempotent jobs. A job checks the report status first and skips if that step is already done, so duplicates cost nothing.
- A real worker. A cron job runs the worker every minute with a time limit, instead of relying on web requests.
The lesson: LLM work is slow work. Treat it like a video encode, not like a database query.
What I got wrong
A fallback you never exercise is a fallback you don't have.
After moving NVIDIA to the top, the Gemini links behind it stopped being used, and the OpenRouter account ran out of credit. They now answer with HTTP 402. NVIDIA is fast and stable, so nobody noticed. If NVIDIA had gone down, the chain would have skipped two dead links and landed on the slowest option.
I only caught it because every attempt is logged, not just the successful one. The fix is boring: a backup that doesn't share a billing account with the others, and an alert when a link in the chain has only failures. If you build something like this, log every attempt, and look at the failures of the links that never get used.
I turned it into a library
This layer started as a class inside Raio-X. I extracted it into an open source Laravel package, llm-relay:
- fallback between models, with per-model timeouts
- API key rotation with cooldowns
- response cache
- cost per call and a daily budget cap
-
php artisan llm-relay:reportwith calls, failure rate, latency and cost per model
It works with any OpenAI-compatible API (OpenAI, OpenRouter, NVIDIA, Groq, DeepSeek, Ollama) and with Gemini's native API.
composer require viniciuswerneck/llm-relay
The short version
- Never call a model directly from a feature. Call a profile.
- Give every model a timeout that matches the job.
- Cache everything that can repeat.
- Put a ceiling on daily spend.
- Run LLM work in a queue with priorities and idempotent jobs.
- Log every attempt, and test your fallbacks on purpose.
To me, that is what AI in production means: the app stays up when the model goes away.
I write about putting AI in production at lab.werneck.dev.br. How Raio-X was built, with the numbers measured in production: Raio-X de VizinhanΓ§a.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.