# Timeouts and retries

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

AI developers · Part 09: Production · Card 5 of 13 · Last updated 2026-10-13

## Analogy

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

## Code example

`timeout.ts`

```typescript
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.

## Related concepts

- [Guardrails](https://studyvisually.com/learn/tech/ai-developers/production/guardrails)
- [Validate tool arguments](https://studyvisually.com/learn/tech/ai-developers/production/validate-tool-arguments)
- [Least privilege](https://studyvisually.com/learn/tech/ai-developers/production/least-privilege)
- [Prompt injection](https://studyvisually.com/learn/tech/ai-developers/production/prompt-injection)

Web page: https://studyvisually.com/learn/tech/ai-developers/production/timeouts-and-retries
Study this card in the app: https://studyvisually.com/app/learn?category=ai-developers&card=timeouts-and-retries
