Bottom line: Most OpenAI-compatible SDKs ship with a 30-second default read timeout and no retry on ReadTimeout. If your generations die at a suspiciously round number, the model is not slow — your client gave up. Raise timeout and add max_retries, and 90% of “the model is flaky” complaints disappear.

The symptom

You log a request, it takes 31 seconds, and you get APITimeoutError or ReadTimeout. Then a retry succeeds in 4 seconds. You conclude the provider is unstable. It usually isn’t: the p99 latency for a 2,000-token completion at 40 tokens/second is 50 seconds, so a 30-second ceiling truncates the tail by construction.

The maths is boring and decisive. At 40 tok/s:

Output tokens Time at 40 tok/s Survives 30s timeout?
500 12.5 s yes
1,000 25 s barely
1,500 37.5 s no
2,000 50 s no

If you ask for long answers, your timeout is the bug.

Check what your client actually does

Python openai exposes timeout and max_retries on the client. Verify with a trivial, deterministic request rather than reading docs:

1
2
3
4
5
6
7
8
9
10
11
import time
from openai import OpenAI

c = OpenAI(timeout=120.0, max_retries=2)
t0 = time.monotonic()
r = c.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "count to 400"}],
max_tokens=1200,
)
print(f"{time.monotonic()-t0:.1f}s", r.usage.completion_tokens)

Run that three times and compare the wall time against your configured timeout. If any run lands within 10% of the limit, you are clipping.

Timeouts stack

There are usually three, and the smallest wins:

  1. Client read timeout (timeout=)
  2. Your HTTP reverse proxy (proxy_read_timeout, default 60s in nginx)
  3. The upstream gateway (often 300s)

A 600-second client timeout behind a 60-second nginx is a 60-second timeout with extra steps. Grep for it:

1
grep -rn "proxy_read_timeout\|proxy_send_timeout" /etc/nginx/

Streaming changes the failure mode

With stream=True, the read timeout applies per chunk, not to the whole response. A slow-but-steady generation never trips it; a 12-second stall between chunks does. That is why streaming “fixes” long generations and why it also surfaces stalls you never saw before. Set an explicit per-chunk timeout and treat a gap as a retryable condition rather than a hard failure.

The retry that makes it worse

Retrying a non-idempotent request after a timeout can double your bill. If the original request eventually completed server-side, you pay twice. For generations, this is usually acceptable; for tool calls with side effects it is not. Tag each request with an idempotency key if the provider supports it, or accept the cost and log usage deltas so double billing is visible instead of mysterious.

Practical defaults

Setting Value Why
timeout 120 s covers 2,000 tokens at 20 tok/s
max_retries 2 one transient stall, one network blip
nginx proxy_read_timeout 300 s longer than the client
per-chunk timeout (stream) 30 s catches genuine stalls

Set these once in a shared client factory and stop re-litigating it in every module.

What to check when it still fails

If requests still die early after these changes, the timeout is not the cause. Check, in order: proxy connect timeouts (a dead upstream looks like a slow model), DNS resolution stalls on the first call only, and request body size limits returning 413 as a generic error. Instrument time_to_first_byte separately from total_time — they fail for completely different reasons and conflating them costs hours.

Designing the measurement

You need three numbers to size the timeout honestly: p50 time-to-last-token, p99, and the output length distribution. Take them from production logs, not from a synthetic prompt. If logs do not carry token counts, add them first — an unmeasured timeout is a guess.

1
2
3
4
5
6
SELECT model,
percentile_cont(0.5) WITHIN GROUP (ORDER BY ms) AS p50,
percentile_cont(0.99) WITHIN GROUP (ORDER BY ms) AS p99,
percentile_cont(0.5) WITHIN GROUP (ORDER BY completion_tokens) AS med_tokens
FROM llm_calls WHERE created_at > now() - interval '7 days'
GROUP BY model;

Set the timeout at roughly 1.5× p99, not at p99 — the distribution has a tail beyond the sample. Then re-measure in a week: if p99 moved, the timeout follows.

Boundary conditions

A long timeout is not free. Each hung request holds a worker and a connection until the ceiling. With 20 workers and a 600 s timeout, a provider stall parks the entire pool for ten minutes. The pairing that works is a long timeout with a concurrency cap and a circuit breaker: after five consecutive timeouts, stop issuing for 30 s and let the provider recover instead of stacking dead requests.

Also bound max_tokens. A runaway generation hits neither the timeout nor the token ceiling and streams until one of them exists.

When it is not the timeout

Triage in this order, each ruling out one layer:

  1. time_to_first_byte alone is long → connection or queueing, not generation
  2. TTFB normal, throughput low → provider-side throttling; check 429 rate
  3. Both normal, occasional total failure → DNS or proxy; check resolution time per request
  4. Fails only on the first request of a process → TLS handshake cost, not timeout

Only case 2 responds to a timeout change. The rest need a different fix and a longer ceiling just delays the diagnosis.

FAQ

Why do my LLM calls fail at exactly 30 seconds?

Most OpenAI-compatible SDKs ship with a 30-second default read timeout and no retry on ReadTimeout. When a generation exceeds that ceiling, the client gives up and raises APITimeoutError or ReadTimeout — the model itself is not slow. Raise timeout and add max_retries to fix it.

How do I check whether my client is actually timing out early?

Run a trivial deterministic request three times and compare wall time against your configured timeout. If any run lands within 10% of the limit, you are clipping. Python’s openai client exposes timeout and max_retries directly, so verify behavior rather than reading docs.

Why does streaming fix long generations?

With stream=True, the read timeout applies per chunk rather than to the whole response. A slow-but-steady generation never trips it, while a 12-second stall between chunks does. That is why streaming “fixes” long generations and also surfaces stalls you never saw before.

What timeout and retry values should I set?

Use a 120-second timeout, max_retries of 2, nginx proxy_read_timeout of 300 seconds, and a 30-second per-chunk timeout for streaming. Set these once in a shared client factory. Note that timeouts stack — the smallest of client, proxy, and gateway wins.

Can retrying after a timeout cost me more?

Yes. Retrying a non-idempotent request after a timeout can double your bill if the original request eventually completed server-side. For generations this is usually acceptable; for tool calls with side effects it is not. Tag requests with an idempotency key where supported, or log usage deltas so double billing is visible.