Build your own coding agent · Chapter 12 of 20
Plan mode
By the end of this chapter you can run Kaarigar in two phases, a plan first and the work after, with a person between them who approves the plan or rejects it with a reason; and you can say why a rule in the prompt does not make an agent read-only, while the tools you offer do. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The loop that fixes
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Build your own coding agent are free. This one comes with the all labs plan.
Why you need this
MD-107 says night fares are short on long trips, and Mahesh-bhau, who reported it, has a theory: round_fare in meterdown/money.py cuts the paise off. Kaarigar believes him. It changes round_fare to round up, and 4 tests that passed before now fail: every fare in MeterDown goes through round_fare. In chapter 10 Kaarigar learnt to roll that back, but the damage was done first and undone after, and the run paid for both.
Rukhsana knows the code. She would have said "not round_fare, it is shared" in five seconds, if anyone had asked her before the edit. Sneha wants the same thing for every issue that touches money: see what the agent means to do, say yes or no, and only then let it change files.
That is plan mode, and most coding agents have one: Claude Code's plan mode, Cursor's and Codex's ask-before-acting modes. The agent reads, writes a plan, and stops. A person reads the plan. Approved, the same conversation goes on and the agent does what it said. Rejected, the person's reason goes back and the agent plans again.
Kaarigar so far has no way to stop before it edits, and no way for a person's "no, because..." to reach it. And the obvious first try, telling it in the system prompt not to change files, does not work. You will measure that first.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
prompt_only.py: a rule in the prompt
Measure the first try at plan mode: a rule in the system prompt. Write a submit_plan Tool (one required string parameter, plan) that keeps each plan and returns "Received: a person will review it." Run run_agent on a checkout of MD-101 with issue_text("MD-101") and an Agent whose system prompt is SYSTEM + a blank line + PLAN_RULE, and whose tools are every tool from tools() plus submit_plan. Then print, in this order:
plans submitted: N (how many times submit_plan ran)
files changed: PATHS (the files in diff(root), from its +++ b/ lines, joined with ", "; none if none)
answer: ANSWER (the first line of run.answer)
- Task 2
plan_mode.py: plan, review, then act
Write plan_then_act(root, task, agent, review) -> Planned. The plan phase is run_agent with replace(agent, ...): the agent's system prompt + a blank line + PLAN_RULE, only the agent's tools whose names are in READ_ONLY plus a submit_plan tool, max_steps 12, and a before_step that returns "answered" once a plan is approved. submit_plan keeps the plan, calls review(plan), which gives (approved, feedback), and returns "Approved: go ahead." or "Rejected: " + the feedback (or "make a better plan." when it is empty). With no approval, return Planned(plans, False, plan_run). Else run the work phase: run_agent with the task "The plan is approved: carry it out." and replace(agent, history=...): the plan phase's messages without the first; return Planned(plans, True, plan_run, run).
- Task 3 · interview question
Interview: review.py, what is a review worth?
Run MD-107 twice with plan_then_act and Agent(SYSTEM, tools()), each in a fresh checkout. First "approve at once": approve every plan. Then "with feedback": reject the first plan with the feedback Rukhsana would give (what not to touch and why, and where to look), and approve the next. After each run print one line:
LABEL: MD-107 fixed, B broken, plans: N
(not fixed when it was not; B from grade(root, "MD-107").broken; N the plans submitted)
What you will learn
- Why a rule in the prompt is not a read-only mode, measured: the model edits anyway.
- Read-only by construction: the plan phase offers only tools that change nothing.
- A plan tool that hands the plan to a person and brings back their answer.
- Two phases, one conversation: the work phase goes on from the plan phase's messages.
- Feedback that changes the plan, and what an approved bad plan costs.
More in The loop that fixes
Build "Plan mode" yourself
MeterDown, a Pune app for auto-rickshaw fares, has eleven open issues. You build Kaarigar, an agent that fixes them without breaking tests, running risky commands or touching files outside the repository. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Build your own coding agent are free. This one comes with the all labs plan.