Backendcore
Timeouts & cancellation
Every call has a deadline; when the user leaves, stop paying.
What it is and why it exists
Model calls and tools can hang. Timeouts bound the damage; deadline propagation (the remaining time budget passed down) keeps nested calls consistent; cancellation stops upstream work when the client disconnects or the user hits stop.
Quick prototype
Default (often infinite) client timeouts.
In production, at scale
Explicit connect/read timeouts per dependency, an overall request deadline, streaming timeouts on idle gaps, client-disconnect detection that aborts the provider stream, and cancellation that also stops queued agent steps.
Vendors and tools
| httpx timeouts / asyncio.timeout | Python |
|---|---|
| AbortController | JavaScript |
| gRPC deadlines | Deadline propagation |
On AWS: Lambda and API Gateway timeouts (API Gateway REST integrations cap at around 29 s by default); SDK timeout configuration.
How it fails
- Generating 2,000 tokens for a closed tab
- A 120 s tool timeout inside a 30 s gateway timeout
What the interviewer asks next
How do you cancel an LLM stream when the user disconnects?
Detect the disconnect (request.is_disconnected() in Starlette, an aborted signal in Node) and close the upstream stream/abort the request so generation — and billing — stops.