Reflection & Self-Correction
Module 7 — Reflection & Self-Correction 🔍
Goal of this module: Learn how agents review and improve their own work — catching mistakes, critiquing their answers, and trying again. This is what separates a sloppy agent from a reliable one.
7.1 Why agents need to check their own work
LLMs make mistakes: wrong math, buggy code, missed steps, made-up facts. A basic agent just hands over its first answer, mistakes and all. A better agent does what a careful human does: it looks back at its work, asks “is this actually right?”, and fixes problems before finishing.
Definition: Reflection (or self-correction) is when an agent evaluates its own output, identifies flaws, and revises — without a human pointing out the error.
Analogy: Writing an essay, then proofreading it and fixing the typos before submitting. The proofreading pass is reflection.
7.2 The reflection loop
Reflection adds a review step to the agent loop:
1. GENERATE → produce an answer or take an action
2. REFLECT → critique it: "Is this correct? Complete? Any errors?"
3. REVISE → if flawed, fix it and try again
4. Repeat until the work passes review (or you hit a limit) → FINAL
Generate draft
│
▼
Reflect / critique ──── "looks good" ───► Final answer
│
"found a problem"
│
▼
Revise ──────────► (back to Reflect)
The key is that the agent generates a critique of its own output and then acts on that critique. It’s thinking about its thinking.
7.3 A concrete example
Task: “Write a function that returns the average of a list of numbers.”
GENERATE (draft):
def average(nums):
return sum(nums) / len(nums)
REFLECT (the agent critiques its own code):
"What if the list is empty? len(nums) would be 0,
causing a divide-by-zero crash. I should handle that."
REVISE:
def average(nums):
if not nums:
return 0 # handle empty list
return sum(nums) / len(nums)
REFLECT again:
"Now it handles empty lists. Looks correct and safe. Done."
The first draft had a hidden bug. Reflection caught it before the user ever saw it. That’s the value: catching mistakes early, automatically.
7.4 Ways to trigger reflection
There are several practical ways to make an agent reflect:
(a) Self-critique prompt
Simply ask the model to review its own answer:
"Here is your draft answer: [...].
Now critique it. Find any errors, missing pieces, or weak reasoning.
Then produce an improved version."
Surprisingly effective — the model is often better at spotting errors than avoiding them in the first place.
(b) Use real feedback (grounded reflection)
The best reflection uses actual results, not just opinion. Examples:
- Wrote code? Run it and read the error messages, then fix.
- Made a claim? Search to verify it.
- Did math? Recompute with a calculator tool.
Key insight: Reflection backed by real feedback (test results, tool outputs) is far more reliable than the model just second-guessing itself in the abstract. Whenever you can, give the agent a way to actually check.
(c) A separate “critic” role
Use the model in two roles: one generates, another critiques. Even though it’s the same underlying model, putting on a “critic hat” with a fresh prompt catches things the “writer” missed. (This naturally leads into multi-agent systems — Module 8.)
7.5 Named reflection techniques (for your vocabulary)
Experts reference a few specific patterns:
- Self-Refine: Generate → give yourself feedback → refine → repeat. The general loop above.
- Reflexion: After an attempt fails, the agent writes a short “lesson learned” note and stores it in memory, so it does better on the next try. (Notice this connects to Module 6 — reflection + memory = an agent that learns from mistakes.)
- Self-critique / Constitutional-style checks: The agent checks its output against a set of principles or rules (“Is this safe? Is this accurate? Is this on-topic?”) and fixes violations.
You don’t need to implement these formally as a beginner — but knowing the names helps you read the field and sound credible.
7.6 When reflection helps most (and when to skip it)
Reflection costs extra time and tokens (you’re running the model more). Use it wisely.
Reflection is worth it when:
- The task is complex (multi-step reasoning, coding, analysis).
- Accuracy matters a lot (financial, medical, legal-adjacent, production code).
- There’s a way to get real feedback (tests, tools, verification).
Skip or minimize reflection when:
- The task is simple (“What’s 2+2?” — no need to deliberate).
- Speed/cost matters more than perfection.
- The first answer is already easy to verify by other means.
Balance: Reflection is a quality dial. Turn it up for hard, high-stakes work; turn it down for quick, low-risk tasks.
7.7 The danger: reflection loops and over-thinking
Reflection can backfire:
| Problem | What happens | Fix |
|---|---|---|
| Endless revising | Agent keeps “improving” forever | Cap the number of reflection rounds (e.g., 2–3) |
| Second-guessing right answers | Agent talks itself out of a correct answer | Ground reflection in real feedback, not vibes |
| Confidently wrong critique | The critique itself is mistaken | Use tools/tests to verify, not just opinion |
| Cost blowup | Each reflection multiplies tokens | Only reflect on hard/important tasks |
The fix is almost always: limit the rounds and base reflection on real evidence (run the test, check the source) rather than the model’s gut feeling.
7.8 Reflection + the rest of the agent
Reflection ties together everything so far:
PLAN (M3) → REASON & ACT (M4, M5) → produce result
│
▼
REFLECT (M7): is it right?
│ │
"yes" ─┘ "no" → revise, maybe
re-plan (M3),
store lesson in
MEMORY (M6)
A great agent: plans, reasons and acts with tools, checks its own work, learns from mistakes, and only then delivers. Reflection is the quality-control inspector at the end of the line.
7.9 Check yourself ✅
- In one sentence, what is reflection?
- Why is reflection grounded in real feedback (like running a test) better than the model just re-reading its own answer?
- What is “Reflexion,” and which earlier module does it connect to?
- Name one risk of reflection and its fix.
Answers
- Reflection is when an agent evaluates its own output, finds flaws, and revises — without a human pointing them out.
- Because real feedback (test results, tool outputs) is objective evidence; the model second-guessing itself can be just as wrong as its first attempt.
- Reflexion is when an agent writes a “lesson learned” after a failure and stores it to do better next time — it connects to Module 6 (memory).
- Endless reflection loops / over-thinking → fix by capping the number of reflection rounds (and grounding critique in real feedback).
7.10 Summary
- Reflection / self-correction = the agent reviews its own work, finds errors, and revises before finishing.
- The loop is Generate → Reflect → Revise → repeat (with a cap).
- Trigger it via self-critique prompts, real feedback (run tests, verify facts), or a separate critic role.
- Named patterns: Self-Refine, Reflexion (learn from failures via memory), self-critique.
- Use reflection for hard, high-stakes tasks; skip it for simple ones. Always limit rounds and ground critique in real evidence to avoid loops and over-thinking.
Next up: Module 8 — Multi-Agent Systems. One agent is great, but what happens when you build a whole team? 👉 08_Multi_Agent_Systems.md