Skip to content
mentoor

Topic 4 of 5 · 5 min read

Setting timeouts for long model generations

Goal: Set connect, read and total timeouts that fit the expected generation length and fail fast when the provider stalls.

Set timeouts that match how long a generation can legitimately take, separate connecting from reading, and use streaming for long outputs so a stall is detected quickly. An LLM call is not like a database query. A long answer can take a minute or more, so a 5 second timeout kills good requests and no timeout at all lets a stalled one hang.

Check yourself · 1 of 3

Why is a 5 second total timeout a poor choice for a call that generates a long report?

Choose one answer

Why it matters

Generation time grows with output length. A reply of 100 tokens comes back fast. A reply of several thousand tokens takes much longer. If a provider stalls and your client waits forever, a worker is blocked and users wait with it.

Three timeouts

Timeout What it bounds Typical choice
Connect Opening the connection Short, a few seconds
Read Silence between bytes from the server Short for streams, long for non-streamed calls
Total The whole call from start to last byte Sized to the longest reply you allow

For a non-streamed call the server may send nothing until the whole answer is ready, so the read timeout must cover the full generation. For a streamed call tokens arrive as they are made, so the read timeout can be short, such as 30 seconds of silence, and a stall shows up quickly.

How to size them

  1. Find your longest allowed output: say max_tokens of 4,000.
  2. Measure real speed for your model on a sample run. If it produces about 50 tokens a second, 4,000 tokens take about 80 seconds.
  3. Set the total timeout to a comfortable multiple, such as 2 times, so about 160 seconds.
  4. Set max_tokens on every request. It is the upper bound on both cost and time.

SDK defaults

SDKs ship defaults, and some refuse very long non-streamed requests and ask you to stream them. The Claude SDKs expect streaming for large max_tokens. Read the SDK's docs for its current default timeout, and set your own explicitly.

import anthropic, httpx

client = anthropic.Anthropic(
    max_retries=0,
    timeout=httpx.Timeout(160.0, connect=5.0, read=30.0),
)

with client.messages.stream(model=MODEL, max_tokens=4000, messages=msgs) as s:
    text = s.get_final_message()

Before and after

Before: default settings, a request with max_tokens=8000 and no stream, and a worker stuck for several minutes when the provider hangs. After: streaming with a 30 second read timeout, so a stall is caught in under a minute and handed to the retry logic.

What goes wrong

  • One short timeout for everything. It cuts off valid long answers and then you pay for the work twice on retry.
  • No timeout. One stalled call blocks a worker.
  • Timeouts shorter than your gateway's. A proxy that cuts connections at 60 seconds kills long calls however you set the client.
  • Retrying a timeout blindly. A call that timed out may still have been billed, so cap those retries.

Examples

Sizing a total timeout

A report generator allows up to 4,000 output tokens. In a test run the model produced about 50 tokens per second, so a full reply takes about 80 seconds. The team sets a total timeout of 160 seconds and a read timeout of 30 seconds on a streamed call.

The proxy that cut at 60 seconds

A long call works in tests and fails in production after exactly 60 seconds. The load balancer in front of the service closes idle connections at 60 seconds. Streaming sends bytes steadily, so the connection stays active and the problem goes away.

Key points

  • Generation time grows with output length, so a database-style short timeout will cut off good answers.
  • Set connect, read and total timeouts separately, and always set max_tokens to bound the work.
  • With streaming, tokens arrive continuously, so a short read timeout detects a stall within seconds.
  • Estimate the total timeout from tokens per second and the longest allowed output, then add a margin.
  • Check proxies and gateways in front of your client, because their own timeouts apply too.

Common mistakes

  • Using one global timeout for short classification calls and long report generation.
  • Leaving max_tokens unset, so the longest possible reply sets the wait.
  • Sending a very long output as a single non-streamed call.
  • Forgetting that a proxy or load balancer has its own timeout.

Check yourself · 2 of 3

Which setting gives the clearest upper bound on how long a single generation can take?

Choose one answer

Check yourself · 3 of 3

How does streaming help detect a stalled provider?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic