Topic 2 of 5 · 5 min read
Timeouts for streamed and long generations
Goal: Set timeouts for a streamed call and for an agent step from observed latency percentiles.
A single timeout number is wrong for an LLM call; set one per phase and derive each from measured latency. A generation can take 40 seconds and be perfectly healthy, or hang for 40 seconds after the first token and be dead.
Check yourself · 1 of 3
What does an idle-stream timeout catch that a total timeout does not catch early?
Why it matters
Without timeouts a stuck call holds a connection, a worker and a user's patience indefinitely. With one timeout that is too short you cancel good long answers and pay for the tokens already generated. With one that is too long, retries and fallbacks never start in time.
The four timeouts
- Connect timeout, a few seconds. Opening a TCP and TLS connection is fast or broken.
- First-token timeout. Time until the first streamed chunk. It grows with prompt size and with reasoning models, which think before they emit anything. Set it from the p95 or p99 of your own traces for that model, plus margin.
- Idle-stream timeout. Maximum gap between chunks once streaming has started. Ten seconds of silence mid-answer usually means a stalled connection. This one is what an overall request timeout cannot express.
- Total timeout. A hard ceiling for the whole call, set from what the user will actually wait for, not from what the model might need.
Agents and deadlines
An agent step is many calls. Give each step its own limit, give the whole run a budget, and pass a deadline (an absolute time) down the chain rather than a fresh timeout at every layer. If the user's request has 30 seconds left, the retry logic and the tool call should see 30 seconds, not their own default 60.
const deadline = Date.now() + 30_000;
async function callModel(prompt: string) {
const left = deadline - Date.now();
if (left < 2_000) throw new Error("deadline_exceeded");
const ctrl = new AbortController();
const total = setTimeout(() => ctrl.abort("total"), left);
let idle = setTimeout(() => ctrl.abort("idle"), 10_000);
try {
for await (const chunk of stream(prompt, ctrl.signal)) {
clearTimeout(idle);
idle = setTimeout(() => ctrl.abort("idle"), 10_000);
yield chunk;
}
} finally { clearTimeout(total); clearTimeout(idle); }
}
Before and after
Weak: timeout: 60 on every call. A stalled stream wastes the full minute, then the retry gets another minute.
Fixed: 3 second connect, first-token limit from the p99 of recent traces, 10 second idle limit, and a total capped by the request deadline. A stalled stream is cut in 10 seconds and the fallback starts with time left.
What goes wrong
- Cancelling without cleanup. The client aborts but the server keeps generating, or your code never releases the worker.
- Using chat timeouts for reasoning models, which can be silent for a long time before the first token.
- Retrying past the deadline, so the user has already left.
- Treating a partial stream as a failure when you could show what arrived.
Examples
Percentile-based limits
Traces for one model show first-token latency p50 1.2 s, p99 6 s, and a longest gap between chunks of 3 s. Reasonable settings: connect 3 s, first-token 10 s, idle 8 s, total 45 s. Review them monthly, because prompt sizes and models change.
The agent that ate the budget
A research agent allows 60 s per step and 12 steps. A slow tool makes one run take 11 minutes. The fix is a run budget of 90 s and a deadline passed to every step, so late steps are skipped and the agent returns what it has.
Key points
- Set connect, first-token, idle-stream and total timeouts separately, because they catch different failures.
- Derive limits from your own latency percentiles per model, not from a round number.
- Reasoning models stay silent longer before the first token, so they need their own first-token budget.
- Pass an absolute deadline down the call chain so retries and tool calls never outlive the user's wait.
- Always release workers and abort the upstream request when a timeout fires.
Common mistakes
- One global timeout for connect, first token and total time.
- Copying a chat timeout onto a reasoning model and cancelling answers that were still thinking.
- Giving every layer its own default timeout instead of sharing one deadline.
- Aborting the client but not the upstream call, so tokens keep being generated and billed.
Check yourself · 2 of 3
Why does a reasoning model need a different first-token timeout from a chat model?
Check yourself · 3 of 3
A request has 12 seconds left before the user's page gives up. What should the retry layer use?
Practice items, not counted towards your mastery.
Start here
- Timeouts, retries and backoff with jitter (AWS Builders' Library) (opens in a new tab)ArticleFree
How to choose timeouts, cap retries and add jitter so retries do not pile onto a struggling service.
- Making retries safe with idempotent APIs (Amazon Builders' Library) (opens in a new tab)ArticleFree
The client-token pattern that makes a retried request produce one effect, not two.
- Circuit Breaker (Martin Fowler) (opens in a new tab)ArticleFree
The closed, open and half-open states in one short page.