Search

Search papers and lessons

Verbal Reinforcement Learning

Level 4 · 9 min read

Verbal reinforcement learning improves an agent by writing reflections on failed attempts into its prompt memory, so the next attempt changes without any update to the model's weights.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Neural network basics
  • Linear algebra
  • Architectures
  • Discrete math
  • Sign in to see your progress

TL;DR

Verbal reinforcement learning improves a language-model agent by having it reflect in words on each failed attempt. The reflection is stored in a short memory that is placed in the prompt of the next attempt. The model's weights never change. The reward signal is turned into text, and the text is what carries the lesson forward.

Where you'll see it: LLM agents that fix their own code after failing unit tests, web or household agents that retry a multi-step task with notes from the last failure, and self-correcting question answering.

Intuition

A coach's notes, not a new brain

Think of a student who keeps failing a driving test at the same roundabout. Each time the examiner says "you failed", which is a bare signal. A better examiner says "you cut in front of the bus at the second exit, check the mirror before you turn". The second message is useful because it names the mistake and the fix. The student writes it on an index card and reads it before the next test. The brain is the same, but the card changes what happens.

Reinforcement Learning normally improves an agent by changing its parameters, using many trials and a scalar reward. A large language model is expensive to retrain on every mistake, and a scalar like "0" tells it little. Verbal reinforcement keeps the parameters fixed. It turns the failure into a short piece of text that explains what went wrong, and it keeps that text in the agent's context, the way In-Context Learning puts examples into the prompt.

The paper's authors describe the reflection as acting like a "semantic" gradient signal. A gradient tells a parameter which way to move. A reflection tells the agent which decision to change, in ordinary words.

flowchart LR
  A["Actor runs a trial with memory"] --> B["Evaluator scores the trajectory"]
  B -->|success| D["Stop"]
  B -->|failure| C["Self-reflection writes a note"]
  C --> M["Memory keeps the latest notes"]
  M --> A

The math

The usual setup

In standard reinforcement learning an agent follows a policy πθ(a∣s)\pi_\theta(a \mid s), a probability of taking action aa in state ss, with parameters θ\theta. A trajectory τ\tau is the sequence of states and actions in one episode. The reward R(τ)R(\tau) is a number, and the learning goal is to choose θ\theta so that the expected reward is high. Methods such as Policy Gradient do this by moving θ\theta along the gradient of expected reward.

The verbal version

Verbal reinforcement keeps θ\theta fixed and adds a memory mem\mathit{mem}, a list of text notes. Let MaM_a be the actor (a language model with a prompt), MeM_e the evaluator, and MsrM_{sr} the self-reflection model, which is also a language model. One trial tt runs as follows.

  1. The actor produces a trajectory from the task and the current memory: τt=Ma(task,memt)\tau_t = M_a(\text{task}, \mathit{mem}_t).
  2. The evaluator scores it: rt=Me(τt)r_t = M_e(\tau_t). This is a scalar, or a binary success flag.
  3. If the attempt failed, the reflection model writes a note from the trajectory and the score: srt=Msr(τt,rt)sr_t = M_{sr}(\tau_t, r_t).
  4. The note is appended to memory, and only the most recent Ω\Omega notes are kept: memt+1=lastΩ(memt∪{srt})\mathit{mem}_{t+1} = \text{last}_{\Omega}(\mathit{mem}_t \cup \{sr_t\}).

The loop repeats until the evaluator accepts an attempt. The paper gives Ω\Omega as usually 1 to 3, because the notes must fit in the model's context window.

Read this as an optimisation. The "parameter" being improved is the memory, and the "update rule" is an LLM writing a note. The actor's policy is π(at∣ot,memt)\pi(a_t \mid o_t, \mathit{mem}_t), so changing the memory changes the policy without touching θ\theta.

Why a bounded memory is enough

Each note summarises one failed trial. Keeping the last Ω\Omega notes gives the actor the most recent lessons at a fixed prompt cost. The paper also names a limitation of this choice: the window forgets older lessons, and a lesson that matters only much later can be lost. Longer memories would need retrieval, such as a vector store.

Worked examples

Example
Counting problems solved with and without reflection

The paper runs an ablation on the 50 hardest HumanEval problems translated to Rust, using GPT-4 as the base model. The numbers below are the pass@1 accuracies from the paper's ablation table.

  1. Base model, no test generation and no reflection: 0.60⋅50=300.60 \cdot 50 = 30 problems solved.
  2. Full Reflexion, with generated tests and reflection: 0.68⋅50=340.68 \cdot 50 = 34 problems solved.
  3. Reflexion without internal test generation, so the agent reflects without checking its code: 0.52⋅50=260.52 \cdot 50 = 26 problems solved.
  4. Gain from reflection with tests: 34−30=434 - 30 = 4 more problems, an 8 point gain.
  5. Dropping test generation loses 34−26=834 - 26 = 8 problems relative to full Reflexion, which leaves it 30−26=430 - 26 = 4 problems below the base model.

