◉CLAUDE LABJP
●CC 2.1.325 — No new release over the weekend; 2.1.325 (11/13) is still the latest●BILLING — 2.1.325 fixes background sessions on a claude.ai login being billed to an API key from the shell●SONNET 4.5 — Retires on the Claude API on 11/30, 14 days left. Check saved model settings too●403 — Users report a Cloudflare 403 page mid-session that still burns tokens●MANAGED — Since 11/12 you can request self-hosted Claude Managed Agents that run entirely on your own infrastructure●NEW — When Browser Extension Is Not Connected Won't Go Away●CC 2.1.325 — No new release over the weekend; 2.1.325 (11/13) is still the latest●BILLING — 2.1.325 fixes background sessions on a claude.ai login being billed to an API key from the shell●SONNET 4.5 — Retires on the Claude API on 11/30, 14 days left. Check saved model settings too●403 — Users report a Cloudflare 403 page mid-session that still burns tokens●MANAGED — Since 11/12 you can request self-hosted Claude Managed Agents that run entirely on your own infrastructure●NEW — When Browser Extension Is Not Connected Won't Go Away
Articles/API & SDK
⬡ API & SDK/2026-04-24Advanced

Running the Claude API in Python Production — Rate Limits, Retries, and Timeouts

If you put Claude API into a real backend service, how you handle 429, 503, and read timeouts decides your reliability ceiling. This is the design I settled on after operating it in production.

claude-api83python23production111reliability18retry7timeout10

When you first integrate the Claude API, it feels complete the moment anthropic.Anthropic().messages.create(...) returns a response. Once you put that call inside a real service, though, a much larger world opens up: sudden 429s, flaky connections, scattered 503s, hanging requests. How you handle these determines the quality of your production service.

This article is the design I settled on after running the Claude API inside a Python backend for a while. I am skipping the basics and going straight to the reliability layer most guides omit.

Retry design breaks first in production, timeout design breaks second

When services that use the Claude API have incidents, the cause is almost always one of three things:

  • Sloppy retries that cannot absorb transient errors
  • Coarse timeouts that let one-way hangs drag the whole service down
  • Misreading rate-limit responses, which turns a spike into a cascade

Once you have these three in focus, wrapping the SDK is not enough. The SDK is built to handle the happy path; you have to build the unhappy path yourself.

Decide what retries target before writing any retry code

The single most important decision in retry design is defining what you retry. Errors from the Claude API fall into these buckets:

  • Retry: 429 (rate limit), 503 and 529 (temporary overload), connection errors, read timeouts
  • Do not retry: most 4xx (malformed request), 401 (auth), 403 (permission), context overflow
  • Case by case: 500 (server) and 504 (gateway timeout) — decide by whether the call is idempotent

Ignoring this split and writing "retry three times on any error" creates the classic incident where a brief auth outage triples your error load simply from the retry attempts.

The code below also retries 504. I judged that safe because messages.create does not change anything on your side, so a resend cannot cause a double side effect. If you put tool execution or other external writes in the same function, I'd take 504 out of the retry set.

My baseline looks like this:

import random
import time
from anthropic import Anthropic, APIStatusError, APIConnectionError, APITimeoutError
 
client = Anthropic(timeout=60.0, max_retries=0)
 
RETRYABLE_STATUS = {429, 503, 504, 529}
MAX_ATTEMPTS = 5
BASE_DELAY = 1.0
MAX_DELAY = 30.0
 
def call_with_retry(payload: dict):
    attempt = 0
    while True:
        attempt += 1
        try:
            return client.messages.create(**payload)
 
        except APIStatusError as e:
            if e.status_code not in RETRYABLE_STATUS or attempt >= MAX_ATTEMPTS:
                raise
            sleep_for = _retry_delay(e, attempt)
 
        except (APIConnectionError, APITimeoutError):
            if attempt >= MAX_ATTEMPTS:
                raise
            sleep_for = _backoff(attempt)
 
        time.sleep(sleep_for)
 
def _backoff(attempt: int) -> float:
    cap = min(BASE_DELAY * 2 ** (attempt - 1), MAX_DELAY)
    return random.uniform(0, cap)
 
def _retry_delay(err: APIStatusError, attempt: int) -> float:
    retry_after = getattr(err.response, "headers", {}).get("retry-after")
    if retry_after:
        try:
            return min(float(retry_after), MAX_DELAY)
        except ValueError:
            pass
    return _backoff(attempt)

I intentionally set the SDK's max_retries to 0 and handle retries myself for two reasons. First, I want to read and log the Retry-After header. Second, I want every sleep to happen in one place so I can reason about timing during incidents. When retries are hidden inside the SDK you lose visibility into where your seconds are going. The SDK's default is 2 retries (I checked on anthropic 1.15.0), so if you leave it on and stack your own 5-attempt loop on top, the worst case is 15 calls for a single request.

