Urgent.News

What's breaking now, across thousands of outlets.

AI

Get the AI call out of your request handler

An API receives a PDF, asks a model to extract the fields, and returns the result. On your laptop it works: the model takes a few seconds, the response arrives, everyone is happy. The trouble starts when the second, the tenth and the hundredth user upload files at the same time. People often describe this as "the API freezes." In Node.js that's only half true, and the other half changes the fix.…

When an API receives a PDF file and asks a model to extract the fields, it returns the result. The process works smoothly on a laptop, with the model taking a few seconds to respond. However, when multiple users upload files simultaneously, the API may freeze. This issue is more than just Node.js' event loop being blocked by CPU-bound tasks like local OCR, image resizing, or PDF generation.

The real problem lies in the connection being held by someone, be it the user, proxy, gateway, or load balancer, each with its own timeout. Additionally, the AI service provider has limits on concurrent requests, tokens per minute, or both. When these limits are hit, calls wait or fail, making it difficult for the caller to determine which calls are being blocked.

Furthermore, paid work is wasted if the client gives up on a timeout, while the server keeps making calls, leading to unnecessary costs. Deployments and restarts also result in lost work, as a 20-second call can die with the pod. The API design involves accepting the request with a 202 Accepted status code, which indicates the request was accepted but processing may not have started and may still fail.

The client then polls the GET /jobs/{id} endpoint until the job's state is completed or failed. During this time, the API sends a Retry-After header, preventing the client from hammering the endpoint. Using a queue, such as Redis-backed BullMQ, ensures that requests are processed sequentially and avoids duplicate jobs caused by network timeouts. With a controlled window for keeping jobs, the system can manage paid model calls effectively.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

Move Slow LLM Calls Off the Request Path with BullMQ in Node.js

If an LLM call can take 20 seconds or more, keep it out of your HTTP handler. Put the work on a BullMQ queue backed by Redis, return a job ID right away with a 202 Accepted , and let a separate worker…

  • Move slow LLM calls to a separate queue using BullMQ in Node.js
  • Return immediate response while dedicated worker handles asynchronous processing
  • Improves retry policy, concurrency control, and failure handling

The Agent Added an Await to a Synchronous Function. The Caller Didn't Notice.

A teammate pasted a stack trace into our channel last week. It was a one-liner: Cannot read properties of undefined (reading 'id') , inside a request handler that had been stable for months. The diff that introduced it was tiny. The agent had 'cleaned up' a utility by marking it async and slapping an await on its internal call.

More from Saturday 14 November →