Reflexion (Shinn et al. 2023) lets an agent learn from its mistakes. After a failed attempt, the agent writes itself a short lesson and tries again.
Steps
- attempt the task
- check the result
- write a lesson, for example "check the total with GST"
- try again with the lesson in the context
Limit
A model that checks its own work shares its own blind spots. In the shopping task it will not spot a trick if it already believes the hidden text.
Prefer checks in code
A rule such as "total is at most 50 dollars" is a more reliable critic than the opinion of the model about itself.
Code
The lesson is stored as text and added to the next attempt:
lessons = []
def attempt(goal, llm, shop, lessons):
"""Run the agent once. The lessons from earlier failures are put in front of the goal."""
goal_with_lessons = {**goal, "text": goal["text"] + " Lessons: " + "; ".join(lessons)}
return run_guarded(goal_with_lessons, llm, shop, Mandate(), ask_user)
history = attempt(GOAL, llm, Shop(), lessons)
refusals = [h for h in history if isinstance(h["obs"], dict) and h["obs"].get("status") == "refused"]
for h in refusals:
lessons.append("; ".join(h["obs"]["reasons"])) # for example "seller TechDealz88 is not verified"Notes:
- the critic here is the code check
Mandate().allows, so the lesson comes from a reliable rule and not from the opinion of the model - the lesson is a form of episodic memory
The lessons and other facts that an agent keeps are its memory, see Agent Memory.