Claude API Request Timeout Configuration Guide
Claude API Request Timeout Configuration Guide
If your Claude API calls are failing with timeout errors, or you're trying to decide what timeout value to set before shipping to production, the short answer is: set your client-side timeout higher than Claude's typical generation time for your max_tokens value, and always prefer streaming over waiting for a single blocking response. Most timeout problems aren't server-side limits — they're client HTTP libraries giving up too early on long-running completions.
This guide covers how request timeouts actually work with Claude's API, how to configure them correctly in common HTTP clients, and how to avoid the two most common failure modes: premature client timeouts and silently dropped streaming connections.
Why Claude API requests time out
Claude generates tokens sequentially. A response with a large max_tokens value, a complex reasoning task, or a long input context can legitimately take 30 seconds to several minutes to complete. The timeout you hit is almost never an API-side rejection — it's your HTTP client (fetch, axios, requests, curl) closing the connection because it assumes something is wrong.
Common causes:
- Default timeouts that are too aggressive. Many HTTP libraries default to 10–30 seconds, which is far too short for non-trivial completions.
- Non-streaming requests on long generations. If you wait for the full response instead of streaming tokens, you're exposed to the entire generation time as a single blocking call.
- Reverse proxies and load balancers. Nginx, API gateways, and serverless function platforms often have their own timeout ceilings (e.g., 30s on many serverless defaults) that cut off the connection before your own client timeout is reached.
- Idle connection timeouts on networks or proxies that kill connections with no data for N seconds — which can happen during streaming if there's a pause between tokens.
Setting timeouts in common HTTP clients
curl
curl https://api.anthropic.com/v1/messages \
--max-time 120 \
--connect-timeout 10 \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4",
"max_tokens": 4096,
"messages": [{"role": "user", "content": "Summarize this report."}]
}'
--max-time caps the whole request; --connect-timeout only caps the TCP connection phase. Set --max-time generously for large max_tokens.
JavaScript (fetch with AbortController)
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 120000); // 120s
try {
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": process.env.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-opus-4",
max_tokens: 4096,
messages: [{ role: "user", content: "Summarize this report." }],
}),
signal: controller.signal,
});
const data = await res.json();
} finally {
clearTimeout(timeoutId);
}
Python (requests)
import requests
response = requests.post(
"https://api.anthropic.com/v1/messages",
headers={
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
json={
"model": "claude-opus-4",
"max_tokens": 4096,
"messages": [{"role": "user", "content": "Summarize this report."}],
},
timeout=(10, 120), # (connect, read)
)
Rule of thumb for choosing a timeout value
There's no single correct number — it depends on max_tokens and model. As a starting point:
- Short responses (≤1024 tokens): 30–60 seconds is usually enough.
- Medium responses (1024–4096 tokens): 60–120 seconds.
- Long responses (4096+ tokens) or complex reasoning tasks: 120–300 seconds, or switch to streaming so you don't need a single long blocking wait.
If you're unsure, set the timeout generously (e.g., 180s) and rely on streaming to get partial output early rather than guessing a tight number.
Use streaming to avoid timeout issues entirely
Streaming changes the problem from "wait for one big response" to "keep reading tokens as they arrive." As long as data keeps flowing, most clients and proxies won't trigger a timeout — you only need to worry about total idle time, not total completion time.
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"x-api-key": process.env.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-opus-4",
max_tokens: 4096,
stream: true,
messages: [{ role: "user", content: "Write a detailed report." }],
}),
});
const reader = res.body.getReader();
// read chunks as they arrive, resetting any idle-timeout logic per chunk
This is also where a gateway layer helps. SubToAPI exposes Claude as a standard HTTPS API with streaming support out of the box — see /docs/streaming — so you don't have to re-implement retry and reconnect logic for every service that calls Claude. Each application gets its own sub_live_... key, and you can tune timeout and retry behavior per integration rather than per Anthropic account.
Handling timeouts gracefully in production
- Retry with backoff, but only on connection-level timeouts, not on legitimate long generations you cut off yourself.
- Reduce
max_tokensif you consistently hit timeouts — a smaller cap finishes faster and is often enough. - Check proxy/gateway limits separately from your application code. If you're behind Nginx, AWS API Gateway, or a serverless function, their timeout ceiling may be shorter than what you set in your HTTP client, and it will win.
- Log duration per request so you can see your p95/p99 completion times and size your timeout against real data instead of guessing.
- Centralize key and timeout management. If multiple services call Claude, managing timeout, retry, and streaming logic in one place — rather than duplicating it in every codebase — reduces inconsistent behavior. This is one of the reasons teams put a layer like SubToAPI (see /docs/quickstart) between their apps and the underlying model access.
questions
What's a safe default timeout for Claude API requests? 60–120 seconds for typical responses under 4096 tokens. Increase it for longer max_tokens values, or use streaming so you're not dependent on a single long wait.
Why does my request time out even though Claude eventually responds? Your HTTP client, proxy, or serverless platform likely has a shorter timeout than the actual generation time. Check all layers — client library, reverse proxy, and hosting platform — not just your own code.
Does streaming prevent timeout errors? It significantly reduces them, since data keeps flowing instead of one long blocking wait. You still need to handle idle-timeout limits if there's a gap with no tokens, but total completion time stops being the main risk factor.