StudyVisuallyStart studying
Part 09 · Production · Card 5 of 13

Timeouts and retries

Put a timeout and retry on every tool call, and return a clear error message to the model when it fails.

Last updated

Analogy

The same resilience you'd add around any flaky third-party API.

Code example

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

In an agent

A good error message lets the agent recover; a hung request just burns time and money.

Questions people ask

Do agent tools need timeouts and retries?

Yes. Tools call real services that can hang or fail. Put a timeout on every call, for example with AbortSignal.timeout, retry briefly on temporary errors, and return a clear error message to the model when it still fails. A hung request just burns time and money.

What should a tool return when it fails?

A short, clear error as the tool result, for example the order service timed out, try again or tell the user it is unavailable. Do not throw and crash the loop. With a readable error, the model can retry, pick another tool, or explain the problem to the user honestly.