# How Should an Agent Handle Tool Errors and Retries?

> Sort each tool error first, retry only the temporary ones in code with a cap, and hand the rest to the model or a person, explained simply for your AI interview.

**In short:** An agent should sort each tool error before it acts. Retry only temporary faults, in your code, with a growing and randomised wait and a hard cap. Send bad input back to the model to fix, and stop for a person when a rule blocks the action.

AI and LLM interview question Q176, Agents and tool use. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-should-an-agent-handle-tool-errors-and-retries

## Say it in 30 seconds

When a tool fails, I first ask what kind of failure it is. A timeout or a busy service is temporary, so my code waits a bit longer after each try, adds a random bit to the wait, and stops at a small cap. Wrong input goes back to the model with a clear message so it can fix the call. A blocked action, like a payment over a limit, goes to a person. I keep retries out of the prompt, because the model can lose count and it cannot see what the first call already did.

## Say it in 2 minutes

A tool can fail in three ways, and each one needs a different answer. The first is a temporary fault, like a timeout or a busy service. Trying again later often works. The second is bad input, like a missing claim ID. The model can fix that itself. The third is a block, like a payment over the approval limit. Retrying will not change the answer, so a person has to decide.

For temporary faults, the retry belongs in my code. I wait a little longer after each failed try, which is called backoff. I add jitter, a random bit in each wait, so many clients do not all retry at the same moment. I cap the tries at a small number and log every attempt. Microsoft's guidance warns that a large number of quick retries can push a busy service further over the edge.

Before any retry, I ask whether running the action twice is safe. A read is safe. A payment is not, unless the payment service checks an ID for duplicates. If I cannot tell whether the first call worked, I stop and ask a person. Asking a person costs a little time. A second charge costs far more.

Last, the errors go back in plain words. A message that says the claim ID is missing helps the model fix the call, while a bare error code does not. Anthropic's writing guide says to write error responses that say what to fix. And every loop has a limit, because a model that keeps retrying wastes money and time.

## What is a tool error, in plain words?

A tool is a function the agent can call, such as looking up a policy or sending a payment. A tool error is what comes back when that call fails. For your own tools, the model never runs the code. Your code runs each call and decides what happens next.

Think of a courier whose van breaks down on the motorway. Wait a while, take the next van, and the parcel still arrives. But if the address on the parcel does not exist, driving there again will not help. Tool errors work the same way: some clear up with time, and some never will.

A real case: a claims triage agent calls the policy service and it times out. A few seconds later, on the third try, the same kind of call works. The same agent then calls the claim lookup with no claim ID, which the model can fix by asking for the ID. A third call asks to pay 6,200 against an approval limit of 5,000, and no amount of waiting changes that answer.

- **Temporary fault:** A failure that may clear up on its own, such as a timeout or a busy service.
- **Backoff:** Waiting longer after each failed try, so you do not pile onto a struggling service.
- **Jitter:** A random bit added to each wait, so many clients do not retry at the same moment.
- **Idempotent:** Safe to run twice: running it again does not repeat the effect, such as a second charge.

## Which errors are worth a retry?

The first question is always what kind of failure it is. Anthropic's tool guide says that when a tool fails, or the call is invalid, you send back an error result so the model can see it. The model can then call the tool again or tell the user what happened, so your loop still needs its own limit.

- **Temporary: Retry in code.** A timeout, a busy service or an overloaded server. Waiting and trying again may work.
- **Bad input: Send back to the model.** A missing ID or a wrong type. The model can correct the call and try once more.
- **Blocked: Stop for a person.** A rule says no, such as a payment over the limit. The same call fails the same way every time.

Here is a claims team on a busy morning, with made-up numbers. Out of 400 tool calls, 36 time out, 9 arrive without a claim ID, and 3 are payments over the approval limit. The right split is 36 timeouts on safe reads retried in code, 9 sent back to the model, and 3 sent to a person. Retrying the 3 only repeats the refusal. Sending the 36 to the model would waste its time and your money on a fault that a short wait would fix.

*Diagram, Three kinds of tool failure: A failed tool call is sorted into one of three kinds. Only the temporary kind goes to a wait and a retry in your code. The other two go back to the model, which can fix the input, or to a person, who decides on a block.*
1. A timeout or a busy service goes this way.
2. Wrong or missing input goes this way.
3. A rule that blocks the action goes this way.
4. Your code waits, then tries again, a few times at most.
5. The error text goes back to the model, which can correct the call.
6. A person gets the full error and the case to decide.

