Let the SDK retry, stream, read by type
Write integration code that relies on the SDK's retries and timeouts, honours retry-after, streams long requests and reads responses by content block type.
Scenario
A nightly job writes contract summaries. During a busy hour it gets rate-limited. Its own loop retries every exception five times, one second apart, on top of the SDK's retries. Each failed summary becomes up to fifteen calls, the throttling lasts longer, and the next morning a model upgrade makes content[0].text crash on half the responses.
None of that code was needed. The SDK already does the hard parts.
Explanation
Retries are built in. The official SDKs retry connection errors, 408, 409, 429 and 5xx errors twice by default, with exponential backoff, honouring the retry-after header when present. Set the count on the client: max_retries in Python, maxRetries in TypeScript (0 turns it off), or per request with with_options(...). Don't wrap calls in your own loop that retries every exception: it multiplies calls during an incident and retries errors that can't succeed, such as a 400 for a malformed request or a 401 for a bad key.
Timeouts are built in too. Requests time out after 10 minutes by default; change it with timeout. A timed-out request is retried like any other, so a timeout that is too short for the work costs you three attempts.
Stream long requests. Idle connections can be dropped on some networks, so the docs warn against a large max_tokens without streaming. The SDKs raise an error before sending a non-streaming request that is expected to take longer than about 10 minutes. Use client.messages.stream(...) and collect the result with get_final_message() (TypeScript: finalMessage()): you still get the complete Message at the end.
Over raw HTTP, you do the SDK's job. Streaming is server-sent events ("stream": true): message_start, then per content block content_block_start, content_block_delta events and content_block_stop, then message_delta and message_stop, with ping events in between. An error can arrive as an error event after the 200 status, and new event types may be added, so ignore unknown ones. On a 429, wait the seconds in retry-after before retrying. Not every 429 has it: a spend-cap 429 never does and keeps failing until access resumes. Retry 500 and 529 (overloaded_error) with backoff. Log the request-id header so support can find the call.
Read content by block type. response.content is a list of typed blocks: thinking, text, tool_use and more. With thinking on, thinking blocks come before the text, and a turn may or may not have one. Loop over the blocks and pick them by type; never assume content[0] is text.
Worked example
Before
for attempt in range(5):
try:
msg = client.messages.create(
model=MODEL, max_tokens=90000,
messages=messages)
break
except Exception:
time.sleep(1)
summary = msg.content[0].text
Up to fifteen calls (five loop attempts, each with the SDK's own three), retries on errors that can never pass, a long non-streaming call, and a crash the first time the response starts with a thinking block.
After
client = anthropic.Anthropic(max_retries=4)
with client.messages.stream(
model=MODEL, max_tokens=90000,
messages=messages,
) as stream:
msg = stream.get_final_message()
summary = "".join(
b.text for b in msg.content if b.type == "text")
The SDK retries only what can succeed, waits as retry-after asks, keeps the connection alive by streaming, and the text is read by type.