Skip to content
tezvyn:
Menu

Reflection: Teaching LLM Agents to Learn from Mistakes

Source: promptingguide.aiMediumHow cards are made

Reflection gives an agent an "inner monologue" to learn from its mistakes. An Actor model attempts a task, an Evaluator scores it, and a Self-Reflection model generates linguistic feedback for the next try.

Why it exists

Standard agents often fail repeatedly on complex tasks because they lack a mechanism to analyze why they failed. They can get a low score but don't understand the root cause. Reflection was created to give agents a structured way to learn from their own history and iteratively improve their strategy.

The mental model

Imagine an agent talking to itself after a failure. It's not just getting a "wrong" score; it's generating a sentence explaining what was wrong. An Actor tries to solve a puzzle, the Evaluator says "You failed," and the Self-Reflection model says, "You failed because you tried the key on the wrong door. Next time, check all doors first." This advice is then remembered for the next attempt.

How it works

The process uses three distinct models. First, an Actor, often a Chain-of-Thought or ReAct agent, attempts a task and generates a trajectory of actions. Second, an Evaluator model scores this trajectory, providing a reward signal (e.g., a number or pass/fail). Third, a Self-Reflection model takes the trajectory and score as input and generates natural language feedback. This linguistic feedback is stored in the agent's long-term memory and added to the context for the next attempt, guiding the Actor toward a better solution.

When to use it

Use Reflexion for multi-step tasks where an agent can get it wrong on the first try and iterative improvement is valuable. This is common in complex decision-making, programming, and advanced reasoning problems where the solution path isn't obvious. It helps the agent avoid repeating prior mistakes by explicitly learning from them.

When not to use it

For simple, single-shot tasks where a correct answer can be found immediately, Reflexion is overkill. The overhead of the Actor-Evaluator-Reflector loop adds latency and cost. If the task doesn't benefit from iterative refinement, a simpler agent framework is more efficient.

One canonical example

An agent is tasked with writing a Python function. On its first try, the Actor generates code with a bug. The Evaluator runs the code against unit tests, which fail, providing a low score. The Self-Reflection model observes the failed test and buggy code, then generates the feedback: "The function failed because of an off-by-one error in the loop. The loop should iterate to n instead of n-1." This reflection is passed back to the Actor, which then generates corrected code in its next attempt.

Interview question

What unique capability does the Self-Reflection model provide in a Reflexion agent's learning process?

  • a.It allows the agent to immediately correct errors within a single task attempt.
  • b.It generates specific, natural language explanations for failure, guiding future attempts.Correct
  • c.It determines the optimal reward function for the Evaluator model.
  • d.It directly fine-tunes the Actor model's parameters based on failure.
Why?

The Self-Reflection model's core function is to generate linguistic feedback explaining the root cause of failure, which then informs the Actor's next attempt. It does not directly fine-tune the Actor's parameters; instead, it provides contextual guidance.

Just read this? Test yourself on what you have been reading.

Read the original → promptingguide.ai

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on llm — each one lists the topics its interview covers.

See open roles