Week 13 • Day 3Week 13
Day 3:Week 13, Day 3: `Plan-and-Execute` vs `Reflexion` — Self-Correction from Tool Error Tracebacks
Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz
Week 13, Day 3: Plan-and-Execute vs Reflexion — Self-Correction from Tool Error Tracebacks
1. Why Pure ReAct Struggles on Long 10-Step Tasks
ReAct is a greedy, one-step-at-a-time local search: at Step 1, it hasn't planned Steps 2–6.
For complex engineering workflows (e.g., "Audit all 3 database replicas, compare replication lag, promote the healthiest replica, and update the connection pooler"), Plan-and-Execute (Wang et al., 2023) separates Strategic Planning from Tactical Execution:
- Planner Model (Frontier Reasoning Model): Emits a structured DAG / checklist of sub-tasks
[Step 1, Step 2, Step 3]. - Executor Model (Fast, Cheap Model): Executes each step's tool call.
- Re-Planner: Updates the remaining steps if Step 2 discovers an unexpected state.
2. Reflexion (Shinn et al., 2023): Turning Tool Errors into Self-Correction
When an agent generates a SQL query, a Kubernetes command, or a tool call that fails validation, naive pipelines abort immediately.
In Reflexion, we feed the structured diagnostic failure (Error Type + Violated Constraint + Hint) back into a bounded Self-Reflection & Repair Loop (max_retries = 2):
code
Attempt 1: Agent calls query_metrics(window="last_year", metric="cpu")
│
▼ Tool Validator Rejects!
Observation: "ValidationError: 'window' must be one of ['5m', '1h', '24h']. Got 'last_year'."
│
▼ Reflexion Critique & Repair Step
Reflection: "I passed 'last_year' which violates the allowed window enum ['5m', '1h', '24h']. I will retry with '24h'."
Attempt 2: Agent calls query_metrics(window="24h", metric="cpu") ──► SUCCESS!
In Simple English: Sketching the Route First (Plan-and-Execute) vs Learning From Mistakes (Reflexion)
- ReAct alone turns one street corner at a time without reading the full map — easy to wander in circles on 10-step tasks.
- Plan-and-Execute: Writes out Steps 1 -> 2 -> 3 -> 4 up front ("1. Check disk, 2. Inspect DB locks, 3. Verify connections, 4. Summarize") so the agent stays focused on the goal.
- Reflexion (Self-Healing From Errors): When a SQL query throws
SyntaxError: near "FRO", instead of giving up or repeating the broken SQL, the agent reads the error traceback, reflects ("Oops, I misspelled FROM"), fixes the query, and retries cleanly!