Agentic AI / Patterns

Reflection

Intermediate [3/5]
Self-reflection Self-critique Meta-cognition

Definition

Reflection is an agentic pattern where AI systems evaluate their own outputs, identify errors or improvements, and iteratively refine their responses. The model acts as its own critic, catching mistakes that would otherwise reach the user.

Reflection enables self-improvement without additional human feedback, significantly enhancing output quality.

Key Concepts

  • Self-evaluation: Model assesses its own output quality
  • Critique generation: Identifying specific problems and suggestions
  • Iterative refinement: Multiple rounds of improvement
  • Stopping criteria: Knowing when output is good enough

Examples

Pattern
Reflection Loop
REFLECTION PATTERN: ┌─────────────────────────────────────────────┐ │ GENERATE │ │ Produce initial response to task │ └────────────────────┬────────────────────────┘ ↓ ┌─────────────────────────────────────────────┐ │ REFLECT │ │ "What's wrong with this response?" │ │ "How could it be better?" │ │ "Did I miss anything?" │ └────────────────────┬────────────────────────┘ ↓ ┌───────────────┐ │ Good enough? │ └───────┬───────┘ │ ┌──────────┴──────────┐ ↓ No ↓ Yes ┌─────────────────┐ ┌─────────────────┐ │ REVISE │ │ RETURN │ │ Apply critique │ │ Final output │ └────────┬────────┘ └─────────────────┘ │ └────────→ (back to REFLECT) MAX ITERATIONS: 3-5 (prevent infinite loops) QUALITY CRITERIA: - Factual accuracy - Completeness - Clarity - Relevance - Tone/style match
Example
Reflection in Code Generation
TASK: "Write a function to validate email addresses" GENERATION 1: def validate_email(email): return "@" in email REFLECTION 1: "This is too simple. Problems: - Doesn't check for valid domain - Allows multiple @ symbols - No length validation - Doesn't handle edge cases" GENERATION 2: import re def validate_email(email): pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$' return bool(re.match(pattern, email)) REFLECTION 2: "Better, but issues remain: - Regex could be more comprehensive - Should handle None input - No docstring explaining behavior - Could return error messages, not just bool" GENERATION 3: import re from typing import Tuple def validate_email(email: str) -> Tuple[bool, str]: ''' Validate email address format. Args: email: Email address to validate Returns: Tuple of (is_valid, error_message) ''' if not email or not isinstance(email, str): return False, "Email must be a non-empty string" if len(email) > 254: return False, "Email too long" pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' if not re.match(pattern, email): return False, "Invalid email format" return True, "" REFLECTION 3: "Good! Comprehensive validation with: ✓ Type checking ✓ Length limit ✓ Proper regex ✓ Informative return values ✓ Documentation This is ready to return."

Interactive Exercise

Practice Reflection

Given this draft response to "Explain machine learning in simple terms":

"Machine learning is when computers learn from data using algorithms."

What critique would a reflection agent provide?

Pro Tips
  • Specific critiques work better than vague "make it better"
  • Use explicit criteria to guide reflection
  • 2-3 reflection rounds usually optimal; more adds latency
  • Different models can critique each other (cross-reflection)

Related Terms