# 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.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Reflexion: how written lessons lift an agent&#39;s success over retries&#34;,&#34;sub&#34;:&#34;Twenty-four household tasks in the style of ALFWorld. A plain retry tends to repeat the same mistake; a Reflexion retry carries the last few self-written lessons. Set the trials, the memory size and how much a lesson helps, then run the trials one by one.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;trials&#34;,&#34;label&#34;:&#34;Trials per task (T)&#34;,&#34;min&#34;:1,&#34;max&#34;:15,&#34;step&#34;:1,&#34;value&#34;:12,&#34;note&#34;:&#34;The Reflexion paper ran 12 trials on ALFWorld&#34;},{&#34;id&#34;:&#34;rho&#34;,&#34;label&#34;:&#34;Repeat-the-mistake correlation&#34;,&#34;min&#34;:0,&#34;max&#34;:1,&#34;step&#34;:0.05,&#34;value&#34;:0.95,&#34;note&#34;:&#34;0 = every retry is a fresh independent attempt; 1 = a failed task always fails again&#34;},{&#34;id&#34;:&#34;lam&#34;,&#34;label&#34;:&#34;Lesson strength&#34;,&#34;min&#34;:0,&#34;max&#34;:1,&#34;step&#34;:0.05,&#34;value&#34;:0.25,&#34;note&#34;:&#34;Share of that correlation each stored lesson removes&#34;},{&#34;id&#34;:&#34;om&#34;,&#34;label&#34;:&#34;Lessons kept in memory (Omega)&#34;,&#34;min&#34;:1,&#34;max&#34;:3,&#34;step&#34;:1,&#34;value&#34;:3,&#34;note&#34;:&#34;The paper bounds the memory to 1 to 3 reflections&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;task&#34;,&#34;type&#34;,&#34;p_first&#34;],&#34;rows&#34;:[[1,1,0.95],[2,1,0.9],[3,1,0.85],[4,1,0.8],[5,2,0.75],[6,2,0.6],[7,2,0.5],[8,2,0.4],[9,3,0.95],[10,3,0.9],[11,3,0.85],[12,3,0.7],[13,4,0.9],[14,4,0.85],[15,4,0.75],[16,4,0.6],[17,5,0.9],[18,5,0.8],[19,5,0.65],[20,5,0.5],[21,6,0.6],[22,6,0.45],[23,6,0.3],[24,6,0.2]],&#34;note&#34;:&#34;Illustrative: 24 tasks, four per ALFWorld type (1 pick, 2 clean, 3 heat, 4 cool, 5 look, 6 pick two), with each task&#39;s first-trial success for a ReAct agent. Mean 69%, near ReAct&#39;s 70.9% for PaLM-540B. Use your own measured rates.&#34;},&#34;pre&#34;:[{&#34;id&#34;:&#34;d&#34;,&#34;label&#34;:&#34;Correlation kept per lesson&#34;,&#34;expr&#34;:&#34;1 - lam&#34;,&#34;hide&#34;:true}],&#34;row&#34;:[{&#34;id&#34;:&#34;s_plain&#34;,&#34;label&#34;:&#34;retry success, no lesson&#34;,&#34;expr&#34;:&#34;(1 - rho)*p_first&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;s_0 = (1-\\rho)\\,p_i&#34;},{&#34;id&#34;:&#34;fp&#34;,&#34;label&#34;:&#34;fails all T plain trials&#34;,&#34;expr&#34;:&#34;(1 - p_first)*(1 - s_plain)^(trials - 1)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;F^{plain}_i(T) = (1-p_i)\\,(1-s_0)^{T-1}&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;fr&#34;,&#34;label&#34;:&#34;fails all T Reflexion trials&#34;,&#34;expr&#34;:&#34;(1 - p_first)*ifelse(ge(om, 1)*ge(trials, 2), 1 - (1 - rho*d^1)*p_first, 1)*ifelse(ge(om, 2)*ge(trials, 3), 1 - (1 - rho*d^2)*p_first, 1)*ifelse(ge(om, 3)*ge(trials, 4), 1 - (1 - rho*d^3)*p_first, 1)*(1 - (1 - rho*d^om)*p_first)^max(trials - 1 - om, 0)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;F^{refl}_i(T) = (1-p_i)\\prod_{t=2}^{T}\\big(1 - (1-\\rho\\,(1-\\lambda)^{m_t})\\,p_i\\big),\\quad m_t=\\min(t-1,\\Omega)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;sp&#34;,&#34;label&#34;:&#34;solved, plain retries&#34;,&#34;expr&#34;:&#34;1 - fp&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;sr&#34;,&#34;label&#34;:&#34;solved, Reflexion&#34;,&#34;expr&#34;:&#34;1 - fr&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;gain&#34;,&#34;label&#34;:&#34;gain from lessons&#34;,&#34;expr&#34;:&#34;sr - sp&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;frk&#34;,&#34;label&#34;:&#34;fails first k trials (Reflexion)&#34;,&#34;expr&#34;:&#34;(1 - p_first)*ifelse(ge(om, 1)*ge(k, 2), 1 - (1 - rho*d^1)*p_first, 1)*ifelse(ge(om, 2)*ge(k, 3), 1 - (1 - rho*d^2)*p_first, 1)*ifelse(ge(om, 3)*ge(k, 4), 1 - (1 - rho*d^3)*p_first, 1)*(1 - (1 - rho*d^om)*p_first)^max(k - 1 - om, 0)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;fpk&#34;,&#34;label&#34;:&#34;fails first k trials (plain)&#34;,&#34;expr&#34;:&#34;(1 - p_first)*(1 - s_plain)^(k - 1)&#34;,&#34;hide&#34;:true}],&#34;derived&#34;:[{&#34;id&#34;:&#34;first&#34;,&#34;label&#34;:&#34;Success on the first trial&#34;,&#34;expr&#34;:&#34;mean(p_first)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;\\bar p = \\frac{1}{n}\\sum_i p_i&#34;},{&#34;id&#34;:&#34;plain_t&#34;,&#34;label&#34;:&#34;Success after T plain trials&#34;,&#34;expr&#34;:&#34;mean(sp)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;S^{plain}(T) = 1 - \\frac{1}{n}\\sum_i F^{plain}_i(T)&#34;},{&#34;id&#34;:&#34;refl_t&#34;,&#34;label&#34;:&#34;Success after T Reflexion trials&#34;,&#34;expr&#34;:&#34;mean(sr)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;S^{refl}(T) = 1 - \\frac{1}{n}\\sum_i F^{refl}_i(T)&#34;},{&#34;id&#34;:&#34;gain_t&#34;,&#34;label&#34;:&#34;Gain from the lessons (points)&#34;,&#34;expr&#34;:&#34;refl_t - plain_t&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;unsolved&#34;,&#34;label&#34;:&#34;Tasks still expected to fail (of n)&#34;,&#34;expr&#34;:&#34;sum(fr)&#34;,&#34;tex&#34;:&#34;\\sum_i F^{refl}_i(T)&#34;},{&#34;id&#34;:&#34;retry_best&#34;,&#34;label&#34;:&#34;Best retry success after Omega lessons&#34;,&#34;expr&#34;:&#34;mean(p_first)*(1 - rho*d^om)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;\\bar s_\\Omega = (1-\\rho\\,(1-\\lambda)^{\\Omega})\\,\\bar p&#34;},{&#34;id&#34;:&#34;mfk&#34;,&#34;label&#34;:&#34;Share failing after k trials&#34;,&#34;expr&#34;:&#34;mean(frk)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;sk&#34;,&#34;label&#34;:&#34;Success after k Reflexion trials&#34;,&#34;expr&#34;:&#34;1 - mfk&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;pk&#34;,&#34;label&#34;:&#34;Success after k plain trials&#34;,&#34;expr&#34;:&#34;1 - mean(fpk)&#34;,&#34;fmt&#34;:&#34;p&#34;}],&#34;iterate&#34;:{&#34;state&#34;:[{&#34;id&#34;:&#34;k&#34;,&#34;label&#34;:&#34;Trials run so far&#34;,&#34;init&#34;:&#34;1&#34;},{&#34;id&#34;:&#34;att&#34;,&#34;label&#34;:&#34;Expected attempts per task so far&#34;,&#34;init&#34;:&#34;1&#34;}],&#34;update&#34;:[{&#34;id&#34;:&#34;k&#34;,&#34;expr&#34;:&#34;k + 1&#34;},{&#34;id&#34;:&#34;att&#34;,&#34;expr&#34;:&#34;att + mfk&#34;}],&#34;max&#34;:14,&#34;per&#34;:1,&#34;track&#34;:&#34;sk&#34;},&#34;chart&#34;:{&#34;title&#34;:&#34;Success after T trials against first-trial success&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;First-trial success of a task (p)&#34;,&#34;from&#34;:0,&#34;to&#34;:1},&#34;y&#34;:{&#34;from&#34;:0,&#34;to&#34;:1},&#34;series&#34;:[{&#34;label&#34;:&#34;Reflexion after T trials&#34;,&#34;expr&#34;:&#34;1 - (1 - t)*ifelse(ge(om, 1)*ge(trials, 2), 1 - (1 - rho*d^1)*t, 1)*ifelse(ge(om, 2)*ge(trials, 3), 1 - (1 - rho*d^2)*t, 1)*ifelse(ge(om, 3)*ge(trials, 4), 1 - (1 - rho*d^3)*t, 1)*(1 - (1 - rho*d^om)*t)^max(trials - 1 - om, 0)&#34;},{&#34;label&#34;:&#34;Plain retries after T trials&#34;,&#34;expr&#34;:&#34;1 - (1 - t)*(1 - (1 - rho)*t)^(trials - 1)&#34;},{&#34;label&#34;:&#34;One trial only&#34;,&#34;expr&#34;:&#34;t&#34;,&#34;dash&#34;:true}],&#34;fit&#34;:{&#34;x&#34;:&#34;p_first&#34;,&#34;y&#34;:&#34;sr&#34;,&#34;label&#34;:&#34;Each task with Reflexion&#34;}},&#34;level&#34;:&#34;intermediate&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Points of success each task gains from the lessons&#34;,&#34;y&#34;:&#34;gain&#34;,&#34;label&#34;:&#34;task&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Trial 1: the actor tries&#34;,&#34;text&#34;:&#34;A ReAct-style actor attempts every task once and an evaluator marks it solved or failed. On average {first:p} of the tasks succeed.&#34;,&#34;show&#34;:[&#34;first&#34;]},{&#34;title&#34;:&#34;Retry without memory&#34;,&#34;text&#34;:&#34;A plain retry of a failed task mostly repeats the same plan, so it succeeds with only (1 - rho) times p. After {trials} trials plain retries reach {plain_t:p}.&#34;,&#34;show&#34;:[&#34;plain_t&#34;]},{&#34;title&#34;:&#34;Reflect on the failure&#34;,&#34;text&#34;:&#34;After a failed trial the model writes a short lesson about what went wrong. The memory keeps the last {om} lessons, and each one removes a share {lam} of the pull towards the old mistake, so a retry can reach {retry_best:p} on average.&#34;,&#34;show&#34;:[&#34;retry_best&#34;]},{&#34;title&#34;:&#34;Retry with the lessons&#34;,&#34;text&#34;:&#34;With the lessons in the prompt, {trials} trials solve {refl_t:p} of the tasks, {gain_t:p} more than plain retries; about {unsolved} of the 24 tasks are still expected to fail.&#34;,&#34;show&#34;:[&#34;refl_t&#34;,&#34;gain_t&#34;,&#34;unsolved&#34;]},{&#34;title&#34;:&#34;Run the trials one by one&#34;,&#34;text&#34;:&#34;Step the loop: after {k} trials Reflexion has solved {sk:p} against {pk:p} for plain retries, using {att} attempts per task on average because solved tasks stop early.&#34;,&#34;show&#34;:[&#34;k&#34;,&#34;sk&#34;,&#34;pk&#34;,&#34;att&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## 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](/ai-simulations/react-tool-calling) and the [ReAct loop](/tools/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](/tools/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](/ai-simulations/pass-at-k-vs-pass-power-k), and count tokens as in the [agent loop cost simulation](/ai-simulations/agent-loop-token-cost).

In [LangGraph](/tools/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

- [Reflexion code, logs and run parameters (noahshinn/reflexion)](https://github.com/noahshinn/reflexion)
- [ReAct prompting code and ALFWorld results (ysymyth/ReAct)](https://github.com/ysymyth/ReAct)
- [LangGraph Reflexion tutorial notebook](https://github.com/langchain-ai/langgraph/blob/23961cff61a42b52525f3b20b4094d8d2fba1744/docs/docs/tutorials/reflexion/reflexion.ipynb)
