Özer Cem KELAHMETKELAHMET
AI Simulations · Agents

Reflexion simulation: how self-written lessons raise an agent's success over retries

A plain retry of a failed agent task usually fails the same way. Reflexion makes the agent write down what went wrong and read it on the next attempt. Change the memory size, the strength of a lesson and the number of trials, and watch success climb trial by trial.

Download Markdown

Interactive simulation. It needs JavaScript; the text below explains what it shows.

What you are looking at

Each row is one household task in the style of ALFWorld, the text game the Reflexion paper used for decision making: put a clean mug in the coffee machine, heat an egg, find two pencils and put them in a drawer. The column p_first is the chance that a ReAct-style agent solves the task on its first trial. The 24 tasks average 69%, close to the 70.9% the ReAct repository reports for PaLM-540B on ALFWorld; the clean and pick-two tasks were made the hardest.

The simulation compares two ways of using more trials. Plain retries run the agent again from scratch. Reflexion retries add the agent's own written lessons from earlier failures to the prompt. The chart puts every task on the model's curves: the dashed diagonal is one trial, the lower curve is plain retries and the upper curve is Reflexion after T trials. The bars show how many points each task gains from the lessons, and the iterate block runs the trials one at a time. For the loop inside a single trial, see the ReAct simulation and the ReAct loop page.

The algorithm

Reflexion has three parts: an actor that acts (here a ReAct agent), an evaluator that says whether the trial succeeded, and a self-reflection model that turns a failed trajectory into a few sentences of advice. The advice goes into an episodic memory that is bounded to the last Omega reflections, 1 to 3 in the paper, so the prompt stays within the context window. No weights change: the "reinforcement" is text in the prompt, which is why the paper calls it verbal reinforcement learning. The same idea is described on the reflection page.

The simulation models why this helps. A failed task has a pull towards the same mistake: a plain retry succeeds with only

s0 = (1 − rho) · p

where rho is the repeat-the-mistake correlation. If retries were independent (rho = 0), ten retries would solve almost everything, which is not what agents do. A lesson breaks part of that pull. With m lessons in memory the retry succeeds with

s_m = (1 − rho · (1 − lambda)^m) · p, with m = min(t − 1, Omega)

so a lesson can at best make a retry as good as a fresh attempt, and a task the agent cannot do stays hard. A task fails all T trials with probability

F(T) = (1 − p) · product over t = 2..T of (1 − s_m(t))

and the success rate is one minus the average of F over the tasks.

Worked example. With the defaults (T = 12, rho = 0.95, lambda = 0.25, Omega = 3) the first trial solves 69.4% of the tasks. A plain retry succeeds only 5% as often as a first attempt (0.05 × 69.4% = 3.5% on average), so twelve plain trials reach 77.3%. With three lessons in memory a retry reaches (1 − 0.95 × 0.75³) × 69.4% = 41.6%, and twelve Reflexion trials reach 98.3%, a gain of 21.0 points; about 0.41 of the 24 tasks are still expected to fail. The paper reports the same shape on ALFWorld: Reflexion solved 130 of 134 tasks (97%) in 12 trials, an absolute 22% above the baseline. The hardest pick-two task, with p = 0.2, still ends at 78.3%.

Running the trials one by one shows where the gain comes from: 69.4% after one trial, 74.2% after two, 80.7% after three, 90.3% after five and 98.3% after twelve. Because solved tasks stop, the twelve trials cost only 2.24 attempts per task on average.

Things to try

  • Set the lesson strength to 0. Reflexion collapses onto plain retries at 77.3%: memory only helps if the lessons change what the agent does.
  • Set the correlation rho to 0. Retries become independent and both methods reach 99.6%; Reflexion matters exactly because real agents repeat their mistakes.
  • Keep only one lesson in memory (Omega = 1). Success after twelve trials falls from 98.3% to 94.0% and about 1.44 tasks stay unsolved.
  • Set the trials to 4. Reflexion already reaches 86.5% against 71.8% for plain retries: most of the value arrives in the first few retries.
  • Look at the bars: tasks with p of 0.9 or more gain 3 to 6 points, while the pick-two tasks with p = 0.3 and 0.2 gain 50 points each.

How it works in real systems

The evaluator is the part that decides whether Reflexion works. In ALFWorld the environment says whether the task was completed, and the paper also used a heuristic that triggers a reflection when the agent repeats the same action or takes too many steps. In code, unit tests are the evaluator: Reflexion reached 91% pass@1 on HumanEval against 80% for GPT-4. Where no reliable signal exists, the agent grades itself, and a confident but wrong self-evaluation writes a wrong lesson.

Retries are not free. Every trial is a full agent run, so a cost per resolved task has to count the extra attempts; with the defaults that is 2.24 runs per task. Report success at a fixed number of trials, as in pass@k and pass^k, and count tokens as in the agent loop cost simulation.

In LangGraph the official tutorial builds the actor as a graph: an initial responder that answers and critiques itself, a tool node that runs searches, and a revisor that rewrites the answer using the reflections, looping a fixed number of times. The tutorial notes the trade-off plainly: the agent spends execution time to gain quality, and the reflections can be paired with external validators. Store lessons per task type rather than per run if the same mistakes recur across users, and keep the memory small: the paper capped it at one to three reflections.

The model here is a simplification. Real lessons can be wrong, can help some tasks and hurt others, and their effect is not the same for every task. Replace p_first with success rates you measured by running your agent several times per task, and fit rho and lambda to your own retry curve.

Sources

Last updated Nov 24, 2026
Type to search.
    ↑↓ navigate↵ open