Some errors never clear up. Anthropic's API guide says a spend-cap 429, the error for an account that has hit its spending limit, keeps failing until access resumes. So a retry loop should limit its retries rather than keep knocking.

## How should the retries work?

1. **Retry only the temporary kind:** Your code checks the error type before it waits. Bad input and blocks skip the loop entirely.
2. **Check it is safe to run again:** Ask whether a second run does the same thing as the first. If you cannot tell, stop and ask a person. Only then wait and retry.
3. **Wait longer each time:** Use backoff: the delay grows after each failure. Microsoft's guidance allows the delay to grow step by step or exponentially, depending on the failure.
4. **Add jitter:** Add a random bit to each wait. AWS's engineers found that if every failed call backs off to the same time, they overload the service again when they retry together.
5. **Cap the tries and log each one:** A small cap, such as three tries, is a sensible start to tune later. Past the cap, the case goes to a person with the log.

*Diagram, The retry loop, with a cap: A temporary fault sends the call into a wait, then the call goes out again. Each pass counts one try, and at the cap the loop stops and a person gets the log. A worked result never enters the loop.*
1. The first try goes out, and its result comes back.
2. A temporary fault starts the wait.
3. The wait ends, and the call goes out again.
4. After the cap, the loop stops and the case goes to a person.

The official guides back these rules, in their own words. These four lines carry the most weight:

