Fix Claude API 504 timeout_error on long requests
Claude API 504 timeout_error means the request timed out while processing. Move long requests to streaming or Message Batches, and check SDK retries.
504 - timeout_error: The request timed out while processing.A Claude API 504 timeout_error means the API ran out of time while processing your request, and the official fix is to move long-running requests to the streaming Messages API or the Message Batches API. The line above is the 504 entry from the official errors page, word for word; the page does not show the message text of a 504 response body.
[1] Decide first whether you got a server 504 or a client-side timeout. The three look alike but need different fixes.
| What you see | Who cut it off | In the Python SDK |
|---|---|---|
HTTP 504 with error.type timeout_error | The API timed out while processing | InternalServerError (500 and above), status_code 504 |
APITimeoutError | The SDK gave up waiting for a response; default 10 minutes | No response at all |
ValueError: Streaming is required … | The SDK refused before sending | The request never left |
Here are all three reproduced against local test servers. case1 is a non-streaming call with max_tokens=128000, case2 uses timeout=2.0 against a server that accepts the connection and never answers, and case3 is a server that returns 504 timeout_error (nothing was sent to the Anthropic API).
$ python3 sdk_check.pyanthropic 2.3.0case1: ValueError - Streaming is required for operations that may take longer than 10 minutes. See https://platform.claude.com/docs/en/cli-sdks-libraries/sdks/python#long-requests for more detailscase2: APITimeoutError - Request timed out or interrupted. This could be due to a network timeout, dropped connection, or request cancellation. See https://docs.anthropic.com/en/api/errors#long-requests for more details. | attempts=3 elapsed=7.4scase3 (default): InternalServerError 504 timeout_error | requests=3case3 (max_retries=0): InternalServerError 504 timeout_error | requests=1case1 matches the Python SDK docs: if a non-streaming request is expected to take longer than about 10 minutes, the SDK raises ValueError. Passing stream=True or setting timeout yourself turns that check off, but it does not make the request any shorter.
[2] Switch long requests to streaming. If you do not need to handle events one by one, the SDK can consume the stream for you and return the same Message object a non-streaming call returns. From the streaming docs:
client = anthropic.Anthropic() with client.messages.stream( max_tokens=128000, messages=[{"role": "user", "content": "Write a detailed analysis..."}], model="claude-opus-5-5",) as stream: message = stream.get_final_message()In TypeScript, call client.messages.stream(...) and then await stream.finalMessage(). curl only prints the raw events and cannot combine them into a final message. If a stream breaks partway, save what arrived; on Claude 4.6 and later models, send a user message that contains the partial response and asks the model to continue from where it left off (putting the partial text in an assistant message is the method for 4.5 and earlier). tool_use and thinking blocks cannot be partially recovered, so resume from the most recent text block.
[3] Move work that does not need an immediate answer to the Message Batches API. You poll for results instead of holding a connection open, which suits long generations on unreliable networks. What the docs state:
- Most batches finish within 1 hour; results are available when every request is done or after 24 hours. Requests not processed within 24 hours end as
expiredand are not billed. - Usage is charged at 50 percent of standard API prices.
- A batch holds up to 100,000 requests or 256 MB, whichever comes first.
[4] Count the retries. The SDK retries connection errors, 408, 409, 429 and 500 and above twice by default, so 504 is included, and timed-out requests are also retried twice. In case2 and case3 above, one call reached the server three times. Apply that rule to the default 10-minute timeout and a long non-streaming request can hang for more than 30 minutes before it reports failure. On paths where a user is waiting on screen, lower timeout or max_retries, and for the real fix move to step 2 or 3.
client = anthropic.Anthropic(timeout=120.0, max_retries=1)If your code has its own retry loop, set max_retries=0 so the two do not stack and multiply requests. Whether a request that ended in 504 is billed is not stated in the docs.
[5] Turn on TCP keep-alive if you call HTTP directly. The errors page says that with a large max_tokens and no streaming, some networks drop idle connections, so the request can fail or time out without a response, and it suggests a TCP socket keep-alive. The official SDK already sets that socket option. If a proxy or load balancer sits in between, check its idle timeout too.
[6] If 504s keep coming, log the request ID and contact support. Every response carries a request-id header, error bodies repeat it as request_id, and the Python and TypeScript SDKs expose it as _request_id.
For overload 529s, see Claude API 529 overloaded_error: retry and back off. For a response that arrived but was cut short, see stop_reason. For a wrong path or model name, see Claude API 404 not_found_error. Stream drops in Claude Code have their own page: Stream idle timeout.
Check it with the HTTP status codes tool ↗ — calculated in your browser.