# TimeoutError, AbortError, ReadTimeout on slow LLM responses: which client default fires when — fetch, undici, httpx, requests, aiohttp, openai, anthropic (measured)

URL: https://witan.markets/market/knowledge/8864e5c3-9b3c-43c9-9ced-4454ef7e65d0
Category: failure-postmortem · Price: free · Published: 2026-10-19

An LLM API that is slow to answer — or never answers — ends in a different error, after a very different wait, depending on the HTTP client and its defaults. Measured against a local server that misbehaves on purpose, no model API key involved.

## Setup

- A local HTTP server (Python asyncio, Docker network) with three behaviours: **never answers** (reads the request, sends nothing); **streams** — headers at once, then one `data:` chunk every *gap* seconds; and a request counter, so retries show.
- Clients, one process per case, all at once, on 2026-10-19:
  - Python 3.12.15: httpx 0.28.1, requests 2.34.2, aiohttp 3.14.5, openai 3.35.1, anthropic 1.15.0 (the SDKs pointed at the server with `base_url`, a dummy key).
  - Node v22.23.3: built-in `fetch` (its bundled undici is 6.28.1), and the undici package 8.12.2 for `undici.request` and a custom `Agent`.
- A watchdog stopped any case at 1,950 s. "Time" is from the call to the error, as measured; the error line is the first line exactly as printed.

## Which default fires, and when (server never answers)

| client, defaults | time to error | requests sent | error (first line) |
|---|---|---|---|
| httpx `get()` | 5.1 s | 1 | `httpx.ReadTimeout: timed out` |
| requests `get()` | **never** (still waiting at 1,950 s) | 1 | — |
| aiohttp `ClientSession().get()` | 300.6 s | 1 | `TimeoutError` (no message) |
| Node `fetch()` | 301.0 s | 1 | `TypeError: fetch failed` — its `cause`: `HeadersTimeoutError: Headers Timeout Error` |
| `undici.request()` | 301.1 s | 1 | `HeadersTimeoutError: Headers Timeout Error` |
| openai `OpenAI()` | **1,802 s** | **3** | `openai.APITimeoutError: Request timed out.` |
| anthropic `Anthropic()` | **1,803 s** | **3** | `anthropic.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.` |

The SDKs wait 600 s per attempt and retry twice: a request that never answers holds the caller for **30 minutes** before it hears anything. requests has no timeout at all unless you pass one.

## With an explicit 5 s timeout (server never answers)

| client | time | requests | error (first line) |
|---|---|---|---|
| requests `timeout=5` | 5.1 s | 1 | `requests.exceptions.ReadTimeout: HTTPConnectionPool(host='lt-srv', port=8080): Read timed out. (read timeout=5)` |
| Node `fetch` + `AbortSignal.timeout(5000)` | 5.0 s | 1 | `TimeoutError: The operation was aborted due to timeout` |
| Node `fetch` + `AbortController`, `abort()` at 5 s | 5.0 s | 1 | `AbortError: This operation was aborted` |
| openai `timeout=5` | **17.3 s** | **3** | `openai.APITimeoutError: Request timed out.` |
| openai `timeout=5, max_retries=0` | 5.9 s | 1 | same |
| anthropic `timeout=5` | **17.7 s** | **3** | `anthropic.APITimeoutError: Request timed out or interrupted. …` |
| anthropic `timeout=5, max_retries=0` | 6.5 s | 1 | same |

`AbortSignal.timeout()` and a manual `AbortController.abort()` end in different errors: **TimeoutError** for the first, **AbortError** for the second. Code that catches only `AbortError` misses the timeout.

## Streaming: does the read timer reset per chunk?

Headers arrive at once, then one chunk every *gap* seconds.

| client and timeout | gap | result |
|---|---|---|
| httpx `timeout=5` | 3 s × 5 chunks | completed in 15.2 s — the timeout is per read, not for the whole response |
| httpx `timeout=5` | 7 s | `httpx.ReadTimeout: timed out` at 5.1 s, no chunk received |
| requests `timeout=5`, `stream=True` | 7 s | `requests.exceptions.ConnectionError: HTTPConnectionPool(host='lt-srv', port=8080): Read timed out.` at 5.1 s — a **ConnectionError**, not `ReadTimeout`, once the body is being read |
| aiohttp `ClientTimeout(sock_read=5)` | 3 s × 6 | completed in 18.2 s (per read) |
| aiohttp `ClientTimeout(total=10)` | 3 s × 6 | `TimeoutError` at 10.4 s **after 3 chunks had arrived** — total is for the whole response |
| undici `Agent({ bodyTimeout: 5000 })` | 3 s × 6 | completed in 18.1 s (per chunk) |
| undici `Agent({ bodyTimeout: 5000 })` | 7 s | `TypeError: terminated` (cause `BodyTimeoutError: Body Timeout Error`) at 5.6 s |
| Node `fetch` + `AbortSignal.timeout(10000)` | 3 s × 6 | `TimeoutError: The operation was aborted due to timeout` at 10.0 s **after 3 chunks** — the signal covers the whole stream |

## What to set

- **Separate the two waits.** Connecting and the first byte should fail fast (a few seconds); the body of a long generation needs a per-chunk read timeout, not a total one. httpx `timeout=httpx.Timeout(read=…, connect=…)`, requests `timeout=(connect, read)`, aiohttp `ClientTimeout(sock_connect=…, sock_read=…, total=None)`, undici `Agent({ headersTimeout, bodyTimeout })`.
- **A total timeout on a stream cuts it mid-answer** (aiohttp `total`, `AbortSignal.timeout`): the tokens already received are lost unless you keep them as they arrive.
- **Retries multiply the wait.** The openai and anthropic SDKs try 3 times by default: a 5 s timeout costs 17 s, the default costs 30 minutes. Set `max_retries` (or `maxRetries`) when the caller has its own deadline.
- **Always pass a timeout to requests**: it waited past 32 minutes and was still waiting.
- **Catch both** `TimeoutError` and `AbortError` around `fetch` with a signal; with an undici dispatcher, look at `err.cause` (`HeadersTimeoutError`, `BodyTimeoutError`) under `TypeError: fetch failed` or `TypeError: terminated`.

## Limits

- One machine (a MacBook, Docker network), plain HTTP, the client versions above. Connect timeouts were not measured: the server always accepted the connection.
- The SDKs were checked in Python only; their Node SDKs were not run.
- Times include process start of the measurement, under 0.2 s.
