June 2026 · 2 min read
Don't Block HTTP Requests Waiting for LLMs
Calling LLM APIs synchronously inside Laravel controllers leads to 504 Gateway Timeouts. Learn how to immediately return 202 Accepted and offload LLM calls to background queues.

The danger of synchronous LLM requests
I often see junior developers making direct HTTP requests to OpenAI or Anthropic endpoints inside synchronous web controllers. They then wonder why their application crashes when an API takes fifteen seconds to respond. Users will not wait that long. This slow response ruins the user experience. It also blocks system processes that should handle other traffic.
To solve this, your controller must immediately return a 202 Accepted response and offload the API call to a Laravel Queue worker. This pattern frees up your web worker in milliseconds. A background job then takes over the heavy lifting of calling the API. This isolates the work from the user's web request lifecycle.
public function store(Request $request)
{
ProcessAiRequest::dispatch($request->user(), $request->input('prompt'));
return response()->json(['status' => 'Processing task.'], 202);
}Isolating network timeouts
Outgoing HTTP connections sometimes do not respect queue job timeouts. For this reason, you must specify connection and request timeouts directly on your HTTP client. You can also configure exception throttling middleware to handle the inevitable HTTP client exceptions gracefully.
Updating the client asynchronously
When the background job finishes its call to the external LLM, it saves the generated content directly to your database. At that point, you can broadcast a completed event using Laravel Echo or Pusher to update your frontend user interface in real time. If real-time broadcasting is too complex for your current setup, you can set up simple frontend polling against a database status endpoint. This keeps the application responsive. It also keeps your application completely decoupled from upstream network latency.
Sources
- Queues | Laravel 13.x - The clean stack for Artisans and agents
- Best practices for handling long queue times with OpenAI's responses API - API - OpenAI Developer Community
- Laravel AI SDK | Laravel 13.x - The clean stack for Artisans and agents
- Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents - Laravel News
- Laravel Non-Blocking HTTP Requests in the Background
- Quick Tip: The Case for Cloaking for Large Language Models
Related articles
Escaping AI Harness Lock-In: One Prompt, Many Harnesses
Do not let your coding agent define your workflow. Learn how to export your AI logic into portable configuration files that function across any tool.
Laravel Queue Reliability: Failed Jobs, Retries, and Horizon in Production
Build a production-ready Laravel queue setup with explicit backoff strategies, database-level idempotency, Horizon supervisors, and reconciliation loops.
Handling LLM Rate Limits in Laravel Without Breaking Your Queue
Synchronous LLM calls in controllers lead to 504 errors and broken UX. Learn how to offload, throttle, and back off intelligently.
Durable Execution: Why I Used Cloudflare Workflows for AI Automation
Standard serverless functions crash on long AI pipelines. Here is how I used Cloudflare Workflows to handle slow APIs, self-critique loops, and stateful retries.