Agents from scratch (TypeScript) · Chapter 10 of 18
Human in the loop
By the end of this chapter you can make an agent stop before it does anything that cannot be undone: sort its tools into safe and dangerous, pause a run at a dangerous call, save it, and carry on later with a person's yes or no, given to an exported function or on the command line. 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
Here is a booking that names no slot, worked through by the agent from chapter 9 (temperature: 0):
Book a carpenter in Indiranagar on 30 Oct: the bedroom door does not
close. I am Lakshmi Iyer, phone 9900887766.
[0] find_technicians({"service":"carpentry","area":"Indiranagar","date":"2026-10-30"})
[1] check_slots({"technician_id":"T29","date":"2026-10-30"}) (and T28, T24)
[2] book_slot({"technician_id":"T29","date":"2026-10-30","slot":"morning",...})
[3] answer: Booking GF100001 on 2026-10-30 is confirmed, for ₹399 (...)
Lakshmi never said morning. The model found free slots and booked the first one. If she works mornings, a carpenter knocks at a locked door and GharFix keeps ₹99 for the visit (policy 4.3). Meenakshi has seen this with her own team's new hires: helpful, quick, and deciding things that were the customer's to decide.
You might try a rule in the system message. With "Never book anything until the customer has confirmed the technician and the slot." added, the lab model ran the same four steps and booked T29 for the morning anyway. A real model follows such a rule much more often, but "much more often" is not a guarantee you can give Farooq, whose ledger gets the refund when it goes wrong. A rule in a prompt is a request. A rule in your loop is a fact.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
dangerous.ts: safe or dangerous
Export function isDangerous(name) from dangerous.ts. It returns false for GharFix's four lookups, which only read (find_technicians, check_slots, get_quote and get_booking), and true for every other name: the four tools that book, move, cancel or refund, and any name it does not know, even one that sounds harmless, or one that every JavaScript object happens to have, such as toString.
- Task 2
pause.ts: pause before a dangerous call
In pause.ts, export RunState as in chapter 9 (a zod schema and its type: run_id, question, status, messages, results, steps, answer) with one more field, pending: an array of the calls waiting for approval, each an object with the call's id, name and arguments (the JSON text, as the model wrote it), empty by default. Export save(state, path) and load(path) as in chapter 9.
Then export async function start(question, path): a new run (a system message saying today is 2026-10-22, the question as the user's), saved, then chapter 9's loop with lab-chat and GharFix's tools, saving after every step. Run every safe call as before. When the model's message has a dangerous call (use isDangerous from ./dangerous.ts), do not run it: push the message, run the message's safe calls, put each dangerous call in pending, set status to waiting, save, and return the state. When the model answers, the run is done with its answer; after 10 requests, failed.
The check starts a booking that names no slot.
- Task 3
decide.ts: yes or no
Export async function decide(path, approved) from decide.ts. It loads a waiting run (import what you need from ./pause.ts) and gives every pending call its tool message: when approved is true, run the call with GharFix's function and record its result; when it is false, run nothing and send a JSON object with an error key saying the customer did not approve it. Then empty pending, set status to running, save, and go on with the loop until the run is done, failed or waiting again. Return the state. A run that is not waiting is returned as it is.
The check starts the booking from task 2, then decides once with true and once with false.
- Task 4 · interview question
Interview: fix-refund.ts, a refund nobody approved
fix-refund.ts's runAgent(question, approve) asks a person before dangerous calls: approve(name, args) is the person, and answers true or false. Yet a customer's refund went through without anyone being asked, and when Meenakshi approved cancelling one of two bookings, both were cancelled.
Fix runAgent so that approve is called for every call that is not one of GharFix's four lookups, once per call, with the tool's name and its arguments as an object, and a call runs only if approve answered true for it. A declined call's result is an error the model reads. Lookups run without asking.
Keep the exported names and parameters and the system message. The check calls runAgent with a refund, two cancellations in one request, a reschedule and a booking, and its own approve.
What you will learn
- Safe and dangerous tools: which calls need a person.
- Pausing a run: the state saved as waiting, with the pending call.
- Resuming with a decision: run it on yes, an error result on no.
- Where the yes comes from: a function call or a command-line word, never a prompt that blocks.
- GharFix's own human in the loop: refunds above ₹2,000.
More in Agents that hold up
Build "Human in 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.