A ReAct agent (Module 4) chooses each action from what it can see at that moment. That makes it adaptable: if a search comes back empty, it tries another query. It also makes it short-sighted. Nothing in the loop asks what the whole task needs, or whether the work so far is on track. On a task with several parts, or one where a tool fails along the way, the agent tends to lose a part, repeat a failing call, or answer before it has everything. People who are good at long tasks work differently. They break the task down first, keep a list, and stop now and then to check it. This module gives an agent two tools for that. Neither tool does anything in the world. Each one writes text into the context that steers the steps after it. In the terms of Module 6, the model engineers part of its own context.

Notebooks

Notebook 7: Planning and reflection gives one multi-part expense question to four agents: ReAct alone, with a plan tool, with a reflection tool, and with both. One of the tools they share always fails. The notebook shows what each extra tool puts into the context and which failure it fixes. It also counts the extra steps and tokens.

Where ReAct breaks

The task in Notebook 7 is ordinary: “I am going to Oslo for three nights. What is the most I can claim, in US dollars, for lodging, the meal per diem and the airport train both ways?” It needs three handbook lookups, an exchange rate and a sum. The answer key is USD 772.38. One tool, a market exchange-rate service, is down, and its error message says “try again later”.

The ReAct agent searches once, with the whole question. The search returns two per diem passages and the ground-transport page, and no lodging page. That is ordinary retrieval behaviour (Module 5), not a staged failure. The agent then calls the rate service, which fails, and calls it again, which fails again. Its answer lists the per diem and the train in Norwegian krone and leaves out lodging without saying so. It looks like a complete answer, but most of the money is missing.

The book lists the typical ways ReAct goes wrong on long tasks:

  • it does too much in one step and then cannot use all the results;
  • it declares the task done while information is still missing;
  • it forgets what it found earlier and repeats work;
  • after a failure, it repeats the same action instead of looking for another way.

The run above shows the first, second and fourth. They share one cause: the agent never looks beyond the next step.

Plans

Definition 7.1 (Plan tool).

A plan tool lets the model write and rewrite a list of steps, each with a status: pending, in progress or completed. The harness stores the plan and shows the current version to the model on every later step, so the plan stays in view for the whole run.

The book’s plan tool, create_tasks, takes the whole list on every call and returns it as text, so the plan exists only in the history. The course’s PlanningTool keeps that interface under the name write_todos and changes where the plan lives. The harness keeps the list in the run’s state. Its process_llm_request hook adds the current plan to the instructions of every request, so the plan survives even if the call that wrote it is cut by a sliding window or compacted (Module 6). The tool description says when to use it (“tasks with several parts”) and when not to (“a question one tool call can answer”). A plan for a one-step question only costs tokens.

With a plan, the agent in Notebook 7 writes one step per part of the question and searches for each part on its own. All three amounts are found. In step 4 the model sees:

Your current plan (keep it up to date with write_todos):
[x] Find the Oslo lodging cap per night
[x] Find the Oslo meal per diem
[x] Find the Oslo airport train fare
[>] Get the exchange rate to US dollars with get_exchange_rate
[ ] Add everything up and convert to US dollars with the calculator

The plan tool (Definition 7.1) fixed the missing part, but not the broken tool. The agent retried the rate service, gave up and answered in krone. A plan says what to do. It does not notice that one way of doing it has stopped working.

A plan also has a cost that is easy to underestimate. Let be the plan’s size in tokens and the number of steps, and suppose the plan is rewritten times, at steps . The plan is shown on every step after the first rewrite. Each rewrite also puts about tokens into the history, because the call carries the list and its result echoes it. Those tokens are then re-sent on every later step. A rough count of our own (the book gives none) puts the extra input at about

and the second term dominates when the plan is rewritten often and early. In Notebook 7’s run with both tools, showing the plan in the instructions costs 515 tokens over the run. The write_todos calls and results in the history cost 4,134. Planning ahead also has a history of its own in the literature. Plan-and-Solve (Wang et al., 2023) asks the model to write a plan before answering. ReWOO (Xu et al., 2023) plans every tool call up front without waiting for observations. LLMCompiler (Kim et al., 2024) runs independent planned calls in parallel and replans when needed. CS329Z Lecture 6 notes that today much of this is done with parallel tool calls inside the loop. The notebook’s agent works in the same spirit: it sends each plan update in the same reply as the action it starts, so the plan adds tokens but almost no steps.

Reflection

Definition 7.2 (Reflection tool).

