AI engineering paper · 2023
Reflexion
How Reflexion lets an agent learn from a failed attempt by writing itself a note and trying again, and where that works and where it does not.
You’ll learn to
- Explain the Reflexion loop: a model that does the task, a check that scores it, a step that writes a note about the failure, and a memory of those notes, with no model weights changed.
- Trace a failed run and its repair on two benchmarks, ALFWorld (text games) and HotpotQA (questions that need several pages), and say what the written note added.
- Read the paper’s results, including the 91% on HumanEval (a set of Python coding tasks) and the MBPP result (a second coding set) that fell short, and say what each depended on.
- Say what later work found about self-correction with and without outside feedback, and what extra trials cost.
Read first: ReAct
The idea in short
An agent (a language model that works in a loop, using tools and reading what comes back) that fails a task usually fails again the same way if you just rerun it. Reflexion adds one step between attempts: a model reads the failed run and the score, writes a short note in plain words about what went wrong, and that note goes into the prompt (the text the model is given) of the next attempt.
No training happens and no weights change: the numbers inside the model stay as they are. The learning lives in a few lines of text. It worked well where something outside the model could say whether the attempt had failed, such as a game, a set of tests or an answer key, and it worked poorly where nothing could.
The problem
This guide covers “Reflexion: Language Agents with Verbal Reinforcement Learning” (Shinn and four co-authors, 2023). It builds on the ReAct guide, which explains the loop where a model thinks, acts and reads what came back. Here we only need the idea that such an agent (a language model that works in a loop, using tools and reading what comes back) runs for a number of steps and then either finishes or fails.
An agent might search for the wrong page, walk past the object it needs, or write code that fails its tests. The obvious response is to run it again. But the model reads the same prompt (the text it is given) and has the same habits, so it often makes the same mistake. The Reflexion authors tested this. In their runs on (questions that each need facts from several web pages), with some randomness added to the model’s output, the plain agents did not solve a single question on a later try that they had failed on the first.
The textbook answer is . You let a model try a task many times, give each try a , which is a number saying how well it went, and then adjust the model’s so that high-reward behaviour becomes more likely. The weights are the numbers the model learned in training. It needs many tries and a lot of computing to update billions of weights. And a single number is a poor teacher. If an agent takes 30 steps and gets a reward of zero, the number does not say which step was the mistake. The paper calls this the credit assignment problem: working out which of many steps deserves the blame or the credit.
Language models can read, though. A failed run is text, and a person who read it could say in one sentence what went wrong. The question the paper asks is: can the model write that sentence itself, and can the next attempt read it?