OpenAI SDK Timeout and Retry Settings: Defaults and Production Tips
In current versions, the official OpenAI SDK has a default timeout of 600 seconds (10 minutes) and retries automatically 2 times. The official Anthropic SDK uses the same defaults. In the Python version, the 600 seconds is an httpx.Timeout object in which the connect phase is set separately to 5 seconds. So for the question "is the OpenAI SDK timeout 60 seconds or 600 seconds?", the answer for official SDKs from 1.x onward is 600 seconds. A 60-second figure you see online most likely comes from a third-party wrapper, another library's default, or outdated docs.
Defaults are fine for "it works," but not necessarily for production. If an interactive request waits 10 minutes before failing, the user is long gone. If a gateway sits in front of the request, it may cut the connection at 100 seconds while the SDK keeps waiting. Below, each SDK parameter is laid out, followed by configurations for several scenarios.
What are the default values in each SDK?
The table below shows the default behavior of the official OpenAI and Anthropic SDKs (the Python openai / anthropic packages and the TypeScript openai / @anthropic-ai/sdk packages) across recent major versions. Defaults can change between versions, so treat the source of the version pinned in your project as authoritative. See the section near the end for how to confirm it.
| Item | OpenAI Python | OpenAI TypeScript | Anthropic Python | Anthropic TypeScript |
|---|---|---|---|---|
| Total timeout | 600 seconds | 600000 ms | 600 seconds | 600000 ms |
| Connect timeout | 5 seconds (set separately) | Not distinguished | 5 seconds (set separately) | Not distinguished |
| Default retries | 2 | 2 | 2 | 2 |
| Parameter names | timeout, max_retries | timeout, maxRetries | timeout, max_retries | timeout, maxRetries |
| Raised on timeout | APITimeoutError | APIConnectionTimeoutError | APITimeoutError | APIConnectionTimeoutError |
"2 retries" means up to 2 more attempts after the first request fails, for at most 3 requests in total. A timeout is itself a retryable error, so with the default configuration a stuck request can wait close to 3 × 600 seconds plus backoff before the exception reaches your code. That is usually why it looks like "the SDK hung."
Which errors are retried automatically?
The retry logic is essentially the same in both SDKs:
- Connection errors (DNS failure, connection refused, connection reset) and timeouts: retried.
- HTTP 408 (request timeout), 409 (conflict), 429 (rate limit): retried.
- HTTP 500 and above (including Anthropic's 529 overloaded): retried.
- Client errors such as 400, 401, 403, 404, 422: not retried, raised immediately.
- If the server returns an
x-should-retry: true/falseresponse header, that header wins.
For backoff, the SDK first reads the retry-after-ms or retry-after response header. If neither is present, it uses exponential backoff with jitter, starting at about 0.5 seconds and capped at about 8 seconds. For what each status code means, see the LLM API error code cheat sheet. For Claude's 529 specifically, see What the Claude 529 overloaded_error means.
One kind of 429 should not be retried. OpenAI's insufficient_quota means your quota is used up, and retrying will only return another 429. The SDK does not distinguish between the two kinds of 429, so you need to check the error code in your business logic and abort. For how to tell them apart, see OpenAI API 429: insufficient_quota vs rate_limit_exceeded.
How do I set per-phase timeouts in Python?
The Python SDK is built on httpx. The timeout parameter accepts either a float or an httpx.Timeout object. There are two differences between them:
- If you pass a float such as
timeout=30, httpx sets all four phases (connect, read, write, pool) to 30 seconds, overriding the original 5-second connect timeout. - The httpx read timeout is "the longest gap between two chunks of received data," not the total duration of the request. A streaming response never triggers the read timeout as long as tokens keep arriving.
I recommend writing httpx.Timeout directly:
import httpx
from openai import OpenAI
from anthropic import Anthropic
timeout = httpx.Timeout(
connect=5.0, # TCP + TLS handshake
read=60.0, # longest gap between two chunks of received data
write=10.0, # sending the request body
pool=5.0, # waiting for an idle connection from the pool
)
openai_client = OpenAI(timeout=timeout, max_retries=2)
claude_client = Anthropic(timeout=timeout, max_retries=2)
To adjust a single request temporarily, use with_options rather than creating a new client (a new client discards the existing connection pool):
resp = openai_client.with_options(timeout=15.0, max_retries=0).chat.completions.create(
model="<your model name>",
messages=[{"role": "user", "content": "ping"}],
)
The Anthropic Python SDK has one extra check. For a non-streaming request whose estimated duration, based on max_tokens, could exceed 10 minutes, the SDK raises an error and tells you to use streaming. When you hit this error, do not raise the timeout. Follow the message and switch to stream=True or messages.stream().
How do I set this in TypeScript?
In the TypeScript SDK, timeout is a number of milliseconds and is not split by phase:
import OpenAI from "openai";
import Anthropic from "@anthropic-ai/sdk";
const openai = new OpenAI({ timeout: 60_000, maxRetries: 2 });
const claude = new Anthropic({ timeout: 60_000, maxRetries: 2 });
// Override for a single request
await openai.chat.completions.create(
{ model: "<your model name>", messages: [{ role: "user", content: "ping" }] },
{ timeout: 15_000, maxRetries: 0 },
);
The TS SDK implements timeouts with AbortController. How far it reaches (only until the response headers return, or through reading the response body) has been adjusted across versions. For streaming, if you need "no more than N seconds overall," pass your own signal as well and use AbortSignal.timeout(N) as a backstop.
What is a reasonable connect timeout?
The connect timeout covers only DNS, TCP and TLS handshakes. It has nothing to do with how fast the model generates and depends only on the network path. As a reference, cdnping's edge probes hit the official API endpoints from Cloudflare edge nodes every 5 minutes. Collection began only on 2026-10-17. The table below is a snapshot from the first day (probe location DEN). The sample is small, so it is only good for orders of magnitude:
| Endpoint | p50 | p95 |
|---|---|---|
| api.openai.com | 96 ms | 194 ms |
| api.anthropic.com | 73 ms | 84 ms |
These are numbers from a data center to the API endpoints, under good network conditions, and even p95 stayed under 200 milliseconds. Compared with that, the SDK's default 5-second connect timeout leaves a large margin. Your own servers or office network will be slower, and cross-border links can have much higher latency and jitter. You can check the "first connection" item in the cdnping online speed test, which includes DNS and the TLS handshake. Live data is on the OpenAI API status and Claude API status pages, and the probing approach is described in Methodology.
A rule of thumb (not official guidance): take your own measured first-connection p95, multiply by 3 to 5, and use that as the connect timeout, with a floor of 2 seconds. If you often time out in the connect phase, check the network first, since raising the timeout won't fix it. AI API latency breakdown shows how to look at connect time, round trip and time to first token separately.
How should I configure each scenario?
| Scenario | connect | read (Python) / total timeout (TS) | max_retries | Notes |
|---|---|---|---|---|
| Chat UI, interactive requests (streaming) | 5 s | read 30–60 s | 1–2 | The wait before the first token also counts as a read gap, so loosen this for reasoning models that think for a long time |
| Short non-streaming calls (classification, extraction) | 5 s | total 30–60 s | 2 | Output is short, so anything over a minute is probably abnormal |
| Long non-streaming output (long-form generation, large batches) | 5 s | Estimate from expected output length, with 1.5x margin | 2–3 | For very long output, prefer streaming |
| Offline batch jobs, queue tasks | 5–10 s | Generous | 3–5 | Not urgent, so more retries are fine, but pair them with a retry budget |
| Health checks, liveness probes | 3 s | total 10 s | 0 | Probes should not retry, or they mask real failures |
On retry counts: the SDK's built-in retries are suited to occasional network blips and 429s. If you also have your own retries in an outer layer (for example, a task queue that re-enqueues failures), the two layers multiply. The SDK's 2 times plus 3 times in the outer layer can send up to 12 requests in the worst case. The usual approach is to keep just one layer: either set the SDK to max_retries=0 and let the outer layer manage everything, or use only the SDK's. For how to write backoff, jitter and retry budgets, see How to write LLM API retries.
Why align timeouts with the gateway when using a custom base_url?
When you point base_url at your own reverse proxy, a company gateway or a third-party aggregation gateway, the request path gains another layer, and each layer has its own timeout:
- For an origin behind the Cloudflare proxy, if no response headers come back within 100 seconds, the client gets a 524. See AI API returns 524 Gateway Timeout.
- Nginx's
proxy_read_timeoutdefaults to 60 seconds, and SSE streams may also get buffered. See LLM streaming output cuts off midway. - Load balancers and API gateways usually have idle timeouts too.
Alignment principles:
- Set the client SDK timeout slightly below the shortest layer on the path. That way your own code detects the timeout first, and the log shows
APITimeoutErrorrather than an ambiguous 502 or 524. - If a long non-streaming request exceeds an intermediate layer's limit, raising the client timeout does not help. Switch to streaming so the intermediate layer keeps receiving data.
- A 502, 503 or 504 from the gateway is treated by the SDK as a 5xx and retried automatically. If the gateway itself also retries upstream, the retry counts multiply again, so turn retries off in one of the layers.
An example: AGI Plan is a commercial partner of cdnping. AGI Plan is compatible with both the OpenAI and Anthropic API formats, and you only need to change base_url to connect. In the same first-day snapshot, the p50 from the cdnping edge (DEN) to agiplan.dev was 883 ms, higher than to the official endpoints. An integration with an extra hop like this needs connect and read estimated again from the latency you actually measure through the gateway. Don't copy the numbers for a direct connection to the official endpoints.
How do I confirm the defaults for my version from the source?
SDK defaults are whatever the source says. Documentation mirrors and old PyPI pages may reflect outdated versions. To confirm:
# Python: check the installed version first, then find the constant definitions
pip show openai anthropic | grep -E "Name|Version"
python -c "import openai, inspect; print(inspect.getsourcefile(openai))"
grep -rn "DEFAULT_TIMEOUT\|DEFAULT_MAX_RETRIES\|INITIAL_RETRY_DELAY\|MAX_RETRY_DELAY" \
$(python -c "import openai, os; print(os.path.dirname(openai.__file__))")/_constants.py
# TypeScript: search node_modules for the defaults
npm ls openai @anthropic-ai/sdk
grep -rn "DEFAULT_MAX_RETRIES\|timeout:" node_modules/openai/client.* node_modules/openai/core.* 2>/dev/null | head
The anthropic package's constants are also in _constants.py inside the package of the same name, so just swap the package name. To confirm the retry decision logic, search for _should_retry (Python) or shouldRetry (TypeScript), which shows directly which status codes are retried.
Pre-launch checklist
- Set
timeoutandmax_retriesexplicitly instead of relying on defaults, so an SDK upgrade can't silently change them. - In Python, use
httpx.Timeoutto set phases separately, so a single float doesn't inflate connect too. - The worst-case wait for interactive requests (timeout × (retries + 1) + backoff) is within an acceptable range.
- There is only one retry layer, or the total after multiplying two layers is under control.
- Errors like
insufficient_quota, 401 and 400 abort immediately in business logic, with no retry. - With a custom
base_url, the client timeout is shorter than the shortest intermediate layer's timeout. - Long output uses streaming. When the Anthropic SDK tells you to switch to streaming, do so instead of just raising the timeout.
- Logs record the status code and duration of every retry, so you can later tell whether it was the network, rate limiting or an upstream failure.
When timeouts show up in bulk, first check the OpenAI API status and Claude API status pages to tell an upstream failure from a problem on your own path.