How Cloudflare 524 Timeouts Happen on Non-Streaming LLM Requests
A Cloudflare 524 (A Timeout Occurred) means Cloudflare opened a TCP connection to the origin server successfully, but the origin did not send an HTTP response before Cloudflare’s Proxy Read Timeout expired. On non-Enterprise Cloudflare zones, that default Proxy Read Timeout is 125 seconds (historically cited as 100s in older docs), and Cloudflare also enforces an unadjustable 30-second Proxy Write Timeout when uploading request bodies.
- Two distinct Cloudflare hops in LLM architectures: A
524can come either from your own Cloudflare proxy timing out while your backend waits on an LLM call, or from an upstream provider’s Cloudflare edge (cf_gateway_timeout) when a non-streaming inference/reasoning request takes too long before emitting its first byte. - Non-streaming buffers the entire completion: With
stream: false, the server holds the HTTP response headers back until every output and reasoning token finishes generating—pushing large outputs, slow models, or multi-step tool loops past the ~100–125s read window. - Intermittent occurrence tracks tail latency (
p95/p99): Requests fail intermittently when normal median latency is 40–70s, but occasional queue spikes, cold starts, or long completions cross the 125s cliff.
Steps to Debug and Fix Intermittent 524 Errors
- Identify which hop generated the
524: Inspect the response body, headers (cf-ray,server: cloudflare), and your origin access logs—if your origin server never logged a524and completed the LLM call after ~130s, your own Cloudflare proxy cut off the client; if your backend received the524from the provider URL, the upstream edge timed out. - Measure Time-to-First-Byte (
TTFB) directly against the origin: Runcurl -w "ttfb=%{time_starttransfer}s total=%{time_total}s\n"against both the proxied domain and the direct origin (or upstream endpoint) to see whether latency crosses ~100–125s or fails at 30s on large request payloads. - Switch upstream LLM calls to
stream: trueinternally: Even if your external API contract must return a single JSON response, consume the provider stream internally so bytes arrive within seconds (resetting idle read timers) and assemble the full result in your worker. - Move long-running client endpoints off synchronous proxied HTTP: For jobs that legitimately exceed 125s, return
202 Acceptedimmediately with a job ID and let clients poll status, or send periodic SSE keepalive comments/chunks, or route internal batch traffic through a DNS-only (grey-cloud) hostname.
TypeScript Example: Streaming Upstream Internally to Avoid 524 While Returning Non-Streaming JSON
import OpenAI from "openai";
const openai = new OpenAI();
export interface NonStreamingCompletionResult {
content: string;
ttftMs: number;
totalMs: number;
usage?: OpenAI.CompletionUsage;
}
/**
* Consumes the upstream LLM call with `stream: true` so intermediate chunks
* keep the connection active and prevent upstream 524 read timeouts,
* while returning a standard aggregated object to callers.
*/
export async function createResilientCompletion(
params: Omit<OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming, "stream">
): Promise<NonStreamingCompletionResult> {
const startedAt = performance.now();
let firstByteAt: number | null = null;
let usage: OpenAI.CompletionUsage | undefined;
const chunks: string[] = [];
const stream = await openai.chat.completions.create({
...params,
stream: true,
stream_options: { include_usage: true },
});
for await (const part of stream) {
if (firstByteAt === null) {
firstByteAt = performance.now();
}
if (part.usage) {
usage = part.usage;
}
const delta = part.choices[0]?.delta?.content;
if (delta) {
chunks.push(delta);
}
}
const finishedAt = performance.now();
return {
content: chunks.join(""),
ttftMs: Math.round((firstByteAt ?? finishedAt) - startedAt),
totalMs: Math.round(finishedAt - startedAt),
usage,
};
}
Helpful Tips for Isolating 524 Timeouts in Production
- Log
cf-ray,ttfbMs, andtotalMson every failed LLM request: Correlatingcf-rayheaders with origin timestamps immediately proves whether Cloudflare aborted at ~125s while your Node.js worker was still awaiting the upstream socket. - Beware blind retries on
524errors: Because the origin server often keeps running the LLM generation after Cloudflare disconnects, retrying a524immediately doubles token spend and GPU queue pressure. - Check
30supload timeouts on large multimodal or base64 payloads: If524errors happen around 30 seconds on requests with large images or long transcripts, you are hitting Cloudflare’s fixed 30s Proxy Write Timeout rather than the 125s read timeout—upload files to object storage and pass URLs instead.
You must log in or register to comment.
