These are the notes for AI episode 31. Our agent loops, uses tools and remembers, but on a big job with many parts it rushes. You'll watch a step-by-step agent run out of money, then invent planning (decide the steps and limits first), replanning (fix the plan when a step fails) and reflection (a critic checks the result against the goal and writes feedback for another try). Every Python example runs as-is in the SyntaxPilot compiler; outputs and errors are real (Python 3.13). The planner, replanner and writer are toys standing in for LLM calls.
Real-life example: planning a family wedding
| At the wedding | In an AI agent |
|---|---|
| Mummy's checklist with a budget for each item | a plan: steps + limits |
| Tick, tick, tick | execute the plan step by step |
| "The caterer cancelled!" → call another one | replan when a step fails |
| The night before: "did we forget the garlands?" | reflect: check the result against the goal |
Problem 1: one step at a time runs out of money
BUDGET = 5000 # Asha's party: 20 people, Rs 5000
SHOP = {"cake": [2500, 1200, 600], "snacks": [2000, 1500, 900],
"decor": [1500, 800, 300], "invites": [400, 0]} # fanciest first
def next_step(need): # toy agent: one step at a time,
return SHOP[need][0] # it just grabs the best option
money = BUDGET
for need in ["cake", "snacks", "decor", "invites"]:
cost = next_step(need)
if cost > money:
raise RuntimeError(f"no money for {need}: need {cost}, have {money}")
money -= cost
print(f"{need:8} Rs {cost:5} left Rs {money}")
Output:
cake Rs 2500 left Rs 2500
snacks Rs 2000 left Rs 500
Traceback (most recent call last):
File "/tmp/tmpckk7fhe4/main.py", line 12, in <module>
raise RuntimeError(f"no money for {need}: need {cost}, have {money}")
RuntimeError: no money for decor: need 1500, have 500
Each step looked fine on its own, but the agent never looked at the whole goal.
The fix: plan first, then execute
BUDGET = 5000
SHOP = {"cake": [2500, 1200, 600], "snacks": [2000, 1500, 900],
"decor": [1500, 800, 300], "invites": [400, 0]}
def make_plan(budget): # think about the WHOLE job first
share = {"cake": 0.3, "snacks": 0.4, "decor": 0.2, "invites": 0.1}
return [(need, int(budget * part)) for need, part in share.items()]
plan = make_plan(BUDGET)
print("PLAN:", plan)
money = BUDGET
for need, limit in plan: # then carry it out, step by step
cost = max(p for p in SHOP[need] if p <= limit)
money -= cost
print(f"{need:8} Rs {cost:5} (limit {limit}) left Rs {money}")
Output:
PLAN: [('cake', 1500), ('snacks', 2000), ('decor', 1000), ('invites', 500)]
cake Rs 1200 (limit 1500) left Rs 3800
snacks Rs 2000 (limit 2000) left Rs 1800
decor Rs 800 (limit 1000) left Rs 1000
invites Rs 400 (limit 500) left Rs 600
Planning = deciding the steps for the whole goal (and limits like budget) before acting. In a real agent the LLM writes the plan and your code executes it: plan-and-execute.
Plan-and-Solve prompting (Wang et al., 2023)
"Let's first understand the problem and devise a plan to solve the problem. Then, let's carry out the plan and solve the problem step by step."
The paper found that plain step-by-step reasoning (Zero-shot-CoT) suffers from calculation errors, missing-step errors and semantic misunderstanding; writing a plan first targets the missing steps.
Check the plan before acting
import json
GOAL_ITEMS = ["cake", "snacks", "decor", "invites"]
def llm_plan(prompt): # toy LLM: returns a plan as JSON
if "Check that" in prompt:
return '["cake", "snacks", "decor", "invites"]'
return '["cake", "snacks", "decor"]' # forgot a step!
prompt = "Plan Asha's party. Reply with a JSON list of steps."
for attempt in range(1, 3):
plan = json.loads(llm_plan(prompt)) # the plan is DATA
missing = [g for g in GOAL_ITEMS if g not in plan]
print(f"plan {attempt}: {plan} missing: {missing}")
if not missing: break
prompt += f" Check that it includes: {', '.join(GOAL_ITEMS)}."
Output:
plan 1: ['cake', 'snacks', 'decor'] missing: ['invites']
plan 2: ['cake', 'snacks', 'decor', 'invites'] missing: []
Asking for the plan as JSON makes it data your code can check: a missing step is caught before a rupee is spent.
Problem 2: a blind plan ignores failures
def run(step):
if "Sweet Bakery" in step:
return "ERROR: Sweet Bakery is closed today"
return "done"
plan = ["order cake at Sweet Bakery", "buy snacks",
"put up decorations", "send invites"]
for step in plan: # follow the plan blindly
print(f"{step:28} -> {run(step)}")
print("Party ready! ...but is there a cake?")
Output:
order cake at Sweet Bakery -> ERROR: Sweet Bakery is closed today
buy snacks -> done
put up decorations -> done
send invites -> done
Party ready! ...but is there a cake?
A plan is made before you know what will happen. If results are never checked, one failed step quietly ruins the goal.
The fix: observe every step, replan on failure
def run(step): # the real world: things go wrong
if "Sweet Bakery" in step:
return "ERROR: Sweet Bakery is closed today"
return "done"
def replan(step, error): # toy: a real agent asks the LLM
return step.replace("Sweet Bakery", "Royal Bakers")
plan = ["order cake at Sweet Bakery", "buy snacks",
"put up decorations", "send invites"]
i = 0
while i < len(plan):
result = run(plan[i])
print(f"{plan[i]:28} -> {result}")
if result.startswith("ERROR"):
plan.insert(i + 1, replan(plan[i], result)) # fix the plan
print(" replanned:", plan[i + 1])
i += 1
Output:
order cake at Sweet Bakery -> ERROR: Sweet Bakery is closed today
replanned: order cake at Royal Bakers
order cake at Royal Bakers -> done
buy snacks -> done
put up decorations -> done
send invites -> done
A real agent sends the error back to the LLM and asks for an updated plan. It's the agent loop from episode 27, steered by a plan.
ReAct vs plan-then-execute
| ReAct (episode 27) | Plan, then execute | |
|---|---|---|
| How | Thought → Action → Observation, one step at a time | write the whole list, then run it |
| Strength | flexible, reacts fast | keeps the goal and limits in view |
| Weakness | can lose sight of the whole goal | the plan can be wrong: needs replanning |
| Use for | short, unclear tasks | long, many-part tasks with budgets |
Problem 3 and the fix: reflection
GOAL = {"date": "8 Nov", "place": "MG Road", "RSVP": "RSVP"}
def write_invite(feedback=None): # toy LLM writer
if feedback is None:
return "Come to Asha's party on Sunday at 7 pm!"
return "Asha's party: Sunday 8 Nov, 7 pm, 12 MG Road. RSVP to Ravi."
def check(text): # the critic: test it against the goal
return [part for part, must in GOAL.items() if must not in text]
feedback = None
for attempt in range(1, 4): # at most 3 tries
draft = write_invite(feedback)
missing = check(draft)
print(f"try {attempt}: {draft}")
if not missing:
print(" check: all good, send it!"); break
feedback = f"Missing: {', '.join(missing)}. Add them."
print(" reflection:", feedback)
Output:
try 1: Come to Asha's party on Sunday at 7 pm!
reflection: Missing: date, place, RSVP. Add them.
try 2: Asha's party: Sunday 8 Nov, 7 pm, 12 MG Road. RSVP to Ravi.
check: all good, send it!
Reflection = look back at your own output, say in words what's wrong, and use that to do better. Always cap the retries.
Try ──► Feedback (tests, checks) ──► Reflect ("I forgot...") ──► Retry with the note
Know when to stop
GOAL = {"date": "8 Nov", "place": "MG Road", "RSVP": "RSVP"}
def write_invite(feedback=None): # toy writer that never learns the place
return "Asha's party: Sunday 8 Nov, 7 pm. RSVP to Ravi."
def check(text):
return [part for part, must in GOAL.items() if must not in text]
MAX_TRIES = 3
for attempt in range(1, MAX_TRIES + 1):
missing = check(write_invite())
print(f"try {attempt}: missing {missing}")
if not missing: break
else: # no break: every try failed
print(f"Stopped after {MAX_TRIES} tries. Ask a human: where is it?")
Output:
try 1: missing ['place']
try 2: missing ['place']
try 3: missing ['place']
Stopped after 3 tries. Ask a human: where is it?
Python's for ... else runs the else only when the loop never hit break. Cap the retries and hand over to a human with a clear question.
Reflexion (Shinn et al., March 2023)
- Reinforces agents "not by updating weights, but instead through linguistic feedback".
- Agents "verbally reflect on task feedback signals, then maintain their own reflective text in an episodic memory buffer" (episode 30).
- "Reflexion achieves a 91% pass@1 accuracy on the HumanEval coding benchmark, surpassing the previous state-of-the-art GPT-4 that achieves 80%."
A critic needs evidence
A lazy critic
def write_invite():
return "Come to Asha's party on Sunday at 7 pm!"
def self_grade(text): # the SAME model asked "is this good?"
return "Looks great! Friendly and clear."
draft = write_invite()
print("draft :", draft)
print("critic:", self_grade(draft))
print("sent to 20 guests. Which Sunday? Where?")
Output:
draft : Come to Asha's party on Sunday at 7 pm!
critic: Looks great! Friendly and clear.
sent to 20 guests. Which Sunday? Where?
| Self-grading ("Looks great!") | Grounded check |
|---|---|
| the same model judges itself | run the tests, add up the bill, check every goal item |
| same blind spots as the writer | feedback from the real world |
| the invite goes out with no date | the missing date is caught |
Planning and reflection aren't free
Every plan, replan and critique is another model call: more tokens, more money, more waiting. Answer simple questions directly; save planning and reflection for long, costly, many-part jobs.
Glossary
| Term | Meaning | Example from this episode |
|---|---|---|
| Greedy step | choosing each step on its own, the best-looking option | the Rs 2500 cake |
| Plan | the list of steps (and limits) for the whole goal | cake 1500, snacks 2000, ... |
| Subtask | one step of a plan | "order cake" |
| Plan-and-execute | the LLM writes a plan; code carries it out | make_plan then the loop |
| Plan-and-Solve prompting | a prompt that asks for a plan before solving | "devise a plan..." |
| Missing-step error | a reasoning step that got skipped | forgetting the invites |
| Executor | the part that runs each step | run(step) |
| Replanning | changing the plan after a step fails | Royal Bakers |
| ReAct | interleaved thought, action, observation (episode 27) | one step at a time |
| Reflection | critiquing your own output in words, then retrying | "Missing: date, place, RSVP" |
| Critic | the checker that compares output with the goal | check(text) |
| Feedback | what the critic says is wrong | feedback |
| Reflexion | keeping written reflections in episodic memory and retrying | 91% on HumanEval |
| pass@1 | share of problems solved on the first submitted answer | 91% |
| Retry limit | the maximum number of tries | range(1, 4) |
| Grounded check | a check against real evidence | tests, totals |
Common mistakes
| Mistake | What you see | Why | Fix |
|---|---|---|---|
| No plan | RuntimeError: no money for decor: need 1500, have 500 |
no view of the whole goal | plan steps + limits first |
| Following the plan blindly | Party ready! ...but is there a cake? |
results never checked | observe each step; replan on errors |
| Self-grading critic | invite sent with no date | no evidence | check against the goal / tests |
| No retry limit | endless loop, big bill | the critic is never satisfied | stop after N tries; ask a human |
Mini project: a party-planning agent
Combine everything: make_plan(goal, budget), an executor that observes each step, a replanner for failures, and a critic that checks the total bill and the guest count. Stop after 3 tries and hand over to a human.
Practice
1. Plan a party for 30 people with Rs 6000, make the decor shop fail, replan, and let the critic check the real total.
Solution
SHOP = {"cake": [2500, 1200, 600], "snacks": [3000, 2000, 1500, 900],
"decor": [1500, 800, 300], "invites": [400, 0]}
def make_plan(budget):
share = {"cake": 0.25, "snacks": 0.5, "decor": 0.15, "invites": 0.1}
return [(need, int(budget * part)) for need, part in share.items()]
def run(need, limit):
if need == "decor":
return None, "ERROR: decor shop closed"
return max(p for p in SHOP[need] if p <= limit), "done"
budget = 6000 # 30 people
money, bill = budget, []
for need, limit in make_plan(budget):
cost, status = run(need, limit)
if cost is None:
print(f"{need}: {status} -> replan: make paper decorations, Rs 0")
cost = 0
money -= cost
bill.append(cost)
total = sum(bill) # the critic checks the real total
print("bill:", bill, "total:", total)
print("check:", "OK" if total <= budget else f"OVER by {total - budget}")
Output:
decor: ERROR: decor shop closed -> replan: make paper decorations, Rs 0
bill: [1200, 3000, 0, 400] total: 4600
check: OK
2. Why is "Looks great!" from the same model a weak check?
Answer
The critic shares the writer's blind spots and has no evidence. Use checks against something real: tests, totals, a checklist from the goal.
3. When would you prefer ReAct over plan-then-execute?
Answer
For short tasks where the next step depends heavily on what you just saw (e.g. searching for one fact). For long tasks with many parts and limits, plan first.
Quick quiz 1
The first step failed: the hall is full on 8 Nov. What should the agent do?
Answer
B: replan: try another hall or date, then continue.
Quick quiz 2
How does a Reflexion agent improve between tries?
Answer
B: it keeps written reflections in an episodic memory buffer and retries; no weights change.
Interview questions
- Why do agents need planning? Choosing each step locally can break global constraints (budget, missing steps); a plan keeps the whole goal in view.
- Plan-and-execute vs ReAct? Plan-and-execute writes the full plan first and replans on failures; ReAct decides one step at a time from the latest observation.
- What is replanning? Updating the remaining plan after an observation shows a step failed or the world changed.
- What is reflection in LLM agents? The agent critiques its own output in natural language and uses that feedback on its next attempt.
- What is Reflexion? A framework where agents reflect on feedback signals and store the reflections in episodic memory, improving without weight updates (91% pass@1 on HumanEval vs GPT-4's 80% in the paper).
- How do you keep reflection loops safe? Ground the critic in real checks, cap the retries, and escalate to a human.
Summary
- One step at a time can break the whole goal: plan first.
- Plan-and-Solve: devise a plan, then carry it out step by step.
- Plans are guesses: observe each step and replan on failure.
- Reflection: a critic writes feedback; the agent retries.
- Reflexion stores reflections in episodic memory: 91% on HumanEval.
- Give critics real evidence and a retry limit.
Next: episode 32, Build your first agent in Python: loop, tools, memory and planning in one script.
Sources: Wang et al., "Plan-and-Solve Prompting: Improving Zero-Shot Chain-of-Thought Reasoning by Large Language Models", arXiv 2305.04091 (2023); Shinn et al., "Reflexion: Language Agents with Verbal Reinforcement Learning", arXiv 2303.11366 (2023).