Agents from scratch · Chapter 8 of 18
Planning
By the end of this chapter you can ask a model for a plan instead of a next step: the calls it would make, in order, with placeholders for what the lookups will find. You can check that plan, show it to a person, execute it without the model, and deal with a step that fails. 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
- Python
- In the all labs plan
The first 2 chapters of Agents from scratch are free. This one comes with the all labs plan.
Why you need this
The loop from chapter 3 decides one step at a time. That is its strength, and Meenakshi's problem with it: nobody knows what the agent will do until it has done it. When a customer on WhatsApp asks "what happens now?", or when her team wants to look over a refund before it goes through, "wait and see" is not an answer.
Anjali asked the question differently: could the agent say what it is going to do, before doing it? It can. You ask the model for a plan: the whole list of calls, written out in advance. A plan can be read, checked, approved, logged and replayed. It is also cheaper: one request to plan, then the steps run with no model at all. The price is that a plan is made at one moment, and the world keeps moving.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
plan.py: ask for a plan
Write two Pydantic models in plan.py: Step, with tool (text) and arguments (a dictionary), and Plan, with steps (a list of Step). Then write get_plan(request): one request to lab-chat with a system message (GharFix's booking assistant, saying today is 2026-10-22, asking it to plan the tool calls), the request as the user's message, GharFix's tools, tool_choice set to none, and a response format of type json_schema whose schema is Plan's JSON Schema. Return the reply as a Plan.
The check calls get_plan with Meenakshi's job J14 and compares the Plan with the model's reply.
- Task 2
show_plan.py: the plan for Meenakshi
Get a plan with your get_plan (import it from plan.py) for this request, exactly:
Book an electrician in Whitefield tomorrow: a switch board is sparking. I am Kiran Hegde, phone 9741234567.
Print it for Meenakshi before anything runs: first Plan for N steps: with the number of steps; then one line per step, numbered from 1: the number, a full stop and a space, the tool's name, and in brackets its arguments as key=value, in the plan's order, with a comma and a space between them. For another request, a line was:
1. find_technicians(service=plumbing, area=Koramangala, date=2026-10-23, slot=morning)
Last, print Changes data: followed by the names of the steps that are not lookups (a lookup's name starts with find_, check_ or get_), with a comma and a space between them, or nothing if there are none.
- Task 3
run_plan.py: execute a plan
Write execute(plan) in run_plan.py. It runs the plan's steps in order with GharFix's functions and returns the list of their results.
Before a step runs, fill its placeholders: an argument whose value is a placeholder like <technician_id from find_technicians> takes that key from the result of the step that ran that tool; when that result is a list, from its first row.
Stop at the first step whose result is an error (keep that result as the list's last). When a placeholder cannot be filled (the tool found nothing, or its result has no such key), add an error result, a dictionary with an error key, and stop there too: a step that cannot be filled must never run.
The check executes plans for a booking with no slot, for a service nobody offers in that area, and for a reschedule GharFix refuses.
- Task 4 · interview question
Interview: replan.py, a plan that went stale
replan.py plans two customers' bookings, runs each plan's lookups so Meenakshi can read who will come, and then books them one after the other. Both plans chose the same technician for the same slot, so the second booking fails, and the program reports None for that customer.
Fix book_all(requests) so that when a customer's booking fails, their request is planned again from the start (a new plan, with fresh lookups) and that plan is run. book_all returns a dictionary from each customer's name to their booking ID, or, if they still cannot be booked, to the error's text.
The check calls book_all with the two customers, and with three customers who all want the same slot.
What you will learn
- A planning request: tools offered, none called, a JSON plan back.
- Placeholders: how a plan refers to what a lookup will find.
- A plan as data: a Pydantic model you can show and check.
- Executing a plan, step by step, without the model.
- When a step fails: what the plan cannot know.
More in Agents that hold up
Build "Planning" yourself
GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. 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 are free. This one comes with the all labs plan.