Building with LLMs (TypeScript) · Chapter 23 of 24
Capstone
By the end of this chapter you have built SafarLine's WhatsApp support assistant: tools over the real trips and bookings, a typed triage that decides who answers, an answer streamed as it is written, and tests that prove it works and catch the bugs that matter. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Building with LLMs
- 5 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Building with LLMs (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Zoya has set the date: "The assistant answers on WhatsApp from next Monday." Lata has one condition: "Lost bags and late buses come to us. People are upset; a person answers them." And Nikhil has his: "It ships with tests, or it does not ship."
Every piece exists already: tool calling and the tool registry (chapter 17), structured output with zod (chapters 12 and 13), classification (14), streaming (5), today's date (13), guardrails (22), tests (16). A product needs them together, in an order that makes sense, with the joins between them done properly. That is what this chapter is about.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
tools.ts: the assistant's tools
Export the tools of SafarLine's WhatsApp assistant, over /workspace/data/trips.json and bookings.json.
findTrips(fromCity, toCity, date) returns that day's trips between the two cities that still have seats, earliest first, each an object with exactly id, from, to, date, departs, bus, fare and seats_left; city names match in any case.
getBooking(pnr) returns the booking with exactly pnr, name, from, to, date, departs, bus, seats, amount and status (not the phone), or { error: 'no booking with PNR SLZZZZZZ' } for an unknown PNR.
TOOLS describes both for the API: find_trips with the required parameters from_city, to_city and date, get_booking with the required parameter pnr.
runTool(name, args) runs a tool from the model's JSON arguments and returns the result as JSON text; an unknown tool, arguments that are not JSON or the wrong arguments return JSON with an error key, never an exception. The check also offers TOOLS to lab-chat and asks for a bus.
- Task 2
assistant.ts: answer with tools
Export answer(message, model = 'lab-chat'): send model, at temperature 0 and with TOOLS, a system message (your instructions, then Today is 2026-10-22. from TODAY, then the whole policy) and the message. While the reply asks for tools, run every call with runTool, send all the results back and ask again; return the first text reply. After MAX_ROUNDS (5) requests without one, return HANDOFF.
Export TODAY, HANDOFF and MAX_ROUNDS with the values in the file. The check asks about two PNRs at once, a bus on a date, an unknown PNR, a bus for kal and a policy question, and runs answer with the model lab-chat-loopy, which asks for tools forever.
- Task 3
assistant.ts: triage and a typed reply
Export Triage, a zod schema with intent (one of booking_request, cancellation, refund_status, delay_complaint, fare_query, lost_item, date_change, praise) and pnr (a string or null), and its type of the same name; and triage(message), which asks lab-chat at temperature 0 for a Triage with a strict JSON schema and returns it.
Export the type Reply, { intent, text, handoff }, and handle(message): it triages the message; a lost_item or a delay_complaint returns { intent, text: HANDOFF, handoff: true } without answering; anything else returns its intent, answer(message) as the text, and handoff false.
- Task 4
assistant.ts: stream the answer
Export streamAnswer(message): the same loop as answer, but every request has stream: true. Write each piece of text as it arrives with process.stdout.write, then one newline at the end, and return the whole text.
When a streamed reply asks for tools, rebuild its tool calls from the pieces (each piece carries the index of its call; the id and name come first, the arguments in many pieces), send them back in an assistant message with the tool results, and stream again. The check asks about two PNRs and compares what you sent back with what the model streamed.
- Task 5
assistant.test.ts: tests that catch bugs
Write vitest tests for the assistant, using only findTrips and getBooking from ./tools.ts and answer, triage and handle from ./assistant.ts.
The check runs your tests on your files and on another correct assistant (both must pass), then on six copies with one careless change each, and each copy must make a test fail: findTrips lists full buses; getBooking crashes on an unknown PNR; answer forgets to tell the model today's date; answer runs only the first tool call; triage loses the PNR; handle answers lost items itself.
What you will learn
- The shape of an assistant: triage first, then a person or the tool loop, then the answer, streamed.
- Tools designed for a model: small results, no personal data the model does not need, errors as data, arguments checked with zod.
- A loop that always ends, whatever the model does.
- Streaming with tools: rebuilding tool calls from their pieces.
- Testing an LLM application: facts, not wording, and tests that catch real bugs.
More in Building with LLMs
Build "Capstone" yourself
SafarLine, an intercity bus company, drowns in WhatsApp messages. You build its first AI assistant, 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 Building with LLMs (TypeScript) are free. This one comes with the all labs plan.