Claude API Streaming Response Timeout Handling Guide
Streaming timeouts happen when a connection to the Claude API stays open longer than a proxy, load balancer, or client library expects, or when the model goes quiet long enough that your client assumes the connection is dead. The fix isn't one setting — it's a combination of correct client timeout configuration, server-side keep-alive handling, and retry logic that distinguishes a genuinely failed stream from a slow one.
This matters because streaming responses don't behave like normal HTTP requests. A single /v1/messages call with stream: true can stay open for 30+ seconds while tokens trickle in, which trips default timeouts in HTTP clients, reverse proxies, and serverless functions that assume a request either finishes fast or fails. Below is how to diagnose where the timeout is actually coming from and how to handle it correctly at each layer.
Where streaming timeouts actually come from
Before writing retry code, identify which layer is cutting the connection:
- Client library default timeout. Many HTTP clients (axios, fetch wrappers, requests in Python) apply a total request timeout of 10–30 seconds by default. Streaming requests violate that assumption because the total duration includes generation time, not just time-to-first-byte.
- Reverse proxy / load balancer idle timeout. Nginx, AWS ALB, Cloudflare, and most API gateways close connections after a fixed idle period (often 30–60 seconds) if no bytes are sent. If the model pauses between chunks longer than that window, the proxy kills the socket even though the upstream is still alive.
- Serverless function execution limits. Lambda, Vercel functions, and similar platforms have hard execution ceilings. A long streaming response can simply outlive the function's allowed runtime.
- Client-side read timeout vs connect timeout. These are different settings. A connect timeout governs how long to wait for the initial TCP/TLS handshake; a read timeout governs how long to wait between received bytes. Streaming failures are almost always read timeout issues, not connect timeout issues.
Fixing the wrong layer is the most common mistake — raising a client timeout does nothing if the real cutoff is an nginx proxy_read_timeout in front of your server.
Client-side timeout configuration
Set generous read/idle timeouts specifically for streaming requests, separate from your default API timeout:
const controller = new AbortController();
const idleTimeout = 90_000; // reset on every received chunk
let timer = setTimeout(() => controller.abort(), idleTimeout);
const response = await fetch('https://api.subtoapi.app/v1/messages', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.SUBTOAPI_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'claude-sonnet-4',
max_tokens: 1024,
stream: true,
messages: [{ role: 'user', content: 'Summarize this document.' }]
}),
signal: controller.signal
});
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
clearTimeout(timer);
timer = setTimeout(() => controller.abort(), idleTimeout); // reset idle window
processChunk(value);
}
clearTimeout(timer);
The key idea: use an idle timeout that resets on every chunk, not a total-duration timeout. A long response that keeps producing tokens shouldn't be killed just because it's taking 45 seconds overall.
Server-side and proxy configuration
If you're running your own proxy or backend in front of a Claude-compatible API, make sure intermediate layers won't close the connection prematurely:
- Nginx: raise
proxy_read_timeoutandproxy_send_timeoutwell above your expected max generation time (120s is a reasonable floor for long completions). - ALB / API Gateway: increase idle timeout settings explicitly — AWS defaults (60s on classic ALB) are too short for long Claude completions.
- Serverless functions: either increase max execution time or move streaming endpoints to a long-running service instead of a function with a hard ceiling.
If you don't want to manage this infrastructure yourself, routing requests through a service like SubToAPI removes this layer entirely — it exposes streaming over a stable HTTPS endpoint with keep-alive handling already configured, so you only need to tune your own client's read timeout. See the streaming docs for event format details and quickstart for setup.
Retry logic that doesn't duplicate output
Naively retrying a dropped stream from scratch re-sends the entire prompt and can produce duplicate or inconsistent output if partial tokens were already rendered to the user. Handle this correctly:
- Track what was already received. Buffer completed chunks client-side before the drop.
- On timeout/abort, discard the partial render if you can't resume mid-stream (most APIs, including Claude's, don't support resuming a stream from an arbitrary token offset).
- Retry with backoff, not immediately — a dropped connection under load will likely drop again if retried instantly.
- Cap retries (2–3 attempts) and fall back to a non-streaming request if streaming keeps failing — non-streaming calls are less sensitive to idle timeouts since you're just waiting for one full response.
async function streamWithRetry(payload, maxRetries = 2) {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await streamRequest(payload); // your streaming call
} catch (err) {
if (attempt === maxRetries) throw err;
await new Promise(r => setTimeout(r, 500 * 2 ** attempt));
}
}
}
Detecting a stalled stream vs a normal pause
Not every pause is a problem — Claude can take several seconds between chunks on complex generations, especially with tool use interleaved in the response (see tool streaming behavior). A practical heuristic: treat anything under 60–90 seconds of silence as normal, and only abort past that. If you're seeing stalls shorter than that consistently, the issue is almost certainly a proxy or load balancer timeout, not the model.
Questions
Why does my Claude streaming request time out even though the model is still generating? Almost always a read/idle timeout set too low in your HTTP client or an intermediate proxy — generation pauses are normal and get misread as a dead connection.
Should I use a total request timeout or an idle timeout for streaming? Use an idle timeout that resets on every received chunk. A total-duration timeout kills valid long responses; an idle timeout only fires when the stream actually goes silent.
Can I resume a dropped Claude stream from where it left off? No — there's no mid-stream resume. On failure, discard the partial output, retry the full request with backoff, or fall back to a non-streaming call.