"Self-healing" code generation isn't the model silently getting it right on a second guess — it's a structured loop where actual execution results (not the model's opinion of its own code) drive the next attempt. Reflexion formalized this as three roles: an actor that generates code, an evaluator that runs it and scores the result, and a self-reflection step that converts failures into a written lesson for the next attempt.
Building the loop
1. Generate an attempt. The actor model writes code for the task, same as any code-generation call.
2. Actually execute it. Run the generated code against real tests or the interpreter — this is the step that produces ground truth, not a model self-assessment.
3. On failure, generate a reflection before retrying. Feed the actual error output to the model and ask it to write a short diagnosis of what went wrong — that diagnosis becomes part of the next attempt's prompt, not just "try again."
# Reflexion-style actor / evaluator / self-reflection loop
def self_healing_generate(task, max_attempts=3):
reflection = ""
for attempt in range(max_attempts):
# Actor: generate code, informed by any prior reflection
code = generate_code(task, prior_reflection=reflection)
# Evaluator: actually execute it - this is real ground truth
result = run_tests(code)
if result.passed:
return code
# Self-reflection: turn the real failure into a written lesson
reflection = generate_reflection(code, result.error_output)
raise RuntimeError(f"Failed after {max_attempts} attempts. Last error: {result.error_output}")
| Approach | What drives the retry |
|---|---|
| Naive retry ("try again") | No new information — the model repeats similar mistakes |
| Reflexion-style loop | Real execution output, converted into an explicit written reflection |
The paper's own results back this up concretely: Reflexion agents improved on Python programming tasks (HumanEval) by as much as 11 percentage points over baseline approaches — the gain comes specifically from grounding each retry in real execution feedback rather than blind resampling.
Practical Challenge
Take a small coding task, write one deliberately buggy solution, and manually write the "self-reflection" you'd want an agent to produce from the resulting error message — then compare it to what the actual traceback tells you.
Concept Check
Sources & Further Reading
- Reflexion: Language Agents with Verbal Reinforcement Learning — the paper this article's actor/evaluator/self-reflection pattern is based on.
AI