Overview
The Reflection pattern adds self-critique to agent workflows. After generating an initial response, the agent switches into "critic mode" to evaluate its work, then revises if needed.
Core Mechanism
Generate Initial Output
↓
Self-Critique (Critic Mode)
↓
Issues Found? → Yes → Revise Output → Loop
↓
No
↓
Return Final Output
Critique Dimensions
Accuracy Check
- Are facts correct?
- Are sources properly cited?
- Are calculations accurate?
Completeness Check
- Are all requirements addressed?
- Is anything missing?
- Are edge cases considered?
Consistency Check
- Does the output contradict itself?
- Is the logic sound?
- Are assumptions explicit?
Quality Check
- Is it well-written?
- Is the structure clear?
- Does it follow guidelines?
Implementation
async def generate_with_reflection(prompt, max_iterations=3):
output = await agent.generate(prompt)
for i in range(max_iterations):
critique = await agent.critique(output)
if critique.is_satisfactory:
return output
output = await agent.revise(output, critique.feedback)
return output # Return best effort after max iterations
When to Use Reflection
Good fit:
- High-stakes outputs where errors are costly
- Creative tasks benefiting from refinement
- Tasks with clear quality criteria
- Educational/explanatory content
Poor fit:
- Latency-critical applications
- Simple factual lookups
- Tasks where "good enough" is acceptable
Combining with Other Patterns
Reflection pairs well with:
- ReAct: Reflect on action outcomes
- Multi-Agent: Separate critic agent
- LLM-as-Judge: Formalize critique scoring
Anti-Patterns
Infinite Refinement
Agent never satisfied with output. Set maximum iterations.
Shallow Critique
"Looks good" isn't useful feedback. Require specific observations.
Over-Revision
Agent makes output worse through excessive changes. Track quality metrics.