(Note · · 6 min read)
Your AI feature times out in production. The work is in the wrong place
It works on your machine and fails in production with a 504. The cause is usually not the model being slow; it is a slow call placed somewhere that was never allowed to take that long. Here is where the time actually goes, and where the work belongs instead.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
The defaults are set against you
The official OpenAI Node client defaults to a 600000 ms per-attempt timeout, which is ten minutes, and retries twice. Its own documentation notes that the total wait can exceed the timeout, because each retry starts a fresh attempt. So a single call can legitimately sit there for longer than half an hour before the client gives up.
Now compare that with where you deployed it. A serverless function is usually capped in seconds, and a load balancer or proxy in front of it has its own, often shorter, limit. The platform kills the request long before the client times out, which is why the user sees a 504 and your logs show no error from the model at all: nothing failed, the request was simply ended underneath it.
Two consequences follow, and both are worth fixing today. Set an explicit client timeout comfortably below your platform's limit, so the failure is yours and arrives as a real error you can catch. And know that a retry on a timeout is not free: the first attempt may still be running and billing, so blind retries can charge you twice for an answer nobody receives.
Decide whether a person is waiting
Almost every fix follows from one question: is a human sitting in front of this, watching for a reply?
- Someone is waiting — stream the answer. Tokens arrive as they are produced, the connection stays busy, and the perceived wait collapses even though the total time has not changed.
- Nobody is waiting — do not do it on the request. Screening a batch of applications, processing an uploaded document, generating a report: none of these needs to block an HTTP response, and all of them will eventually exceed one.
Most production AI features are the second kind, and are written as the first kind because that is the shortest path to a working demo.
Accept the job, then do the work
The pattern is old and boring, which is why it holds. The endpoint validates the request, writes a job record, returns immediately with its id, and a worker picks it up. The client polls that id or waits for a push. Nothing long-running lives inside a request.
POST /documents/:id/extract
-> validate, create job (status: queued), return 202 { jobId }
worker
-> claim job (status: running)
-> call the model, with an explicit timeout
-> store result, status: done | failed, record the error
GET /jobs/:jobId
-> { status, result?, error? }On Joblee and Paylio, the hiring and payroll products I build at Serviq, the AI screening, document and OCR processing, and payroll runs all work this way: async, event-driven pipelines of background workers and schedulers over PostgreSQL and Redis. The request path stays fast because the slow work was never on it.
The job record is the point
Keeping the job in memory, or as a promise nobody awaits, recreates the original bug in a new place. The moment the process restarts during a deploy, in-flight work vanishes with no trace and no way to tell the user. Persist the job where a restart cannot touch it, and make the worker claim jobs rather than receive them, so a crashed worker's job becomes available again instead of disappearing.
Store the provider's own identifier for the call alongside your job. When you need to know whether something ran, was billed, or is still going, that id is the only thing that can tell you.
Bound everything, then watch it
A queue removes the timeout from the user's request; it does not remove the failure. Put a concurrency limit on the worker so a burst of uploads cannot open hundreds of parallel calls into a rate limit. Give every job a deadline and a maximum number of attempts, and let it fail honestly when it passes them.
Then monitor for the specific failure a queue introduces: jobs that are accepted and never finish. They do not raise errors and do not appear in latency graphs, because nothing returned. Alert on a job sitting in queued or running past its deadline, and make sure the interface can say so rather than spinning forever.
What to change first
- Set an explicit client timeout below your platform's request limit, so failures surface as catchable errors instead of 504s.
- Check your retry settings and confirm a retry cannot silently double the bill on a timeout.
- Decide, per feature, whether a person is waiting. Stream the ones where someone is.
- Move the rest behind a job record: return an id immediately, process in a worker.
- Persist job state outside the process, and let workers claim jobs so a deploy cannot lose them.
- Cap worker concurrency, give every job a deadline, and alert on jobs that pass it.
Common questions
Why does my OpenAI API call work locally but 504 in production?
Because the hosting platform ends the request before the client gives up. The official OpenAI Node client defaults to a 600000 ms (ten minute) timeout and two retries, while a serverless function is usually capped in seconds. Set an explicit client timeout below your platform's limit so you get a real error instead of a 504.
Should I retry an LLM call that timed out?
Not blindly. The first attempt may still be running and billing, so a retry can pay twice for an answer nobody receives. Set the retry count deliberately and bound the total time, remembering that total wait can exceed the per-attempt timeout because each retry starts fresh.
When should an AI feature run as a background job?
Whenever no person is waiting on the reply. Batch screening, document and OCR processing and report generation should return a job id immediately and run in a worker. Keep the request path only for cases where someone is watching, and stream those so the wait feels short.
How do I stop background AI jobs disappearing on deploy?
Persist the job record outside the process, in a database or a durable queue, and have workers claim jobs rather than hold them in memory. A claimed job whose worker dies can then be picked up again instead of vanishing with the process.
What should I monitor on an AI job queue?
Jobs that are accepted and never finish. They raise no errors and never appear in latency graphs because nothing returned. Give each job a deadline and alert when one stays queued or running past it.