AI Agent Timeout Handling 2026

🤖 Agents 2026-07-17 · Updated 2026-11-10 3 min read

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

  1. Retry once, only for idempotent operations and connection-class errors. A model retry doubles token cost.
  2. Fail over to a backup source or a faster model, and keep that path exercised.
  3. Skip non-critical tools and answer with what you have, labeled as incomplete.
  4. 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

Related Articles
2026-08-30
Best AI Headshot Generator for Real Estate Agents 2026: 6 Tools for Consistent Branding
2026-07-14
How to Build a Local AI Agent: Complete 2026 Guide
2026-07-16
AI Agent Fallback Strategy 2026
2026-07-12
Hermes Agent Launches Skills Hub: 672 Installable Skills Usher AI Agents into the "App Store" Era
2026-07-17
AI Agent Async Processing 2026
2026-10-18
Hermes Agent v0.22.0 released: runs without an API key, one-click local model installation

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.

💬 Comments (0)

No comments yet. Be the first!

Login to comment