AI booking agent project (TypeScript) · Chapter 3 of 12
The loop
By the end of this chapter you can run an agent from a member's message to an answer: ask the model, run the tools it asks for, send each result back with its call's id, and ask again, with every request carrying the whole conversation. And you can stop a model that never answers. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The loop
- 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
Chapter 2's tools work, but only by hand. Neha's "Book two seats for Ghar Wapsi Express on Saturday evening, near the aisle, under ₹200 each" needs two of them in order: find the showtime, then book the seats in it, the second step using the showtime_id the first one found. In the trial weekend Datta's box office did exactly that by hand, 286 times.
The model does one step per request and remembers nothing between requests (chapter 1). So something has to run each tool it asks for, carry the result back, and ask again until it has an answer for Neha. That something is a loop of about fifteen lines, and Nikhil wants one more thing from it: it must always end, even when the model does not.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
loop.ts: run the tools the model asked for
Write runTools(ctx, reply, messages, calls) in src/agent/loop.ts. For each call of reply.calls, in order, it runs result = await runTool(ctx, call.name, call.arguments) and pushes { role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) } onto messages and { name: call.name, arguments: call.arguments, result } onto calls. It returns nothing.
The Check gives your runTools a reply with two calls, find_showtimes for Ghar Wapsi Express on Saturday evening and find_shows for comedy, as Neha, and reads the two lists: a tool message per call in order with its id, the content the JSON of the tool's result (showtime 18 for the first), and the calls with their names, arguments and results.
- Task 2
loop.ts: the loop
Write runAgent(ctx, conversation, { tools, maxSteps }) in src/agent/loop.ts. Work on a copy of the conversation, with TOOLS when tools is not given. Repeat: reply = readReply(await chat(messages, tools)); push reply.message onto the messages; if the reply has no calls, return { status: 'answered', answer: reply.text, messages, calls, steps: the number of model calls made }; otherwise await runTools(ctx, reply, messages, calls) and ask again.
The Check runs Neha's "Book two seats for Ghar Wapsi Express on Saturday evening, near the aisle, under ₹200 each" through your runAgent and reads every request the lab model received: find_showtimes, then book_seats, each request sending everything so far, every call's result sent back, then the answer. It wants the list it gave you unchanged, the status answered, the last reply's text, 3 steps, the messages ending with the answer, both calls in calls, and one pending booking for Neha.
- Task 3 · interview question
Interview: loop.ts, the step limit
Make runAgent ask the model at most maxSteps times (agentConfig.maxSteps, 6, when it is not given). When they are used up without an answer, push { role: 'assistant', content: answer } with answer = stepLimitAnswer(maxSteps) and return { status: 'step_limit', answer, messages, calls, steps: maxSteps }.
The Check runs your loop with lab-chat-loopy, a model that asks for a tool again after every result, with maxSteps 3 (exactly 3 requests, then the status step_limit and the stepLimitAnswer answer pushed), then with agentConfig.maxSteps set to 2 (2 requests). Then POST /agent/chat as Neha must answer 200 with the status answered.
Nikhil will ask: why a limit on steps, when the loop already stops as soon as the model answers?
What you will learn
- The tool message: a result sent back, with tool_call_id and the result as text.
- The order of messages: an assistant message with tool_calls, then one tool message per call.
- The 400s you get when that order breaks.
- The loop: model, tools, results, until it answers in words, with the whole conversation resent every time.
- What each step costs: tokens and seconds, growing with the conversation.
- A step limit: a loop that ends on its own, tried on lab-chat-loopy.
More in The loop
Build The loop 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.