Search

Search papers and lessons

Reflexion: Language Agents with Verbal Reinforcement Learning

Noah Shinn, Federico Cassano, Edward Berman et al. · 2023

Reflexion lets a language agent learn from failure by writing verbal reflections into memory, without changing its weights, and beats strong baselines on ALFWorld, HotpotQA and HumanEval.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Language & LLMs
  • Neural network basics
  • Linear algebra
  • Architectures
  • Sign in to see your progress

TL;DR

A language agent that fails a task usually fails the same way again, because nothing carries the lesson into the next attempt. Reflexion turns the environment's success or failure signal into a short written reflection about what went wrong, and stores that reflection in memory for the next trial. The model's weights are never updated; the only thing that changes is the text the agent conditions on. On ALFWorld, HotpotQA and HumanEval the method beats strong baselines by about 22, 20 and 11 points.

Where you'll see it: in agent loops that retry a task, such as a coding agent that runs tests and rewrites its solution, where the feedback is a failed check rather than a gradient.

The problem

By 2023 language models were being used as agents that act in environments, through tools, APIs and text worlds. The paper's motivation is that traditional reinforcement learning methods need extensive training data and expensive fine-tuning of the model. Scalar rewards are also hard to use with language models: a single number says that an attempt failed, but not which step caused the failure. This is the credit assignment problem, and it makes the signal hard to act on in a long trajectory.

Key idea

Convert the sparse signal into language. After each failed trial, a second model reads the trajectory and the outcome and writes a few sentences about what it should have done differently. Those sentences are appended to a memory. On the next trial the actor sees its own past reflections as extra context, so it can avoid the same mistake. The authors call the reflection a "semantic" gradient: it gives a direction for improvement in words, where a gradient would give one in numbers.

flowchart LR
  A[Actor produces trajectory] --> E[Evaluator gives reward]
  E -->|success| D[Done]
  E -->|failure| R[Self-reflection writes lesson]
  R --> M[(Memory of reflections)]
  M --> A

The method

Three models

Reflexion has three components, which can be separate LLM calls or the same model with different prompts:

  • The Actor MaM_{a} generates text and actions. It is built on a prompted language model, with chain-of-thought or ReAct as the generation style.
  • The Evaluator MeM_{e} scores a trajectory and returns a reward rtr_{t}. For reasoning this is exact match against the answer. For decision-making it is a hand-written heuristic or an LLM judgement. For programming it is the result of self-generated unit tests.
  • The Self-reflection model MsrM_{sr} takes the trajectory, the reward and the current memory, and writes a verbal summary srtsr_{t} that is stored in memory.

The loop

The paper states the process as an iterative loop. In trial tt the actor produces a trajectory τt\tau_{t}, the evaluator scores it, and, if the score is not success, the self-reflection model writes srtsr_{t} into memory. The loop stops when the evaluator accepts the trajectory or after a fixed number of trials.

mem = []
for t = 0, 1, 2, ...
    tau_t = Actor(task, mem)          # short-term memory is the trajectory
    r_t   = Evaluator(tau_t)
    if r_t is success: stop
    sr_t  = SelfReflection(tau_t, r_t, mem)
    mem.append(sr_t)                  # long-term memory is the reflections

Short-term memory is the current trajectory. Long-term memory is the list of reflections, bounded by a maximum number of stored experiences Ω\Omega, usually 1 to 3, so the context stays within the model's limit. The HotpotQA experiments use three experiences and the programming experiments use one.

The authors argue that the reflection is more useful than a scalar reward for three reasons. It can name a specific action that caused a later failure, which solves the credit assignment problem in words. It is interpretable, because a person can read it. And it gives explicit hints for the next trial. The cost is that Reflexion depends on the model's ability to judge its own work, and there is no formal guarantee of success.

Choosing the evaluator

The method depends on what the evaluator can see. The paper tries three signals. Binary environment feedback is the simplest. Pre-defined heuristics detect common failure patterns, such as a hallucinated possession in ALFWorld. Self-evaluation, by an LLM or by self-written unit tests, is used when no external check exists. Programming is the most grounded case, because the authors can generate a test suite of up to six unit tests, filter the tests for valid syntax by building an abstract syntax tree, and sample from the result.

Results

Decision making: ALFWorld

ALFWorld has 134 text-based environments across six task types. The paper uses ReAct as the actor and two self-evaluation methods: an LLM classifier and a heuristic that detects hallucinations and inefficient planning. ReAct plus Reflexion completes 130 of 134 tasks, which is 97 percent. The ReAct-only baseline stops improving between trials 6 and 7. Reflexion keeps learning across the 12 consecutive trials. The paper's analysis is that long-term memory helps in two cases: an early mistake in a long trajectory can be identified and corrected, and a room with many containers can be searched more systematically using past experience.

Reasoning: HotpotQA

