esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

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.

14 min readIntermediateCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

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.

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.

Key words

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.

Three kinds of tool failure

Tool call fails

an error comes back to your code

Timeout or busy service

may work if tried again

Wrong or missing input

the model can correct it

Blocked or over a limit

retrying will not help

Retry with backoff

wait, try again, capped

Model fixes the call

error text goes back to it

Person decides

with the case and the error

  1. 1A timeout or a busy service goes this way.
  2. 2Wrong or missing input goes this way.
  3. 3A rule that blocks the action goes this way.
  4. 4Your code waits, then tries again, a few times at most.
  5. 5The error text goes back to the model, which can correct the call.
  6. 6A person gets the full error and the case to decide.
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.

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. 1

    Retry only the temporary kind

    Your code checks the error type before it waits. Bad input and blocks skip the loop entirely.

  2. 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. 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. 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. 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.

The retry loop, with a cap

Call the tool

one try, logged

What came back

Worked

use the result

Temporary fault

not a bad input or a block

Wait longer, then retry

random bit added each time

Stop at the cap

a person gets the log

  1. 1The first try goes out, and its result comes back.
  2. 2A temporary fault starts the wait.
  3. 3The wait ends, and the call goes out again.
  4. 4After the cap, the loop stops and the case goes to a person.
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.

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

  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."

    BetterIt 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."

    BetterIt may lose count and cannot see side effects. The cap and the stop belong in code.

  • Trap"Any error means try again."

    BetterSome 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?

Quick check

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

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q173How do you stop endless agent loops?11 min
  2. Q175What makes a tool-calling loop reliable enough to ship?13 min
  3. Q337Timeouts, retries and backoff?Coming soon
  4. Q179Excessive agency and least privilege?11 min
  5. Q186What is tool-result poisoning?13 min
  6. Q187How do you control agent cost?13 min

Last updated on 26 November 2026

Building an agent that calls real systems?

I design and build AI agents, including the tool layer, the error handling and the checks a person sees. Book a short call and tell me which systems your agent touches.