Planning · Free preview

Reflect and Retry

Notice failure, revise

Self-reflection turns a failed step into a repair: the agent checks each action's result, and on failure inserts a fix step and retries instead of blindly marching on.

Notice failure, revise

The plan-then-act agent from the last lesson had a quiet assumption baked in: that every action in the plan succeeds. The real world doesn't honor that. A step that ran fine in rehearsal fails in production — the file is locked, the editor isn't focused, the API returns a 429. An agent that marches straight down its plan ignoring results ends up in a corrupt state: it runs save over a document it never managed to write, then cheerfully reports success. The plan completed; the work didn't.

Self-reflection closes that gap. The shape is simple but the discipline is the point: after every action, read the result. On success, advance. On failure, don't barrel onward — pause, run the repair, then retry the same step. The agent treats the checker's verdict as live feedback rather than a formality, so a dead end becomes a detour instead of a crash.

Here the plan is ["open","write","save"], but checker rejects write until the editor has been focused — exactly the dependency a static plan can miss. The fixes map says the repair for write is focus. So the run should go: open ok, write FAIL → undo the failed attempt, run focus, retry write (now ok), save ok. Reflection discovers at runtime the prerequisite the plan never encoded.

The starter just loops run(step) and plows past the failure. Add the reflection: when run(step) returns "fail", pop the bad attempt off state, print step failed -> inserting fix -> retry, run fixes[step], then run step again. Done means a clean final state of open,focus,write,save.

A plan you can revise mid-flight beats a perfect plan you can't — the checker is what turns a dead end into a detour.

In the full academy, you write and run this — live, graded:

// A plan of steps to execute in order.
const plan = ["open", "write", "save"];

// Steps already done, in order. (Your record of the run.)
const state = [];

// The environment checks each step AFTER you run it.
// "write" only sticks once the editor has been focused first.
function checker(step, state) {
  if (step === "write" && !state.includes("focus")) return "fail";
  return "ok";
}

// A repair for a step that fails: run this first, then retry the step.
const fixes = { write: "focus" };

// Run one step: record it in state and report the check result.
function run(step) {
  state.push(step);
  const result = checker(step, state);
  console.log(`run ${step} -> ${result === "ok" ? "ok" : "FAIL"}`);
  return result;
}

// TODO: execute the plan WITH self-reflection.
// On a "fail", undo this step from state, print
//   "step failed -> inserting fix -> retry"
// then run fixes[step], 

🔒 Live code execution, real agent runs, mastery tracking and verifiable credentials unlock with the full academy.

This is 1 of 50 lessons.

The full academy: write real code, watch real agents run, and earn verifiable credentials — across 8 tracks, in a 3D campus.

Unlock the full academy — $100 →

14-day refund · 🔒 Stripe-secured checkout · lifetime access

More free lessons: An LLM Is a Function  ·  The Agent Loop  ·  Define a Tool  ·  Give an Agent a Tool  ·  Durable State

← The Agent Marketplace