reflexion
reflexion is a self-improvement loop: the agent acts, evaluates its output, reflects on what to do differently, and repeats until satisfied.
When to use it
Section titled “When to use it”- Tasks requiring iterative self-improvement
- Agents that need to learn from mistakes within a single run
- Complex reasoning where the first attempt is rarely optimal
- Code generation with self-testing
File structure
Section titled “File structure”agents/my-reflexion/├── AGENT.md ← pattern: reflexion, max_iter, config└── reflect.md ← system prompt for the reflection stepAGENT.md
Section titled “AGENT.md”---name: code-improverdescription: Writes and iteratively improves code through reflection.version: "1.0.0"pattern: reflexionmax_iter: 4call: model: role: thinker---
You are an expert programmer. Write code that solves the given task.
After each attempt, you will receive a reflection with improvement suggestions.Incorporate the feedback and produce a better solution.
When your solution is complete and well-tested, call finish with key="DONE" and the final code.Call finish with key="DONE" (uppercase) to exit the loop early if satisfied.reflect.md
Section titled “reflect.md”---call: model: role: thinker temperature: 0.1---
You are a code reviewer. You have just seen an attempted solution.
Evaluate it on:1. Correctness — does it handle edge cases?2. Efficiency — is the algorithm optimal?3. Readability — is it clean and well-commented?
Provide specific, actionable feedback for the next iteration.
If the solution is already excellent, call finish with key="DONE" to stop the loop.Otherwise, call finish with key="continue" with your critique as the value.Scaffold
Section titled “Scaffold”tama add reflexion my-agentHow it works
Section titled “How it works”start() → task ↓[act: AGENT.md body] → attempt ↓[reflect: reflect.md] → critique or DONE ↓if DONE → exit loopif continue → back to act with critique in context ↓(repeat up to max_iter times)Stopping the loop
Section titled “Stopping the loop”The reflect step controls the loop:
finish(key="DONE", ...)— exit the loop, return the result- Any other key — continue to the next iteration
The max_iter field caps the total number of act-reflect cycles (default: 4 for reflexion).
The DONE convention
Section titled “The DONE convention”Use "DONE" (uppercase) to signal completion from within the loop. This is a convention — the reflexion runtime specifically checks for this key to exit.