Advanced about 5 h
The picture
01
Plan
ordered steps
02
Execute
one step at a time
03
Verify
check the result
04
Replan
on failure
05
Critique + revise
bounded rounds
Why plan at all
A ReAct loop decides one step at a time, so on long tasks it can wander. A plan gives structure, makes progress visible, lets you review before acting, and lets cheaper models execute steps that a stronger one designed.
Plan-and-execute with replanning
import json
def make_plan(goal: str) -> list[str]:
out = llm(f"Break this goal into 3 to 7 concrete, ordered steps that each use at most one tool. "
f"Return a JSON list of strings only.\n\nGoal: {goal}", 600)
return json.loads(out)
def run_plan(goal, executor, max_replans=2):
plan, done, replans = make_plan(goal), [], 0
while plan:
step = plan.pop(0)
res = executor.run(f"Goal: {goal}\nCompleted so far: {done}\nCurrent step: {step}")
if res.stop != "done":
if replans >= max_replans:
return {"status": "failed", "done": done, "failed_step": step}
replans += 1
plan = json.loads(llm(f"Goal: {goal}\nDone: {done}\nFailed step: {step} ({res.stop}).\n"
f"Return a revised JSON list of remaining steps.", 600))
continue
done.append({"step": step, "result": res.answer})
return {"status": "ok", "done": done}
Note the bounded replans. Without a cap, planners thrash.
Reflection: critique and revise
def critique_and_revise(task, draft, rounds=2, check=None):
for _ in range(rounds):
feedback = check(draft) if check else llm(f"Task: {task}\n\nDraft:\n{draft}\n\nList concrete defects. If none, say OK.", 400)
if feedback.strip().upper().startswith("OK"):
break
draft = llm(f"Task: {task}\n\nDraft:\n{draft}\n\nDefects:\n{feedback}\n\nRewrite fixing every defect.", 1200)
return draft
The crucial parameter is check. Reflection helps when feedback comes from outside the model: unit tests, a compiler, a schema validator, a database lookup, a calculator. When the model only critiques itself on a reasoning task it often cannot find its own errors, and it may "fix" correct answers into wrong ones. Prefer objective checks; treat pure self-critique as something to measure, not assume.
Searching over reasoning
Tree-of-Thoughts-style methods generate several candidate next steps, score them and expand the best, trading many more model calls for better results on puzzles and planning tasks. Reasoning-trained models now do a version of this internally, so the explicit scaffolding is often unnecessary. Reach for it only where a verifier exists and a single pass fails.
def best_of_n(task, n=5, verify=None):
candidates = [llm(task, 800) for _ in range(n)]
if verify:
passing = [c for c in candidates if verify(c)]
return passing[0] if passing else None
return max(candidates, key=lambda c: score(task, c)) # a judge: weaker than a real verifier
Choosing
- Short tasks: plain ReAct.
- Long, decomposable tasks: plan-and-execute with replanning and a budget.
- Output quality tasks with an objective checker: reflection or best-of-n against the checker.
- Hard puzzles with a scoring function: search.
Common mistakes
- Unbounded replanning.
- A critic with no new information, asked to find faults in its own output.
- Paying for five-way sampling when one pass with a verifier would do.
Exercises
- Implement best-of-5 for a coding task using unit tests as the verifier and compare pass rate to a single sample.
- Show a case where self-critique without a checker makes a correct answer worse.
You are done when you can…
- Implement plan-and-execute with a replanning trigger and a step budget
- Build a critique-and-revise loop with an objective stopping rule
- Explain why self-critique without external feedback is unreliable