Agents from scratch (TypeScript) · Chapter 7 of 18
Validating arguments
By the end of this chapter you can check every tool call before it runs: one zod schema per tool, rules written once and shared, an error that names every wrong field, and a loop in which the model reads that error and corrects itself. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Agents that hold up
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Agents from scratch (TypeScript) are free. This one comes with the all labs plan.
Why you need this
In chapter 5 you made the loop survive bad calls: a refusal or a bad name became an error result instead of a crash. But runTool still runs whatever GharFix's function will take, and GharFix's functions were written to be forgiving with people typing into a form. Meenakshi found what that means on a Monday morning: a booking whose phone number the technician could not call, made from a call nobody had checked. Watch how much bookSlot accepts:
bookSlot({ technician_id: 'T18', date: '2026-10-24', slot: 'evening', job: 'fan-repair',
area: 'Hebbal', customer_name: 'Sana Desai', phone: '+91 96576-74999' }).booking_id;
GF100001
It took the last ten digits and booked. That is kind to a person; it is risky for an agent, whose arguments are text a model guessed. A tool call is the model's best reading of a message, and it can be a wrong key, a number where text goes, a date in the wrong shape, a slot GharFix does not have. Vikram's rule for the team: never run a call you have not validated.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
tool-args.ts: a zod schema per tool
In tool-args.ts, write one zod schema for the arguments of each of GharFix's eight tools, with the same keys as TOOLS, and export them as ARGS: an object from each tool's name to its schema.
Every schema refuses keys it does not have and needs every required argument. Every value is text, as GharFix's functions expect it: a number where text belongs is refused, and nothing is converted or reshaped. Besides that: service is one of plumbing, electrical, carpentry or ac; slot is one of morning, afternoon or evening; a date is a real day written YYYY-MM-DD (2026-02-30 is refused); technician_id is T and two digits; booking_id is GF and six digits; phone is exactly 10 digits, the first from 6 to 9. find_technicians's date and slot may be left out.
The check validates good arguments for every tool with safeParse, and arguments with each of these problems, and reads which fields each refusal names.
- Task 2
validated-call.ts: validate, then run
Write and export validatedCall(name, argumentsJson) in validated-call.ts: name is the tool the model asked for and argumentsJson the JSON text it wrote. Validate the arguments with the tool's schema from ARGS (import it from ./tool-args.ts) before anything runs. When they are valid, run GharFix's function with the validated data and return the result as JSON text; a function's own error result is returned unchanged.
Otherwise return JSON text of an object with an error key, and run nothing: for a name that is not a tool (toString included), a sentence naming it; for text that is not JSON, a sentence saying so; for arguments that fail validation, one sentence that names every field that failed (a stray key counts as a field) and gives zod's message for it, so the model can correct all of them at once. It never throws.
The check calls it with good calls, a call GharFix itself refuses, an unknown tool, text that is not JSON, a JSON array, the confused model's wrong key, and a booking whose phone has a space in it.
- Task 3
confused.ts: the error the model reads
Run Meenakshi's job J02 through the loop with the model lab-chat-confused, a system message (GharFix's booking assistant; it must say today is 2026-10-22), GharFix's TOOLS, and your validatedCall for every call, exactly:
Book a plumber in Bellandur 26 Oct morning: the bathroom drain is blocked. I am Nikhil Prasad, phone 9962962975.
For every call, print one line as it runs: the tool's name, a colon and a space, then ok, or the text of the result's error. When the model 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-validator.ts, a slot that slips through
fix-validator.ts validates book_slot and reschedule_booking calls, and lets customers' own words for a slot through (shaam is evening, subah is morning). Meenakshi found a morning booking for a customer who had asked for raat, at night: GharFix has no night slot, so that call should never have run.
Fix it so that a slot that is not one of the words in SLOT_WORDS (in any case, with spaces around it) is refused, whatever its type: validatedCall returns an error that names the slot field and says what it got (the value itself, a number included), and books or moves nothing. The customers' words must keep working, and a refused call's error must say which field failed and why, for any field.
Keep validatedCall(name, argumentsJson), SLOT_WORDS and the two schemas' keys, all exported.
What you will learn
- Why TypeScript's types cannot check a tool call, and what can.
- A zod schema per tool: the contract every call must meet.
- Rules written once (a date, a slot, a booking ID) and shared.
- Keeping values in the shape the function expects, and why z.coerce is the wrong fix.
- The error the model reads: every problem, by field, in one result.
More in Agents that hold up
Build "Validating arguments" yourself
GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. You build it in TypeScript. 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 (TypeScript) are free. This one comes with the all labs plan.