HotpotQA has 113k question-answer pairs over Wikipedia. The paper tests two settings. In the first, chain-of-thought gets the ground-truth context, which isolates reasoning. In the second, a ReAct agent retrieves Wikipedia pages itself. Exact match against the answer gives the binary signal between trials. On 100 questions, the baselines do not improve on any task that failed in the first trial, while Reflexion does. The ground-truth-context agent still misses 39 percent of questions, and Reflexion improves its accuracy by 14 points without access to the answer. An ablation against an episodic-memory baseline, which keeps only the most recent trajectory, shows that self-reflection adds about 8 points beyond the memory alone.

Programming

On HumanEval Python, Reflexion reaches 91.0 percent pass@1, against 80.1 for GPT-4 without it. On the Rust translation of HumanEval, the paper reports 68.0 against 60.0 for GPT-4. On MBPP Python, Reflexion scores 77.1, slightly below the 80.1 of GPT-4, which the authors discuss as a limitation of their self-generated tests. A new benchmark, LeetcodeHardGym, has 40 hard LeetCode problems in 19 languages; Reflexion reaches 15.0 percent pass@1 against 7.5 for the GPT-4 baseline the paper reports.

The authors' ablation on Rust HumanEval, using the 50 hardest problems, shows that both parts matter. Removing test generation drops accuracy to 52 percent, below the 60 percent baseline. Removing the self-reflection text, so the agent only retries from test feedback, gives no gain over the baseline. The paper concludes that blind trial and error without reflection is not enough on harder tasks.

Why it mattered

The paper's claim is that verbal reinforcement is a lightweight alternative to fine-tuning for agents, that it gives interpretable episodic memory, and that self-reflection beats both refinement-only and memory-only approaches in the authors' experiments. The authors also say it may make agents easier to monitor, since their reflections can be read before a tool is used.

The paper does not report any later effects, so this lesson does not describe any. The limitations section names local minima in policy optimisation, the sliding-window memory, and the difficulty of writing accurate tests for code that depends on hardware or external APIs.

Reading guide

Read the paper in this order, using these prerequisite lessons:

  1. Section 1, Introduction. Needs Language Model and In-Context Learning. The argument is that in-context learning is the only cheap way to teach a frozen model, which is the gap Reflexion fills. The phrase "credit assignment" points to Reinforcement Learning.
  2. Section 2, Related work. Needs ReAct Prompting for the actor style and Chain-of-Thought Prompting for the reasoning baseline. The comparison table is the easiest way to see what Reflexion adds.
  3. Section 3, Reflexion. Needs Verbal Reinforcement Learning, which defines the reflection loop, and Markov Decision Process for the notation of trajectories and rewards. The Evaluator and Memory paragraphs need no more than this.
  4. Section 4.1, ALFWorld. Needs ReAct: Synergizing Reasoning and Acting in Language Models for the actor and Experience Replay for the idea of learning from stored experience. The results table is a direct count of tasks.
  5. Section 4.2, HotpotQA. Needs Chain-of-Thought Prompting Elicits Reasoning in Large Language Models for CoT and the ablation design. Read the episodic-memory ablation to see what is new.
  6. Section 4.3, Programming. Needs no further prerequisites beyond the loop in Section 3. The test-suite filtering uses only standard parsing ideas.

Check yourself

QuestionReflexion does not update the model's weights. What does it change between trials, and where is the lesson stored?Show answer
The text the agent conditions on changes. Each failed trial adds a self-reflection to a long-term memory, and the actor reads that memory in the next trial, along with the current trajectory as short-term memory. The weights of the language model stay fixed.
QuestionOn ALFWorld, ReAct plus Reflexion completes 130 of 134 tasks. What is that as a percentage, and how many tasks did it fail?Show answer
130 divided by 134 is about 0.970, so 97.0 percent. It failed 4 tasks.
QuestionOn HumanEval Python, Reflexion reaches 91.0 percent pass@1 and the GPT-4 baseline reaches 80.1. Which gain does the introduction quote, and what is the arithmetic difference?Show answer
The difference is 91.0 minus 80.1, which is 10.9 percentage points, roughly the 11 points the introduction quotes as the improvement on HumanEval. The gain is measured in absolute points, not relative percent.
QuestionWhy does the paper count false negatives as less harmful than false positives in its self-generated tests, and why does that matter for the agent?Show answer
A false positive means the tests pass on a wrong solution, so the agent stops with a wrong answer. A false negative means the tests fail on a correct solution; the self-reflection then gets a chance to notice the faulty test and keep the working code. The design therefore prefers the error that the agent can recover from.
QuestionA team wants to use Reflexion on a task where no success signal exists. Based on the paper, what is the main risk?Show answer
The method depends on the evaluator. Without an external check, the agent relies on an LLM judgement or a heuristic, and if that judgement is wrong, the reflections point the agent in the wrong direction. The paper states there is no formal guarantee of success.

Sources

Read the full paper

Noah Shinn, Federico Cassano, Edward Berman et al., 2023. arXiv:2303.11366