Agents from scratch (TypeScript) · Chapter 3 of 18
The loop
By the end of this chapter you can write an agent's loop as an async function: send the request, run every call, push the model's message and the results, ask again, and stop when the model answers. You can read a run step by step and print it as a transcript. 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
- 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 1 you walked a booking through by hand: one request, a call, a result, another request. Meenakshi has forty jobs on her list today, and every one takes a different number of steps. A reschedule needs one tool; a booking needs two; a booking with no slot needs three. Nobody can write those steps out in advance, because the model decides the next one only after it reads the last result.
So the code cannot be a fixed sequence of requests. It has to be a loop that keeps going for as long as the model asks for tools, and stops when it answers. That loop, about twenty lines, is the heart of every agent, in every framework. Vikram wants you to own it: where it starts, what it keeps, when it stops, and what it costs. The rest of this lab builds on the one you write here, runAgent.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
agent.ts: the loop as a function
Write and export an async function runAgent(question) in agent.ts. It starts a conversation with a system message (GharFix's booking assistant; it must say today is 2026-10-22) and the question as the user's message, and offers lab-chat GharFix's tools (TOOLS).
While the model asks for tools, run every call with GharFix's function (FUNCTIONS), push the model's own message and one tool message per call with the result as JSON, and ask again. When the first choice's finish reason is stop, return an object { answer, requests }: the text of that answer, and the number of requests sent. Never send more than 10 requests for one question; if the tenth still asks for tools, return null as the answer.
Importing agent.ts must send nothing (put any try-out run under if (import.meta.main)). The check calls runAgent with three of Meenakshi's jobs from /workspace/data/jobs.jsonl: a booking, a reschedule and a quote. Later chapters build on runAgent, so keep its name and what it returns.
- Task 2
steps.ts: watch each step
Send lab-chat Meenakshi's job J09, exactly:
Book a carpenter in Bellandur 29 Oct morning: I need a new wardrobe assembled. I am Priya Rao, phone 9515819658.
with the same kind of system message as task 1 and GharFix's tools, and run the loop. For every request, print one line: the step number from 1, the first choice's finish reason, the names of the tools the model asked for (if any), and the prompt tokens the request used. For another job, the lines were:
step 1: tool_calls find_technicians, 963 prompt tokens
step 2: tool_calls book_slot, 1120 prompt tokens
step 3: stop, 1312 prompt tokens
Several tools in one reply are written with a comma and a space between them; a step that answers in text has no names. After the last step, print the model's final answer.
- Task 3
transcript.ts: the run as a transcript
Run Meenakshi's job J12 through the loop, exactly:
Book an electrician in BTM Layout 30 Oct evening: the geyser is not heating. I am Akshay Sharma, phone 9670169067.
with the same kind of system message and GharFix's tools. Keep the model's final answer in the conversation too, as its last message. Then print the conversation, one line per message, in order:
a system, user or answering assistant message: its role, a colon, a space and its text;
an assistant message with tool calls: assistant: calls followed by the tools' names, with a comma and a space between them;
a tool message: tool: followed by the first 60 characters of its text.
Your file must pass typecheck.
- Task 4 · interview question
Interview: fix-loop.ts, fix the loop
fix-loop.ts runs an agent for two customers in a row, but it stops with an error from the API at its second request. Fix it so that every request is one the API accepts (the model's own message with its calls, then one tool message per call), and so that each customer's run starts from its own conversation: the system message and that customer's message, nothing from the customer before.
Keep runAgent(question), the system message, the two customers' messages and the two answers it prints, one per line.
What you will learn
- The loop: request, run the calls, push, repeat.
- When to stop: finish_reason, and a cap on requests.
- The messages array is the run's memory, and what each request holds.
- Reading a run step by step, and as a transcript.
More in The loop
Build The loop 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.