A reflection tool takes the model’s own assessment of where it stands: what has happened, what it means and what to do next, plus a flag need_replan. The assessment becomes part of the context for the next step. The flag tells the model to rewrite the plan.

The book suggests four moments to reflect: when a step of the plan is done, after a tool fails, when results from different sources must be combined, and before the final answer, to check that nothing is missing. It calls the last one the most important and the easiest to skip. The course’s tool description keeps the last three. The book also separates two kinds of trouble. If only the method for a step has to change, the plan can stay. If a premise of the plan was wrong, the plan has to be rewritten. In Notebook 7 the plan’s rate step names its method (“with get_exchange_rate”), so after the failure the step itself has to change:

[>] Get the exchange rate to US dollars with get_exchange_rate      (version 3)
[>] Find the exchange rate to US dollars in the handbook            (version 4)

The handbook’s currency page then shows that the rate service was the wrong source anyway: expense reports use a fixed quarterly rate of 10.50 NOK per dollar, not a live market rate. The agent computes USD 772.38, reflects once more as a self-check, and answers.

write_todos plan, shown each step act search, call, compute reflect on track? event log errors, plan progress on track: next step need_replan: rewrite the plan facts
Remark 7.1 (A reflection should be checked against the record).

The book’s reflection tool returns the model’s analysis as it is. The course’s ReflectionTool also reports what the harness knows for certain: how many plan steps are completed, which are not, and which tools have failed since the last reflection. In Notebook 7, an overconfident analysis (“Everything the question asks for is done”) on the plan-only agent’s context gets the reply “Plan: 3 of 5 steps completed”, followed by the two rate-service errors. The model sees the contradiction on its next step.

Four agents, one task

Notebook 7 runs every combination of the two tools. “Parts” counts the parts of the question that have an amount in the answer.

agentstepsfailed callspartsanswerinput tokens
ReAct422 of 3krone only2,716
plan523 of 3krone only7,627
reflect1013 of 3USD 772.3814,177
plan + reflect813 of 3USD 772.3818,312

Each tool fixes a different failure. The plan fixes the missing part in advance, by turning the question into a checklist. Reflection (Definition 7.2) fixes the broken tool. Without a plan, reflection also catches the missing part, but only after the fact. The reflect-only agent computes a total without lodging, finds the gap in its self-check, searches again and computes again. That is why it takes the most steps. With both tools, the agent takes two steps fewer but sends about 4,100 more tokens than with reflection alone, and equation accounts for almost all of the difference. These counts come from an offline model that acts its part (Remark 7.2).

Remark 7.2 (The offline model plays its part).

The offline policy model in Notebook 7 acts out these failure modes on purpose. It searches once when it has no plan, retries a failed tool once, and uses write_todos and reflect the way their descriptions ask. A live model shows each failure some of the time. Its plans can be too fine-grained or wrong, and its reflections can miss problems. Run the notebook with a live model several times before drawing conclusions about your own agent, and compare the runs with the same grader (Module 10).

What reflection cannot do

A reflection is more text from the model that made the mistake. With no outside signal, it can be confidently wrong. Huang et al. (2023) found that asking a model to review and correct its own reasoning, with no external feedback, often does not improve the answer, and sometimes makes it worse. The methods that work give the reflection something to check against. Reflexion (Shinn et al., 2023) turns test results or a task’s success or failure into a written lesson for the next attempt. CRITIC (Gou et al., 2024) checks a draft answer with tools, such as search or code execution. CS329Z Lecture 6 adds that reasoning models now do much of this checking inside their own reasoning. An explicit reflection tool earns its place where the harness can attach facts the model cannot see by introspection.

The harness can also go further than reporting. It can refuse a final answer while the plan has open steps. It can require a reflection after every tool error. It can check the answer with a separate grader before returning it. Each of these trades more steps for fewer silent failures. Which trade is right depends on what a wrong answer costs.

Try this

  • In Notebook 7, make get_exchange_rate work and return "10.80". Which answer does “plan + reflect” give, and does the handbook allow it? What would catch it: an instruction, a richer self-check, or a tool that knows the policy?
  • Make PlanningTool return only “Plan saved (n of m done)” instead of the whole plan. Which term of equation shrinks, and by how much does the token count of “plan + reflect” fall?
  • Write a before_llm callback that lists the open plan steps in the instructions and sets request.tool_choice = "required" while any step is open. With a live model, what does the plan-only agent do instead of giving up?
  • Give the overconfident claim (Remark 7.1) to ReflectionTool on the reflect-only agent’s context, which has no plan. What can the tool still report? What would the harness need to know to catch the missing lodging on its own?

Readings