DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

Reflexion: Make an AI Agent Learn From Its Own Mistakes

An agent that fails a task and just… tries the same thing again is useless. Reflexion gives it a memory of its own mistakes: fail → write a lesson → retry with that lesson in context. It's learning without touching the weights.

🪞 Watch it fail, reflect, then nail it: https://dev48v.infy.uk/prompt/day20-reflexion.html

The loop

  1. Attempt the task.
  2. Evaluate — tests, a checker, or a self-critique say pass/fail (with a reason).
  3. On failure, reflect — the model writes a short verbal lesson: "I assumed X; the failing case shows Y; next time I'll Z."
  4. Retry with that reflection added to context. Repeat (capped) until it passes.

The demo shows attempt 1 failing on an edge case, a lesson getting written into a growing "reflection memory," and the next attempt using it to succeed.

Why it's powerful

The reflection is verbal, episodic memory — no fine-tuning, no gradient step. It's in-context learning at inference time, fully reversible, and it stops the agent repeating the same error.

Where it shines (and doesn't)

Great when there's a checkable signal — coding (run the tests), tool-use agents, puzzles. Weak when you can't tell whether an attempt actually failed, or when a bad evaluator sends it chasing the wrong fix.

It pairs with ReAct (act+observe) and is a core trick behind self-improving agents.

🔨 Full pattern (generate → evaluate → reflect → retry-with-memory) on the page: https://dev48v.infy.uk/prompt/day20-reflexion.html

Part of PromptFromZero. 🌐 https://dev48v.infy.uk

Top comments (0)