AI Agent Timeout Handling 2026
One third-party endpoint hangs and the agent waits with it while the user closes the tab. Timeouts are not a single number: each tool needs a budget, a fallback and a retry limit.
📜 Table of Contents
📖 Keep reading
Timeout Handling for AI Agents: Budget Every Tool
Agents hang in a simple way: a tool call never returns. The user sees a frozen screen and leaves.
1. Timeouts are plural
| Type | Meaning | Typical failure |
|---|---|---|
| Connect | TCP/TLS setup | Slow DNS, unreachable host |
| Read | Waiting for bytes | Slow server, stalled stream |
| Total | Hard cap for the call | Chunked transfers never trip read timeouts |
| End-to-end | Budget for the whole request | Each call passes, the sum does not |
Read timeouts alone are not enough. A server trickling one byte every 20 seconds never trips them.
2. Budget by tool type
| Tool | Suggested timeout |
|---|---|
| Short model answer | 20-30s |
| Long generation | 60-120s, streamed |
| Search / fetch | 8-10s |
| Internal API | 3-5s |
| Database | 2-5s |
| File I/O | 2-3s |
Start from the smaller of "what a user tolerates" and "your p99", then calibrate on real data.
3. Code
async def call_tool(fn, *, timeout: float, retries: int = 1):
for attempt in range(retries + 1):
try:
return await asyncio.wait_for(fn(), timeout=timeout)
except (asyncio.TimeoutError, httpx.TimeoutException):
if attempt == retries:
return {"ok": False, "error": "timeout"}
await asyncio.sleep(0.5 * (2 ** attempt))
client = httpx.AsyncClient(timeout=httpx.Timeout(connect=3.0, read=8.0, write=3.0, pool=2.0))
wait_for enforces the total, httpx enforces connect and read. You need both.
4. Four ways to recover
- Retry once, only for idempotent operations and connection-class errors. A model retry doubles token cost.
- Fail over to a backup source or a faster model, and keep that path exercised.
- Skip non-critical tools and answer with what you have, labeled as incomplete.
- Return partial results with a clear statement of what failed.
5. Retry discipline
Cap retries at one or two, use exponential backoff with jitter, retry only timeouts, connection errors, 429 and 502/503, and wrap flaky dependencies in a circuit breaker.
6. Observe it
Record duration, timeout flag and fallback flag per call. Watch p95 and timeout rate per tool; a rising rate means upstream degradation, your own saturation, or longer generations.
FAQ
Will short timeouts kill healthy requests? Yes. Base them on p99 plus 30% headroom and pair with fallbacks.
Do I still pay for a timed-out call? Usually yes for generated tokens, so a retry pays twice. Stream long outputs to keep partial content.
User already left but the task is still running? Enforce an end-to-end budget and return the best current result.
Cancellation leaks resources? It can. Propagate cancellation, close connections, and manage resources with async with.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.