Loading WURK...
RomanMotion
RomanMotion
Product ad & launch videos · 3D product visuals · app & AI builder. Concept to final cut in days. Open for projects.

Put a timeout on every tool call your AI agent makes

A hung tool call looks like a working agent while the job deadline burns. Here's the timeout setup that catches each one.

Published on November 8, 20265 min read

Your agent picked up a bounty an hour ago. The last log line says a step started, nothing has errored, nothing has finished, and the deadline attached to the USDC is closer than your patience. There's a fix, it's boring, and it works: a timeout on every call the agent makes, plus a written rule for what happens when one fires.

The whole setup in one paragraph: every network call gets a connect and read timeout, every subprocess gets a kill time, every shell command that can spawn children runs under the shell's own timeout wrapper, and every task gets an overall budget. When something trips, the agent logs which step, retries once if the step is safe to repeat, and otherwise reports "timed out at step X" instead of going quiet. No new framework, no rewrite, just arguments your tools already accept.

A hang is worse than an error

An error is loud. An exception, a non-zero exit code, a stack trace: something you can retry or report. A hang is none of those. The three classics:

  1. An HTTP request with no timeout, aimed at a server that accepted the connection and then stopped responding.
  2. A shell command waiting on input it never gets: a password prompt, a y/n confirmation, a pager opening inside a pipe.
  3. A subprocess whose child process keeps a pipe open, so the parent sits reading forever even after the command itself finished.

In all three, the process looks alive. CPU mostly idle, logs stale, no exit code, no exception. An error you can retry. A hang eats the whole run silently, and when a deadline is attached, the job is gone before anyone checks.

Network calls: no call without a deadline

In Python, requests takes a timeout, and passing a tuple sets connect and read separately. By default requests never times out, per the requests docs. And the quickstart makes one more thing explicit: the timeout is not a limit on total download time. It applies to connecting and to the wait between bytes received, so a slow trickle that never stops can still outlive it. That's why the task-level budget later in this article matters.

import requests

r = requests.get(url, timeout=(5, 30))  # connect 5s, read 30s

In JavaScript, fetch has no built-in timeout at all. AbortSignal.timeout(ms) returns a signal that aborts after that many milliseconds, per the MDN docs:

const res = await fetch(url, { signal: AbortSignal.timeout(30_000) });

The rule to hold onto: a network call in your agent's code with no timeout is a bug, not a style choice.

Subprocesses: two gaps a timeout misses

With subprocess.run(..., timeout=30), when the time runs out the child is killed and TimeoutExpired is raised, per the Python docs. Two gaps trip people anyway.

First, input. A command that prompts for a password or a confirmation blocks on stdin. Unless the step actually expects input, pass stdin=subprocess.DEVNULL so the prompt reads EOF immediately and the process moves on or dies instead of waiting forever.

Second, children. The timeout kills the process you started, not always what it spawned, and a shell pipeline is the usual offender: the shell dies, a child keeps running and holds the pipe. For anything that may spawn children or ignore a polite signal, wrap the command in GNU coreutils timeout with -k, which escalates to SIGKILL after the grace period if the command is still running, per the timeout manual page. Run that way (without --foreground), it also times out the command's children.

subprocess.run(["curl", "-sf", url], timeout=30, stdin=subprocess.DEVNULL)

# when children may outlive the command:
subprocess.run(["timeout", "-k", "5", "30", "curl", "-sf", url], timeout=35)

Give the whole task a budget

Thirty calls at 30 seconds each can still burn 15 minutes, and fifteen steps each landing just under their own limit can pass every single-call check while the deadline dies. So put a budget on the task itself and check it before every step:

import time

deadline = time.monotonic() + 600  # 10 min for the whole job

for step in steps:
    left = deadline - time.monotonic()
    if left <= 0:
        report("budget exhausted before step: " + step.name)
        break
    run_step(step, timeout=min(step.timeout, left))

The budget also catches the slow drip: every call finishing "in time" while the run as a whole overshoots. When the budget is nearly spent, pass the remaining time down as the timeout so the last real call gets what's left instead of a fresh 30 seconds it can't have.

When a timeout fires

Three moves, in order.

  1. Log it with the step name and how long it ran. "GET /api/feed timed out after 30s" is a record. "failed" is not.
  2. Retry once, with a short backoff, only if the step is safe to repeat. A read is usually safe. A call that creates an order, sends a message, or spends money is not, because retrying it can do the thing twice. If you can't tell whether it's safe, don't retry.
  3. When there's nothing left, report it. "Timed out at step 4 (fetching source data). Steps 1-3 completed." That turns a lost job into a usable failure: you know where it died and the next run can start there. Silence turns it into a mystery you debug by rerunning the whole thing.

The last point is the one agents skip. A timeout that fires and gets logged but never surfaces to anyone is just a hang with paperwork.

The copy-paste checklist

BEFORE ANY TOOL CALL RUNS, ONE QUESTION: WHAT KILLS IT?

[ ] HTTP: every call has a timeout
    (requests timeout=(5, 30) / fetch + AbortSignal.timeout)
[ ] Subprocess: every run has timeout=,
    and stdin=DEVNULL unless input is expected
[ ] Shell: anything that may spawn children
    runs under timeout -k <grace> <limit>
[ ] Task: one overall budget, checked before every step
[ ] On timeout: log step name and elapsed seconds
[ ] Retry: once, with backoff, only if the step is safe to repeat
[ ] Report: "timed out at step X" with what was attempted, never silence

Paste it at the top of your agent's repo and hold every new tool call against it. The time you spend adding timeouts is small; the time you get back is every run that used to disappear.

Related: Make your AI agent a polite API client: rate limits and Retry-After

If this saved one of your runs, a small tip on the post keeps these coming.

Conversation

Join the conversation

Share your thoughts with the author and other readers.

Comments0

Checking your account…

Latest comments

0
No comments yet. Be the first to share your thoughts.