When you see a 429, find out who emitted it and why

A 429 is Anthropic telling you to slow down, but the "who" and "why" behind it vary. Account-wide token rate, per-model RPM, concurrent connection caps on a single key — these are different signals surfaced the same way.

What saves you later is logging status errors with structure, including the Retry-After header and the request ID:

except APIStatusError as e:
    log.warning({
        "event": "anthropic_status_error",
        "status": e.status_code,
        "message": getattr(e, "message", None),
        "request_id": getattr(e, "request_id", None),
        "retry_after": getattr(e.response, "headers", {}).get("retry-after"),
        "attempt": attempt,
    })

The request_id is decisive if you ever contact Anthropic support. Teams that forget to log it lose the ability to correlate server-side traces with their incidents.

In one incident I had to debug, sporadic late-night 429s were appearing. Because I had request IDs and timestamps, I could see that the spikes were confined to one specific model. My application had not changed, so I could conclude quickly that the issue was not on our side.

Separate connect timeout from read timeout

Python's httpx lets you configure connect and read timeouts independently. For a service like the Claude API, where request time varies wildly with prompt size and model, this separation is not optional.

Connect timeout is about "can we establish a TCP connection at all," which should fail fast. I use 5 seconds. Read timeout covers "Claude is thinking," which should be generous. Depending on model and max_tokens, I set 60 to 180 seconds.

import httpx
from anthropic import Anthropic
 
http_client = httpx.Client(
    timeout=httpx.Timeout(
        connect=5.0,
        read=120.0,
        write=10.0,
        pool=5.0,
    ),
    limits=httpx.Limits(max_connections=50, max_keepalive_connections=20),
)
 
client = Anthropic(http_client=http_client, max_retries=0)

Another subtle point: passing a scalar timeout=60.0 applies to every phase including connect. A 60-second connect timeout is too long — it masks DNS and TCP issues. Always split it.

Streaming adds another wrinkle. With streaming you have two concepts: time to first byte and time to completion. Read timeout is effectively the idle interval between chunks, so 60 seconds is usually fine. For non-streaming long outputs, 120 to 180 is safer.

Design retry and timeout together

This is where people trip. Setting read timeout to 120 and retries to 5 means the worst case is over ten minutes. Most upstream requests have SLAs of seconds to tens of seconds. Exceeding that inside your API layer creates incidents.

My rules:

  • Cap total time at 70% of the upstream SLA
  • Work out per-attempt timeout × max retries so they stay under the total cap
  • Limit user-facing synchronous calls to max_attempts=3 — 5+ belongs to async queues

In code:

def call_with_budget(client, payload: dict, total_budget_s: float):
    start = time.monotonic()
    attempt = 0
    while True:
        attempt += 1
        remaining = total_budget_s - (time.monotonic() - start)
        if remaining <= 0:
            raise TimeoutError("budget exhausted")
        try:
            return client.with_options(timeout=min(remaining, 30.0)).messages.create(**payload)
        except APIStatusError as e:
            if e.status_code not in RETRYABLE_STATUS or attempt >= MAX_ATTEMPTS:
                raise
            wait = _retry_delay(e, attempt)
        except (APIConnectionError, APITimeoutError):
            if attempt >= MAX_ATTEMPTS:
                raise
            wait = _backoff(attempt)
        # If sleeping would leave no budget, give up now instead of sleeping
        if time.monotonic() - start + wait >= total_budget_s:
            raise TimeoutError("budget would be exhausted while waiting")
        time.sleep(wait)

with_options(timeout=...) sets each attempt's timeout from what is left, so you never blow the upstream SLA.

The first version of this article had a hole here. Its except clause only caught connection errors, so a 429 was never retried at all — it flew straight to the caller. It also happily slept through a Retry-After and then found the budget gone.

I only noticed when I ran it against a stub client that kept returning 429 (anthropic 1.15.0):

StatusRetry-AfterBudgetResult
42920 s10 sOne call, then an immediate TimeoutError without sleeping (waiting could not fit the budget)
4291 s10 sFive calls over about 4 s, then APIStatusError
4001 s10 sOne call, immediate APIStatusError (not retryable)

The first row is the behavior I care about most. Sleeping when you already know you cannot make it is the most annoying failure a caller can receive.

Concurrency — do not burn your rate limit silently

Batch jobs and multi-tenant services that fan out calls always hit 429 if concurrency is uncapped. asyncio.gather on 50 requests is a production anti-pattern.

I use asyncio.Semaphore for an explicit parallelism ceiling and layer a token bucket on top for per-second rate control.

import asyncio
from anthropic import AsyncAnthropic
 
client = AsyncAnthropic()
sem = asyncio.Semaphore(10)
 
async def call_async(payload: dict):
    async with sem:
        return await client.messages.create(**payload)

A semaphore alone will not cap requests per second. When bursts are expected, add a bucket:

