Skip to main content
HTTP 504 Gateway Timeout
A gateway did not receive a timely response from the upstream server.
5xx · Server error✓ retryable with backoff
In AI APIs specifically
Long non-streaming generations are the usual trigger — the proxy's read timeout expires before the model finishes. Streaming avoids this because bytes flow continuously.
Fix checklist
- Switch to streaming for long generations.
- Raise gateway/proxy read timeouts above max generation time.
- Reduce max_tokens for latency-sensitive paths.
- Retry idempotent requests with backoff.
Retry handler (TypeScript)
async function fetchWithRetry(url: string, init: RequestInit, maxRetries = 5) {
for (let attempt = 0; ; attempt++) {
const res = await fetch(url, init);
// 504 is retryable — back off and try again.
if (res.status !== 504 || attempt >= maxRetries) return res;
const retryAfter = Number(res.headers.get("retry-after"));
const delay = Number.isFinite(retryAfter) && retryAfter > 0
? retryAfter * 1000
: Math.min(60_000, 1000 * 2 ** attempt) * (0.5 + Math.random()); // expo backoff + jitter
await new Promise((r) => setTimeout(r, delay));
}
}Spec: RFC reference
Related status codes
500 Internal Server Error
The server hit an unexpected condition.
501 Not ImplementedThe server does not support the functionality required to fulfil the request..
502 Bad GatewayA gateway or proxy received an invalid response from the upstream server..
503 Service UnavailableThe server is temporarily unable to handle the request — overload or maintenance.