Interpretation: reflection alone with no reliable signal does not help. The reflection needed the test outcomes to know what went wrong. Without them the agent made harmful edits, and accuracy fell below the baseline.

Example
A bounded memory with two notes (illustrative trace)

This trace is hand-written to show the mechanism. It is not output from the paper. Suppose the task is to find an object in a house, the memory size is Ω=2\Omega = 2, and the evaluator returns 0 for failure and 1 for success.

  1. Trial 1. Memory is empty. The agent opens the wrong drawer, searches the fridge, and reports failure, r1=0r_1 = 0. The reflection says: "I searched the fridge before checking the countertop, where the object was likely to be."
  2. Trial 2. Memory holds that note. The agent checks the countertop first, finds nothing, then searches a drawer, and fails again. The reflection says: "Check the drawer near the sink before the bedroom, because I skipped it."
  3. Trial 3. Memory now holds both notes, since Ω=2\Omega = 2 allows two. The agent starts at the sink drawer, finds the object, and succeeds, r3=1r_3 = 1.
  4. If a fourth failure happened, the oldest note would be dropped to make room.

Interpretation: no weights moved. Each failure changed what the actor saw next, and only the two most recent lessons were kept.

Tradeoffs and failure modes

SituationProblemBetter choice
The task has a reliable check, such as unit tests or an exact-match answerReflection works best with a clear signalUse this method, with the check as the evaluator
The only feedback is "failed" with no detailThe reflection has little to work fromGive richer feedback, such as compiler errors or environment messages
Long tasks with many lessonsThe bounded memory drops early lessonsAdd retrieval over a store of past notes
The model is weak at self-critiqueReflections can be wrong, and then they mislead the next attemptUse a stronger reflection model, or check reflections against the evaluator
Fine-tuning is possible and the task recurs at scalePrompt memory costs context on every callUse Fine-Tuning or a policy-gradient method such as Proximal Policy Optimization (PPO)

Failure modes to watch for:

  • Reflection drift. A wrong note persists in memory and steers later attempts the wrong way, because nothing forces the note to be checked.
  • Context cost. Every stored note is read on every trial. The bound on Ω\Omega keeps this under control, but it is a hard limit on how much can be learned.
  • Overfitting to the task. Notes written for one environment may not apply to another. The memory is specific to the task it was written for.
  • Missing signal. The paper reports that self-reflection without the test-generation step did worse than the base model. A reflection with no check can make the agent edit correct code into wrong code.

History

Shinn and colleagues (2023) introduced Reflexion, which the paper describes as reinforcing language agents through linguistic feedback rather than by updating weights. Their experiments covered three areas: sequential decisions in household text environments, multi-hop question answering, and code writing.

In the sequential decision task, the paper reports that ReAct with Reflexion completed 130 of 134 tasks, against a ReAct-only baseline whose performance stopped improving after a few trials. On a 100-question HotPotQA ablation, the paper reports an 8% absolute boost from the self-reflection step over an episodic-memory-only variant that keeps the most recent trajectory. On HumanEval in Python, the paper reports 91% pass@1 for Reflexion, against a previous best of 80.1% for GPT-4 as reported in the same table.

The paper frames Reflexion as an alternative to policy or value-based RL that needs no fine-tuning of the language model, and the loop that carries a lesson from one attempt to the next is its central mechanism.

Check yourself

QuestionWhat plays the role of the parameter update in verbal reinforcement learning, and what stays fixed?Show answer

The memory of reflection notes plays the role of the update. The model weights θ\theta stay fixed. Each failed trial adds a note, and that changes the prompt, which changes the policy π(at∣ot,memt)\pi(a_t \mid o_t, \mathit{mem}_t) without any change to θ\theta.

QuestionIn the Rust ablation, the base model has accuracy 0.60 and full Reflexion has 0.68 on 50 problems. How many more problems are solved, and what is the percentage point gain?Show answer

0.68⋅50=340.68 \cdot 50 = 34 and 0.60⋅50=300.60 \cdot 50 = 30, so 4 more problems are solved. The gain is 0.68−0.60=0.080.68 - 0.60 = 0.08, which is 8 percentage points.

QuestionAn agent has memory size Ω=3\Omega = 3 and has failed five trials. Which notes are in memory before trial six?Show answer

Only the three most recent reflections, from trials three, four and five. The reflections from trials one and two were dropped when newer notes were added, because the memory keeps the last Ω\Omega items.

QuestionWhy did removing the test-generation step make Reflexion worse than the base model on the Rust subset?Show answer

Without generated tests, the agent cannot tell whether its current code is correct, so its reflections are not grounded. It keeps making edits, some of which break working code. The paper reports 52% accuracy for this variant, against 60% for the base model.

QuestionA team wants an agent to improve on a task that recurs thousands of times a day, and the prompt memory is costing too much context. What are two options and their tradeoffs?Show answer

Fine-tuning or a policy-gradient method changes the weights, so the memory is no longer needed in every prompt. The cost is the training effort and the need for a reliable reward signal. Keeping the prompt memory costs context on every call but needs no training. A reasonable choice depends on how often the task recurs and how stable it is.

Sources