Master

Reasoning & planning

Good agents plan before they act, then revise when something breaks. Forcing a plan onto paper, then reflecting on the result, is what separates a fragile script from a robust one.

What you will see

Breaking goals into subgoals Decomposition turns an overwhelming task into manageable steps. Decomposed 'launch product' into: ['build landing page', 'open signups', 'notify users'] Solving each small piece solves the whole.

About 2 min of reading

Try It

Plan before acting, then reflect and revise

1

Break a goal into steps before running anything.

print() print("Reasoning & planning") print("Forcing a plan onto paper before acting improves hard tasks.") def model_break_down_goal(goal): return [s.strip() for s in goal.split(",")] goal = "search docs, write code, run tests" plan_steps = model_break_down_goal(goal) print("Goal: %s" % goal) print("Steps: %s" % plan_steps) print("Writing steps down lets the plan check its own logic.")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Reasoning & planning Forcing a plan onto paper before acting improves hard tasks. Goal: search docs, write code, run tests Steps: ['search docs', 'write code', 'run tests'] Writing steps down lets the plan check its own logic.
Forcing a plan before acting dramatically improves results on hard tasks.
2

Run one ReAct cycle: think, act, observe.

print() print("ReAct loop") print("Interleave Thought, Action, Observation to approach the goal.") def call_tool(action): return {"get_order": "shipped"}[action] thought = "I need the order status." action = "get_order" observation = call_tool(action) print("Thought: %s" % thought) print("Action: %s" % action) print("Observation: %s" % observation) print("Now email the tracking.")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
ReAct loop Interleave Thought, Action, Observation to approach the goal. Thought: I need the order status. Action: get_order Observation: shipped Now email the tracking.
3

Critique the result, then fix the flaw.

print() print("Reflection and self-critique") print("Generate a solution, critique it, then fix it.") solution = "return x + 1" critique = "off-by-one in base case" fixed = "return x + 1 if n > 0 else 0" print("First attempt: %s" % solution) print("Critique: %s" % critique) print("Fix: %s" % fixed) print("The critique raises quality without any extra tools.")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Reflection and self-critique Generate a solution, critique it, then fix it. First attempt: return x + 1 Critique: off-by-one in base case Fix: return x + 1 if n > 0 else 0 The critique raises quality without any extra tools.
4

Decompose a big goal into smaller subgoals.

print() print("Breaking goals into subgoals") print("Decomposition turns an overwhelming task into manageable steps.") def break_down(goal): return { "launch product": ["build landing page", "open signups", "notify users"], }.get(goal, [goal]) def plan(goal): sub = break_down(goal) if len(sub) == 1: return [goal] return [plan(g) for g in sub] def flatten(items): out = [] for it in items: if isinstance(it, list): out.extend(flatten(it)) else: out.append(it) return out big_goal = "launch product" print("Decomposed '%s' into: %s" % (big_goal, flatten(plan(big_goal)))) print("Solving each small piece solves the whole.")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Breaking goals into subgoals Decomposition turns an overwhelming task into manageable steps. Decomposed 'launch product' into: ['build landing page', 'open signups', 'notify users'] Solving each small piece solves the whole.

Data Flow

The agent plans, acts, observes, and reflects, adjusting its plan until the goal succeeds.
1 Generate a step plan
↓
2 Run the ReAct loop: think, act, observe
↓
3 Reflect on the outcome and find flaws
↓
4 Fix and re-run until satisfied
Why this works
ReAct: reason then act

The ReAct pattern interleaves reasoning and action:

Thought: "I need the order status."
Action: call get_order()
Observation: "shipped"
Thought: "Now email the tracking."

Each Thought-Action-Observation cycle moves the agent closer. It is the backbone of modern agents.

Reflection and self-critique

Add a second pass where the agent reviews its own work:

first = model.generate(solution)
critique = model.generate(
    "Find bugs in:", solution=first
)
final = model.generate(
    "Fix:", solution=first, feedback=critique
)

Generate -> critique -> fix. The critique is worth its weight in gold.

Self-critique consistently raises quality without extra tools.

Examples

Chain of thought planning

Ask the agent to plan first

plan = model.generate(
    "Break this goal into steps:", goal,
    thinking=True
)
# ["search docs", "write code", "run tests"]

Forcing a plan before acting dramatically improves results on hard tasks.

Why it works: writing steps down makes the model check its own logic.

Recovering from failure

When a step fails, retry differently

for attempt in range(3):
    try:
        return run(step)
    except Exception as e:
        plan.adjust(step, str(e))

A good agent doesn't just repeat, it changes approach after a failure.

Fail -> understand why -> try something new

Breaking goals into subgoals

Break goals into subgoals

def plan(goal):
    if easy(goal):
        return [goal]
    subgoals = break_down(goal)
    return [plan(g) for g in subgoals]

Big goals break into subgoals, which break further. Solving each small piece solves the whole.

Why it works: decomposition turns an overwhelming task into manageable steps.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Multi-agent systems
Previous Lesson
Multi-agent systems
Next Lesson
Evaluating agents
Evaluating agents →