Lesson 8New

Reflexion — Self-Correcting LLM Outputs

Self-correction through reflection

The Problem: AI makes mistakes but doesn't learn from them within a conversation. How can we make AI reflect on its errors and improve?

The Solution: Learn From Failure

Reflexion is a technique where AI analyzes its own mistakes, generates feedback, and uses that insight to improve. It's like a student reviewing wrong answers after a test to do better next time. It goes beyond Chain-of-Thought by adding a self-critique loop, similar to how Chain of Verification checks for errors.

Think of it like analyzing your mistakes after a test:

  • 1. Attempt: Try to solve the problem
  • 2. Evaluate: Check if the answer is correct
  • 3. Reflect: "I made an error because I forgot to consider..."
  • 4. Retry: Apply the lesson and try again

Where Is This Used?

  • Code Debugging: AI fixes its own buggy code after testing
  • Reasoning Tasks: Correcting logical errors in multi-step problems
  • Learning Agents: Agents that improve over multiple interactions
  • Quality Improvement: Iteratively refining outputs based on feedback

Fun Fact: Reflexion agents can solve coding challenges that require multiple attempts. In benchmarks, reflexion improved pass rates from around 30% to over 90% by learning from test failures!

Try It Yourself!

Use the interactive example below to see how Reflexion helps AI learn from mistakes and progressively improve its answers.

Frequently asked questions

What is Reflexion in LLMs?

Reflexion is a framework where an LLM agent evaluates its own output, generates verbal self-criticism, and uses that feedback to improve subsequent attempts — achieving better results through iterative self-correction.

How does Reflexion differ from Chain-of-Thought?

Chain-of-Thought prompts the model to reason step-by-step in a single pass. Reflexion adds a feedback loop: the model generates an answer, reflects on errors, and retries with self-generated insights — making it multi-turn and self-improving.

When should you use Reflexion agents?

Reflexion works best for tasks where initial attempts are often imperfect — code generation, complex reasoning, and multi-step problem solving. It's especially effective when you can automatically verify outputs (e.g., running tests on generated code).

Try it yourself

Interactive demo of this technique

Technique Comparison
Demo Mode
Pre-recorded responses
TaskIntermediateMath

Calculate the area of a triangle with sides 5, 12, and 13.

Without technique
Without technique
Prompt
Calculate the area of a triangle with sides 5, 12, and 13.
Response

Using Heron's formula: p = (5+12+13)/2 = 15 S = √(15·10·3·2) = √900 = 30

Tokens:28/45
Time:380ms
Quality:
With Reflexion
With technique
Prompt
Calculate the area of a triangle with sides 5, 12, and 13. After solving: 1. Verify your answer using a different method 2. If you find an error — fix it 3. Explain why the chosen method is optimal
Response

Method 1 (Heron): p = (5+12+13)/2 = 15 S = √(15·10·3·2) = √900 = 30

🔍 VERIFICATION: Notice: 5² + 12² = 25 + 144 = 169 = 13² This is a right triangle! (Pythagorean theorem)

Method 2 (simpler): S = (1/2) · leg₁ · leg₂ = (1/2) · 5 · 12 = 30 ✓

✅ REFLECTION: Both methods gave 30 — answer is correct. But method 2 is simpler! Should have first checked if it's a right triangle.

Answer: 30 sq. units (right triangle, optimal method — half product of legs)

🔢Heron method solution: S = 30
🔍Check: 5² + 12² = 13² — right triangle!
🔍Simpler: S = ½ · 5 · 12 = 30 ✓
✏️Reflection: should have checked type first
Tokens:65/185
Time:780ms
Quality:
Why this works

Reflexion not only verifies the answer but evaluates method efficiency. The model found a simpler solution path.

1 / 4

Lesson Quiz

1 of 3

1.What is the key innovation of the Reflexion technique?

Practice Challenges

Create a free account to solve challenges

3 AI-verified challenges for this lesson

This lesson is part of a structured LLM course.

My Learning Path