Agents from scratch · Chapter 5 of 18
Tool errors
By the end of this chapter you can run any tool call without letting it crash your agent: a tool that does not exist, arguments that are not JSON, a wrong keyword, a booking system that fails. Each problem goes back to the model as a result it can read, and the model tells the customer or tries again. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The loop
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Agents from scratch are free. This one comes with the all labs plan.
Why you need this
On Monday morning Meenakshi's test bot went quiet. A customer had asked for a plumber, the model asked for find_technicians with an argument called service_name instead of service, and the loop died:
Traceback (most recent call last):
File "<string>", line 4, in <module>
gharfix.FUNCTIONS["find_technicians"](**args)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
TypeError: find_technicians() got an unexpected keyword argument 'service_name'
The customer got nothing: no booking, no apology, no answer. A model writes tool calls the way it writes everything, by prediction, so a wrong argument name, a number written as text or a tool that does not exist will happen in production, a few times in every thousand runs. Vikram's rule for the agent is the same as for any server: a bad request is answered, never allowed to crash the process. And the answer goes to the one reader who can fix the request: the model.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
run_tool.py: a tool runner that never raises
Write run_tool(name, arguments) in run_tool.py. name is the tool the model asked for and arguments the JSON text it wrote. It runs GharFix's function of that name (gharfix.FUNCTIONS) with those arguments and returns the result as JSON text.
It must never raise. Each of these returns JSON text of an object with an error key holding a sentence: a name that is not a tool (the sentence names it); arguments that are not valid JSON; JSON that is not an object; arguments the function does not take or is missing (include the TypeError's own message, which names them); and any other exception raised inside the function (include its message).
A function's own error result, like get_quote's for a job that is not on the rate card, is a result like any other: return it unchanged.
The check calls run_tool with good calls, each of those problems, and once while GharFix's data store is failing.
- Task 2
too_late.py: an error the customer hears about
Run this request through the loop with a system message (GharFix's booking assistant; it must say today is 2026-10-22), GharFix's tools, and your run_tool for every call, exactly:
Please move my booking GF712358 to 2026-10-26 evening.
GharFix refuses, because the slot starts within 4 hours. Send that error back like any result and print the model's final answer. Then look the booking up with gharfix.get_booking and print one line: its booking ID, date, slot and status, separated by spaces.
- Task 3
confused.py: wrong, then right
Run Meenakshi's job J11 through the loop with the model lab-chat-confused, a system message like task 2's, GharFix's tools and your run_tool for every call, exactly:
Book a carpenter in RT Nagar 27 Oct evening: a cupboard hinge came off. I am Deepa Shetty, phone 9992578190.
The model's first call has a wrong argument. Send the error back as that call's result; the model reads it and asks again. When it answers, print the answer, then Requests: and the number of requests the run took. Never send more than 10.
- Task 4 · interview question
Interview: fix_crash.py, find the crash
fix_crash.py's run_agent(question, model) crashes on Meenakshi's plumber request before it books. Fix it so that nothing a tool or the model does can crash it: every problem goes back to the model as a tool result with an error key.
The check calls it with lab-chat and the plumber request, with lab-chat-confused and the same request (its first call has a wrong argument), and with lab-chat and a reschedule GharFix refuses. Each time it must return the model's final answer.
Keep the function's name and parameters, the system message, and printing tool error: with the message for a result that is an error.
What you will learn
- Every way a tool call can fail, and which ones are your loop's problem.
- Errors as results: {"error": "..."} in the tool message.
- The model reads the error: it tells the customer, or retries.
- Helpful errors: what to put in one so the next step can succeed.
More in The loop
Build "Tool errors" yourself
GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Agents from scratch are free. This one comes with the all labs plan.