AI booking agent project (TypeScript) · Chapter 5 of 12
Errors from tools
By the end of this chapter your agent survives everything a tool call can run into: the API saying no, the model writing a wrong argument, a bug in a tool, an API that is down for a moment. Each one becomes a result the model reads and acts on, a bug leaves its stack in the server log and nothing private in the model's hands, and a call is tried again only when trying again may work. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: When things go wrong
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of AI booking agent project (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Diwali week sells out. When Ghar Wapsi Express's cheap rows are gone, book_seats says so: "There are no 3 free seats side by side at ₹100 or less for showtime 18". Neha should hear exactly that, and choose a dearer row or another show. Today, with chapter 4's code, she gets this:
{"error":{"code":"internal","message":"Something went wrong on our side"}}
[500]
and the server log gets the real answer, which nobody reads:
ApiError: There are no 3 free seats side by side at ₹100 or less for showtime 18
at Module.bookSeats (/workspace/chapters/04-structured-outputs-solution/src/agent/tools/parda.ts:90:11)
...
status: 409,
code: 'no_seats'
}
runTool throws, and the error ends her whole request. It is the same for the model's own mistakes. In chapter 4, your parseArgs caught lab-chat-confused writing date_name instead of date and said so in one clear sentence; that sentence went to the log and Neha got a 500. The model, which could have fixed it in one more step, never saw it.
Datta adds a worry from the trial weekend: one booking was paid twice because a member sent "pay" again when the first answer was slow. An agent that "tries again" on its own can do the same, so retries need rules.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
run.ts: refusals become results
Write runTool(ctx, name, text, wait) in src/agent/tools/run.ts so that it never throws. args = parseArgs(name, text); on a BadArguments e return { error: e.message }. Then return the tool's result through callWithRetries (Task 3): await callWithRetries(() => FUNCTIONSname, agentConfig.toolRetries, wait); on an ApiError e return { error: e.message, code: e.code }.
The Check cancels showtime 19, then runs book_seats as Neha in it (showtime_cancelled), in showtime 999 (not_found) and for 3 seats under ₹100 (no_seats), find_showtimes for Ghar Wapsi Express on Saturday (the tool's own list), find_showtimes with date_name instead of date and a tool called book_tickets (parseArgs's sentences). Then it runs Neha's booking with lab-chat-confused, a model that gets one argument wrong in its first call: the run must go find_showtimes (wrong), find_showtimes (right), book_seats, and end answered.
- Task 2
run.ts: a bug in a tool must not end the run
Make runTool catch any other error too: print it for the server's log (console.error(e)) and return { error: 'The tool <name> failed (<e.name>). Tell the member it did not work.' }, never the error's own message.
The Check swaps find_shows for a function that throws new TypeError('password=parda-secret-123 ...') and wants an error that names find_shows and TypeError but not the password, and the error (with the password) on the server's log.
- Task 3 · interview question
Interview: run.ts, try again only when it may work
Write callWithRetries(fn, retries, wait): return await fn(); when it throws an ApiError whose status is in RETRY_STATUSES (502, 503, 504), await wait(500 * 2 ** attempt) (500, then 1000, ...) and try again, up to retries more times, then throw the error. Any other error is thrown at once, without a wait.
The Check calls it with two 503s then success (it wants 'done' after waiting 500 and 1000), three 504s (the 504 thrown after 500 and 1000), a 409 and a TypeError (thrown at once, no wait). Then it makes list_my_bookings time out once and wants runTool to try it again after waiting 500 through the wait it was given.
Nikhil will ask: book_seats timed out (a 504). Should runTool try it again? What could happen to Neha?
What you will learn
- An error is a result: why every tool call needs its tool message, and the 400 when one is missing.
- What the model does with an error: reads it, then answers or asks again.
- Three kinds of trouble: the model's arguments, the API's refusals, a bug in your code.
- Bugs: console.error for the log, and why the error's message never goes to the model.
- Retries: which statuses are worth another try, waits that double, and a wait function you can replace in tests.
- What a timeout does not tell you: whether the request happened.
More in When things go wrong
Build "Errors from tools" yourself
Parda members want to book by chat. You build the agent that finds the show, holds the seats through Parda's own API, and asks before it pays. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI booking agent project (TypeScript) are free. This one comes with the all labs plan.