Reflection Agent: Self-Improving AI Through Iterative Critique
AI Audio Version
The Reflection Pattern is the simplest yet surprisingly effective agentic design pattern. It allows an AI agent to evaluate its own output and iteratively improve it β like having an infinite pair-review loop built into the system.
Introduction to Reflection Agent
A Reflection Agent is an agent that thinks about its own outputs or decisions, evaluates their quality or correctness, and then revises or improves them. This is inspired by metacognitionβthinking about thinking. The agent uses its own generated critique or structured prompts to self-evaluate and refine its responses.
Key Characteristics
Generates initial output
Critiques or reflects on that output
Refines or rewrites based on the reflection
Often operates in a two-pass loop: generate β reflect β improve
Can be part of self-correcting, ReAct, or Chain-of-Thought workflows
Analogy
Think of a student writing an essay:
Writes the first draft
Reads it and thinks, βHmm, this part is vague.β
Revises that section for clarity
Thatβs reflection in action.
Simple Python Example
#| echo: true
def generate_summary(text):
return f"Summary: {text[:50]}..."
def reflect_on_summary(summary):
if "..." in summary:
return "This summary seems incomplete or too short."
return "Summary looks good."
def improve_summary(text, reflection):
if "incomplete" in reflection:
return f"Improved Summary: {text[:100]}..."
return generate_summary(text)
# Input text
text = "Artificial Intelligence (AI) enables machines to learn from data, adapt to new inputs, and perform tasks that typically require human intelligence."
# Run reflection agent
summary = generate_summary(text)
reflection = reflect_on_summary(summary)
improved = improve_summary(text, reflection)
print(improved)Common Reflection Loop
Task: Generate a response or solution
Self-critique: Evaluate it (e.g., clarity, correctness, completeness)
Refine: Use critique to revise the original
This loop can run once or multiple times.
LLM-Based Example (High-Level)
Prompt format:
Step 1: Write a short blog intro
Step 2: Critique it (Was it engaging? Too vague?)
Step 3: Rewrite it using the feedback
You can use LangChain or CrewAI to chain these steps into a multi-agent or single-agent pipeline.
Use Cases
Self-revising writing agents (blogs, summaries, essays)
Code improvement agents (e.g., Copilot β Critique β Fix)
Research agents that review and refine findings
Teaching agents that reflect on student misconceptions
Email agents that rewrite for tone, clarity, and grammar
Key Prompt Pattern for LLMs
Task: Summarize the following text.
Reflection: Is the summary accurate, complete, and clear?
Improved Output: Rewrite the summary based on your reflection.
π Active Projects
Browse these practical implementations to see the Reflection Pattern in action. You can find the full source code on GitHub.
π Self-Improving Summary Generator
This agent takes a block of text, generates a summary, then critiques its own output, and finally rewrites the summary based on that reflection. It operates in three main steps:
Generate β Reflect β Revise all handled internally using prompt chaining or custom logic.
- Metacognitive flow: Demonstrates how agents can βthink about their thinking.β
- Flexible Reflection: Can use basic logic or an LLM for the critique phase.
- Extensible Architecture: Structured for easy expansion into multi-pass, scoring, or reasoning loops.
# Step 1: Generate a summary
def generate_summary(text: str) -> str:
return text[:100] + "..." if len(text) > 100 else text
# Step 2: Reflect on the summary quality
def reflect_on_summary(summary: str) -> str:
if len(summary.split()) < 20:
return "The summary is too short and lacks detail."
elif "..." in summary:
return "The summary seems incomplete or abrupt."
return "The summary is clear and sufficiently detailed."
# Step 3: Improve the summary
def improve_summary(text: str, reflection: str) -> str:
if "too short" in reflection or "incomplete" in reflection:
return text[:200] + "..." if len(text) > 200 else text
return generate_summary(text)
# Full Reflection Agent
def reflection_summary_agent(text: str) -> str:
summary = generate_summary(text)
print("π Initial Summary:", summary)
reflection = reflect_on_summary(summary)
print("π Reflection:", reflection)
improved = improve_summary(text, reflection)
print("β
Improved Summary:", improved)
return improved
# π§ͺ Example
text = (
"Artificial intelligence (AI) is revolutionizing industries by enabling machines to perform tasks that normally require human intelligence. "
"From healthcare to finance, AI is optimizing workflows, making predictions, and transforming decision-making processes. "
"This shift is largely powered by advancements in machine learning, natural language processing, and computer vision."
)
reflection_summary_agent(text)π Initial Summary: Artificial intelligence (AI) is revolutionizing industries by enabling machines to perform tasks tha...
π Reflection: The summary seems incomplete or abrupt.
β
Improved Summary: Artificial intelligence (AI) is revolutionizing industries by enabling machines to perform tasks that normally require human intelligence. From healthcare to finance, AI is optimizing workflows, making predictions, and transforming decision-making processes...
- Writing assistants that revise their own drafts for better clarity.
- Research summarizers that auto-improve quality based on accuracy checks.
- Multi-pass content generation workflows where output quality is paramount.
π Essay Evaluator Agent
This agent takes a short essay, evaluates it based on criteria like clarity, structure, and argument strength, and then suggests improvements or rewrites. It reflects on the essay before rewriting, mimicking a self-reviewing student or an AI tutor.
- User Content Reflection: Reflects on user-provided inputs rather than just its own generations.
- Tutor-style Feedback: Can simulate various βpersonasβ (Teacher, Peer, Critic) for the critique phase.
- Criteria-Driven: Uses explicit evaluation metrics to drive iterative improvements.
# Step 1: Evaluate the essay
def evaluate_essay(essay: str) -> str:
score = 0
feedback = []
if len(essay.split()) < 100:
feedback.append("The essay is too short.")
else:
score += 1
if "however" in essay or "on the other hand" in essay:
feedback.append("Good use of contrast and structure.")
score += 1
else:
feedback.append("Try adding transitional phrases for better flow.")
if "because" in essay:
feedback.append("Argument is supported with reasoning.")
score += 1
else:
feedback.append("Consider adding rationale for your opinions.")
return f"π§ Reflection:\n" + "\n".join(feedback), score
# Step 2: Rewrite based on feedback
def revise_essay(essay: str, feedback: str) -> str:
if "too short" in feedback:
return essay + " In conclusion, this topic requires more exploration to fully understand its implications."
if "Consider adding rationale" in feedback:
return essay + " This is important because it affects many aspects of society."
return essay
# Agent: Full pipeline
def essay_evaluator_agent(essay: str) -> str:
print("βοΈ Original Essay:\n", essay, "\n")
feedback, score = evaluate_essay(essay)
print(feedback)
print(f"π Score: {score}/3")
revised = revise_essay(essay, feedback)
print("\nβ
Revised Essay:\n", revised)
return revised
# π§ͺ Example
essay_text = (
"Technology has changed the way we communicate. Social media allows people to stay connected. "
"This can be positive, but also creates issues with privacy and attention spans."
)
essay_evaluator_agent(essay_text)βοΈ Original Essay:
Technology has changed the way we communicate...
π§ Reflection:
The essay is too short.
Try adding transitional phrases for better flow.
Consider adding rationale for your opinions.
π Score: 1/3
β
Revised Essay:
Technology has changed the way we communicate...
This is important because it affects many aspects of society.
- AI Writing Tutors providing instant student feedback.
- Automated Essay Scoring and revision loops for educational tech.
- Self-Reviewing Content Platforms that ensure a minimum quality bar.