Reflection and Self-Critique Loops
After each step, ask the model 'did that work? what next?' — the cheapest reliability win.
Reflection and Self-Critique Loops is a free AI Agents lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Reflection?
Reflection is when the agent looks at its own past actions and asks: "What went well? What went poorly? What should I do differently?"
The cheapest reliability win in agents.
Reflexion Paper
Shinn et al. 2023 introduced Reflexion. After each failed attempt, the agent writes a self-critique and uses it as guidance for the next attempt. Quality jumped dramatically.
A Simple Reflection Loop
def with_reflection(task, max_attempts=3):
reflections = []
for attempt in range(max_attempts):
result = agent.run(task, hints=reflections)
if result.success:
return result
reflection = llm.invoke(f'''
Task: {task}
Attempt: {attempt + 1}
What went wrong: {result.error}
Reflect on what to do differently next time.
''').content
reflections.append(reflection)
return last_resultSelf-Critique Without Failure
You can also have the agent self-critique successful outputs:
critique_prompt = '''
Review your answer for:
1. Is it factually correct?
2. Is it complete?
3. Are there obvious improvements?
If improvements possible, return REVISE: <revised answer>.
Otherwise return ACCEPT.
'''Critic as a Separate Model
Use a different model for critique:
- Reduces shared biases
- Can be cheaper (small critic, big author)
- Or vice versa (small author, big critic)
Step-Level Reflection
Reflect after each STEP, not just at the end:
thought = 'I will use search_kb'
action = call_search_kb(...)
observation = '...'
reflection = llm.invoke(f'Did that step help? Should I try a different tool next?').contentCost Awareness
Reflection doubles LLM calls. For latency-critical paths, skip it. For high-quality outputs, it's worth it.
Avoiding Sycophantic Reflection
If you ask "did that work?" the model often says "yes". Force critical evaluation:
critique_prompt = 'List 3 SPECIFIC PROBLEMS with the previous answer. If you cannot find any, say WHY there are none.'Reflection Memory
Save reflections to a persistent store. Over many sessions, the agent builds a "lessons learned" library:
save_reflection({
'task_type': 'sql-write',
'lesson': 'Always check if table is partitioned before running heavy aggregations.',
'created_at': now()
})Retrieve Relevant Past Reflections
At the start of a new task, retrieve lessons matching the task type:
lessons = vector_db.search(embed(task_description), filter={'type': 'reflection'}, k=3)
prompt += '\nLessons learned from past tasks:\n' + '\n'.join(lessons)A Concrete Win
Adding "After completing each step, briefly note what could go wrong and double-check" to a code-agent prompt typically reduces bug rate by 15-30% in benchmarks.
Verifier-Generator
Variant: have one LLM generate, another verify. If verifier rejects, generator retries with the rejection reason. Used in many production agents today.
When NOT to Reflect
- Already-cheap one-shot tasks
- Real-time chat
- Latency-critical paths
Reflexion Insight
What is the key insight from the Reflexion paper?
Recap
Reflection is cheap quality. Critique after failures, save lessons, retrieve them for similar future tasks. Use a separate critic model when possible.
Frequently asked questions
Is the “Reflection and Self-Critique Loops” lesson free?
Yes — the full text of “Reflection and Self-Critique Loops” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Reflection and Self-Critique Loops”?
After each step, ask the model 'did that work? what next?' — the cheapest reliability win. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reflection and Self-Critique Loops” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Hierarchical Task Decomposition
- Goal Stacks and Backtracking
- World Models and Lookahead
- Reflection and Self-Critique Loops