> "When a client tool fails or a call is invalid, send a tool_result block with is_error set to true." (Anthropic, tool use docs)
>
> "Cancel. If the fault indicates that the failure isn't transient or is unlikely to be successful if repeated, the application should cancel the operation and report an exception." (Microsoft, Azure retry pattern)
>
> "In our experience, a good place to start is to remember that retries are selfish." (Amazon Builders' Library)
>
> "A spend-cap 429 keeps failing until access resumes, so limit your retries after a 429." (Anthropic, API errors)

## Why do retries live in code, not in the prompt?

A prompt that tells the model to keep trying until the call works has three problems. The model cannot see whether the first call already did something. It may lose track of how many tries it has made. And each extra try adds load to a service that is already struggling.

Amazon's engineers put the cost bluntly: retries are selfish, because each one asks the service for more of its time. The engineers say they retry only when the dependency looks healthy, and stop once retries stop helping.

Anthropic's SDK (the code library that calls Claude) already retries failed calls to Claude itself. By default it tries again up to two times, and the max_retries setting changes that. It retries connection errors, timeouts and certain status codes. That covers the call to the model. Your own tool wrapper, the small piece of code that runs each tool, still needs its own rules.

> **Tip:** Asked how to retry safely? Say it in order: sort the error, check the call is safe to repeat, retry only the temporary kind with backoff and jitter, cap it, and send the rest to the model or a person.

Here is that wrapper in plain Python. The three fake tools fail in three ways, and the wrapper answers each one differently. The numbers are made up. The demo retries only a timeout, and it does not check that a repeat is safe, so do not copy it for payments. The payment case below covers that.

```python
# A claims agent's tool wrapper, made-up numbers. Three kinds of failure, three different answers.
import random

rng = random.Random(7)  # fixed seed, so the run repeats exactly

def call_with_retries(tool, max_tries=3):
    for attempt in range(1, max_tries + 1):
        kind, result = tool(attempt)
        if kind == "ok":
            return f"OK on try {attempt}: {result}"
        if kind == "bad_input":
            return f"RETURN TO MODEL: {result}"
        if kind != "timeout":  # a block or an unknown error: a person decides, never a retry
            return f"STOP, ask a person: {result}"
        if attempt < max_tries:  # temporary fault: wait longer each time, plus random jitter
            wait = 2 ** attempt * rng.uniform(0.5, 1.0)
            print(f"  temporary fault, wait {wait:.2f}s")
    return "STOP after the cap: send to a claims handler"

def get_policy(attempt):
    return ("ok", "policy P-1042") if attempt == 3 else ("timeout", None)

def request_payment(attempt):
    return "blocked", "6200 is over the 5000 limit"

def lookup_claim(attempt):
    return "bad_input", "claim_id is missing"

for name, tool in [("get_policy", get_policy), ("request_payment", request_payment), ("lookup_claim", lookup_claim)]:
    print(name, "->", call_with_retries(tool))
```

Output:

```text
  temporary fault, wait 1.32s
  temporary fault, wait 2.30s
get_policy -> OK on try 3: policy P-1042
request_payment -> STOP, ask a person: 6200 is over the 5000 limit
lookup_claim -> RETURN TO MODEL: claim_id is missing
```

### How would you handle a payment that times out?

This is the hard case. A payment call times out, and you do not know whether the bank took the money. A blind retry could charge the customer twice, and nothing in your code would notice.

- **Blind retry: Try the payment again.** If the first call went through, the customer pays twice, and the wrapper never noticed.
- **Safe path: Check, then decide.** Set a request ID before the first call, a tag the service uses to spot a repeat. After a timeout, look up the payment by that ID. If it exists, stop. If not, retry with the same ID, but only if the payment service checks for duplicates.

Microsoft's guide makes the rule explicit. A retry is safe for an operation that is idempotent. Otherwise, a retry could run the operation more than once, with side effects. A payment with no ID the service checks is not safe to retry, so the answer is to stop and ask a person.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Sort each error into temporary, bad input or blocked before acting.
- [ ] Retries live in code, with backoff, jitter and a small cap.
- [ ] Retry only what is safe to run twice, and check first.
- [ ] Errors go back to the model in words it can act on.
- [ ] Blocked actions go to a person, with the full error and the case.
- [ ] Every try is logged, so you can see loops and cost.

**Common traps**

- Trap: "Just retry until it works." Better: It needs a cap and a reason to stop. Past the cap, send the case to a person with the log.
- Trap: "The model will know when to stop." Better: It may lose count and cannot see side effects. The cap and the stop belong in code.
- Trap: "Any error means try again." Better: Some errors never clear up, such as a spend cap or a blocked payment. Sort first, then retry only the temporary kind.

## Can you spot the error worth a retry?

**Spot the error worth a retry:** Your claims agent's wrapper logs four failures. Which one belongs in your code's wait-and-retry loop?

A. claim_id is missing from the request
B. request_payment: 6200 is over the 5000 limit
C. get_policy: timeout, the service is busy
D. policy P-1042 ended in 2023, so the cover is closed

**Answer:** C. A timeout while a service is busy is a temporary fault. Microsoft says these faults are typically self-correcting, so a wait and another try may work. The other three fail the same way on every try: a missing field goes back to the model, and the limit and the closed policy go to a person.

**Quick check:** The send_email tool keeps returning an error that the recipient is not on the approved list. What should the wrapper do?

A. Retry five times with a longer wait each time
B. Stop and send the case to a person, since the rule will not change on a retry
C. Ask the model to try again with the same recipient
D. Let the model pick another recipient and send

**Answer:** B. A block is a rule, so waiting will not change it. Letting the model pick another recipient could let a hostile message steer the call past the rule. A person decides.

## Can you answer these from memory?

**Flash cards**

- **What are the three kinds of tool error?** Temporary faults, bad input, and blocked actions. Each has its own next step.
- **Where should retries live?** In your code, with a counter and a cap. Not in the prompt.
- **What is backoff?** Waiting longer after each failed try, so you do not overload the service.
- **What is jitter for?** A random bit in each wait, so clients do not all retry at the same moment.
- **Can you retry a payment blindly?** No. Check it first, or use a request ID that the service checks for duplicates.
- **When does a retry stop?** At a small cap. Then the case goes to a person with the log.

## Likely follow-up questions

- Q173 How do you stop endless agent loops? https://designervenkat.online/interview/ai/how-do-you-stop-endless-agent-loops
- Q175 What makes a tool-calling loop reliable enough to ship? https://designervenkat.online/interview/ai/reliable-tool-calling-loop
- Q337 Timeouts, retries and backoff? (full answer coming)
- Q179 Excessive agency and least privilege? https://designervenkat.online/interview/ai/excessive-agency-and-least-privilege
- Q186 What is tool-result poisoning? https://designervenkat.online/interview/ai/what-is-tool-result-poisoning
- Q187 How do you control agent cost? https://designervenkat.online/interview/ai/how-do-you-control-agent-cost
