Reflection
Reflection and Self-Critique in LLMs
What is Reflection?
Reflection is a technique where an LLM evaluates and refines its own outputs, often improving quality through iterative self-critique.
Basic Reflection Pattern
[Initial Generation]
|
v
[Self-Critique]
"What could be improved? Are there any errors?"
|
v
[Refined Generation]
Incorporate feedback and generate improved outputImplementation Approaches
Single-Pass Reflection
def reflect_and_refine(prompt: str) -> str:
# Initial generation
initial = llm.generate(prompt)
# Self-critique
critique = llm.generate(f"""
Review this response for accuracy, clarity, and completeness:
{initial}
What could be improved?
""")
# Refined generation
refined = llm.generate(f"""
Original response: {initial}
Critique: {critique}
Generate an improved response addressing the critique.
""")
return refinedMulti-Pass Refinement
def iterative_refinement(prompt: str, max_iterations: int = 3) -> str:
response = llm.generate(prompt)
for i in range(max_iterations):
critique = llm.generate(f"Critique: {response}")
if "looks good" in critique.lower():
break
response = llm.generate(f"Improve based on: {critique}")
return responseReflexion Framework
Combines reflection with memory for agents:
1. Agent attempts task
2. Evaluator provides feedback
3. Self-reflection generates insights
4. Memory stores learnings
5. Next attempt uses accumulated insights
When Reflection Helps
| Scenario | Benefit |
|----------|---------|
| Complex writing | Improved structure and clarity |
| Problem solving | Catch reasoning errors |
| Code generation | Fix bugs before output |
| Factual accuracy | Identify and correct mistakes |
Considerations
- Adds latency (multiple LLM calls)
- Not always improvements (may introduce new errors)
- Works best with capable models
- Diminishing returns after 2-3 iterations