class TokenBucket:
    def __init__(self, rate: float, capacity: int):
        self.rate = rate
        self.capacity = capacity
        self.tokens = capacity
        self.last = time.monotonic()
        self.lock = asyncio.Lock()
 
    async def acquire(self):
        async with self.lock:
            now = time.monotonic()
            elapsed = now - self.last
            self.tokens = min(self.capacity, self.tokens + elapsed * self.rate)
            self.last = now
            if self.tokens < 1:
                wait = (1 - self.tokens) / self.rate
                await asyncio.sleep(wait)
                self.tokens = 0
            else:
                self.tokens -= 1

I target 70 to 80 percent of the Anthropic RPM limit. That headroom protects against other clients sharing the same key and against small demand spikes.

Standardize your error logging

The single biggest operational difference between mature and immature services is the granularity of error logs. Do not re-raise SDK exceptions bare — attach metadata and write structured logs first.

def call_safely(payload: dict):
    try:
        return client.messages.create(**payload)
    except APIStatusError as e:
        log.error({
            "event": "claude_api_status_error",
            "status": e.status_code,
            "request_id": getattr(e, "request_id", None),
            "payload_model": payload.get("model"),
            "payload_max_tokens": payload.get("max_tokens"),
            "input_tokens_estimate": _estimate_tokens(payload),
        })
        raise
    except (APIConnectionError, APITimeoutError) as e:
        log.error({
            "event": "claude_api_network_error",
            "error_type": type(e).__name__,
        })
        raise

Request ID, model, max_tokens, and an estimated input token count — those four fields let you answer "which request was heavy" and "which model was unstable" months later. Without them, the data is gone.

Three production failure modes worth knowing

Mode 1: silent streaming disconnects. When the network drops mid-stream, some clients simply stop emitting events rather than raising an error. Add an idle guard:

My first attempt measured the time since the previous event whenever an event arrived:

# The version that did not work: the clock is only read when an event shows up
async for event in stream:
    if time.monotonic() - last > idle_limit_s:
        raise TimeoutError("stream idle exceeded")
    last = time.monotonic()

If the connection stalls and the next event never comes, that if is never reached, because the loop never returns from its wait. With a fake stream that stops mid-way, a 0.5 s limit did nothing until my outer 3 s timeout cut it off. The version below raised at 0.5 s:

async def stream_with_idle_guard(payload, idle_limit_s=20.0):
    async with client.messages.stream(**payload) as stream:
        it = stream.__aiter__()
        while True:
            try:
                event = await asyncio.wait_for(it.__anext__(), timeout=idle_limit_s)
            except StopAsyncIteration:
                return
            yield event

The point is to cap the wait for the next event, not to measure elapsed time after the fact. wait_for interrupts the wait itself, so a stalled stream raises reliably (asyncio.TimeoutError is an alias of TimeoutError from Python 3.11).

Mode 2: context overflow from user-generated input. Services that feed raw user content into Claude eventually start getting context overflow errors. Check input size before the API call and summarize or chunk when it gets close.

Mode 3: retry storms from ignored Retry-After. When a 429 comes with a Retry-After value and your code keeps its own exponential backoff without reading the header, you fight the server's guidance. Respect Retry-After first, backoff second.

Closing thought

Running the Claude API in production is about quiet defenses, not clever optimizations. Split connect and read timeouts. Honor Retry-After. Log request IDs. Work back from an overall time budget. These simple habits move the reliability needle more than any tuning trick.

If you only add one thing today, add request-ID logging. The next time something goes wrong, you will be glad you did.

Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

If you found this article helpful, a small tip ($1.50) would mean a lot to us. Your support helps keep this site ad-free and covers server and hosting costs.

Related Articles

⬡ API & SDK2026-06-12
Handing Monthly Revenue CSVs to the Claude API Code Execution Tool — Files API Wiring, Container Reuse, and Billing Traps
How I moved monthly revenue-CSV reconciliation into the Claude API Code Execution tool — a pre-upload sanity check, Files API integration, container reuse, the 5-minute billing minimum, and the file recovery that pause_turn quietly drops.
⬡ API & SDK2026-05-26
Stabilizing Claude API Structured Responses in Production — Notes on tool_use, JSON Schema, and Layered Validation
Getting Claude to return JSON takes a few lines. Keeping that JSON usable in production is a different problem. Here is the layered design I landed on after running a wallpaper classification pipeline through Claude API, built around tool_use, JSON Schema, and domain validation.
⬡ API & SDK2026-05-22
Why tool_result could not be submitted Keeps Coming Back, and How to Build a Recovery Handler That Actually Holds
Run a Claude agent long enough and one day it starts: 'tool_result could not be submitted', back to back, and retries change nothing. The error message hides four completely different root causes. Here is what I learned debugging it on the always-on agent jobs I run as an indie developer, with the TypeScript recovery handler I now ship in production.