How should an agent handle tool errors, timeouts and retries?
1.2k views
62 of 83
1
The problem
Question presented to candidate:
"Your agent calls a dozen tools: search, a CRM, a payments API, a report builder. Tools time out, get rate limited, and get called with bad arguments. Walk me through how the agent and the code around it should handle each kind of failure without looping, double-charging anyone, or burning the budget."
What a strong answer should cover:
Classify before reacting: transient faults (timeouts, 429, 5xx) are retried by the runtime; semantic errors (invalid arguments, not found, permission denied) go back to the model as an error tool result with an actionable message; anything else stops the run cleanly
Retry mechanics: capped exponential backoff with jitter, honouring Retry-After, a per-attempt timeout and a total deadline, and a maximum number of attempts
Writes and ambiguity: a timeout on a write does not tell you whether it committed, so writes carry an idempotency key that the server uses to replay the first result instead of acting twice
Protecting the loop: a per-tool circuit breaker after repeated failures, a cap on consecutive tool errors, and the agent's overall step and cost budgets
What the model sees: short, sanitised error text (no stack traces, paths or secrets), flagged as an error (for example is_error: true in Anthropic tool results, isError in MCP), saying what to change or that the tool is unavailable
Observability: every attempt logged with status, latency and the reason the wrapper gave up
Clarifying questions expected:
Which tools have side effects, and do their APIs accept idempotency keys?
What is the latency budget for one agent task, and is a partial answer acceptable when a tool is down?
Code / implementation expected: Optional โ a tool wrapper that classifies errors, retries with backoff and returns a structured error result to the model.
error-handling
retries
agents
resilience
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Engineers putting an agent in front of real APIs, where things fail every day, and anyone asked to explain how an agent stays reliable when its tools do not.
Difficulty: Hard
Verified with: Node 22.22.0 and Python 3.12.3 (both examples below, identical output). Error-result formats were checked in October 2026 against the Anthropic "Handle tool calls" docs page and the MCP tools specification (modelcontextprotocol.io).
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. All program output shown was produced by actually running the examples; the Verified section quotes it in full.
1. Why This Even Matters โ A Story First
You send an assistant to the bank to pay a supplier. Three things can go wrong.