Learn Agentic Patterns
CoursesInterviewSchoolVideosAI agent videosAI now
Learn Agentic Patterns

Learn agentic AI, then get interviewed on it. A free course, four job paths, a daily broken agent, and the interview.

learnagenticpatterns.com

Navigation

HomeThe courseThe interviewPatterns (developer track)PM trackBlogCareer GuideAboutSign Up

Connect

Cheat sheet (free PDF)Reskilling guideLinkedIn (Mousa Jawaheri)

© 2026 learnagenticpatterns.com

Privacy PolicyTerms of Service

Based on “Agentic Design Patterns” by Antonio Gullí

> All Patterns

[01]Prompt Chaining[02]Routing[03]Parallelization[04]Reflection[05]Tool Use[06]Planning[07]Multi-Agent Collaboration[08]Memory Management[09]Learning & Adaptation[10]State Management (MCP)[11]Goal Setting & Monitoring[12]Exception Handling & Recovery[13]Human-in-the-Loop[14]Knowledge Retrieval (RAG)[15]Inter-Agent Communication (A2A)[16]Resource-Aware Optimization[17]Reasoning Techniques[18]Guardrails & Safety[19]Evaluation & Monitoring[20]Prioritization[21]Exploration & Discovery[22]Boundary Translation
HomeCurriculumReflection
Pattern [04]

Reflection

≈ Unit Testing / Code Review / Test-Driven Development (TDD) Loop

[04]

TL;DR

Enables an agent to critique its own output (or the output of another agent) to identify errors, hallucinations, or areas for improvement, and then iteratively refine the result. It acts as an internal feedback loop.

SWE: Unit Testing / TDD Loop
Architecture Flow
agentgate
Coder AgentagentCritic AgentagentFeedback LoopgateMax Retries Gategate

> Agentic Definition

Reflection (or Self-Correction) enables an agent to critique its own output (or the output of another agent) to identify errors, hallucinations, or areas for improvement, and then iteratively refine the result. It acts as an internal feedback loop, significantly increasing the quality and robustness of the final output.

Before: Single Pass Generation

Single Pass Generation
1def generate_code(spec):
2 return codegen_module.generate(spec)
3 # If it's buggy, the user finds out later in production.

After: Reflective Loop Architecture

Reflective Loop Architecture
1def generate_robust_code(spec):
2 # Draft Phase
3 code = coder_agent.run(spec)
4
5 # Reflection Loop (The "Code Review")
6 for _ in range(MAX_RETRIES):
7 # The Critic Agent acts as the Unit Test Suite
8 critique = reviewer_agent.run(code)
9
10 if critique.is_approved():
11 return code
12 else:
13 # Feedback injection: The loop enables self-correction
14 code = coder_agent.run(spec, feedback=critique.comments)
15
16 raise MaxRetriesExceededError("Unable to generate valid code")

Traditional SWE

Unit Testing / TDD Loop

≈maps to

Agentic Pattern

Reflection

≈How They're Similar

Both exist to catch errors before final delivery. The "Red-Green-Refactor" loop in TDD is structurally analogous to the "Draft-Critique-Revise" loop in Reflection. It is a quality assurance gate embedded in the development/execution process.

≠Key Divergence

Unit tests check against known, deterministic assertions (e.g., assert x == 5). Reflection checks against qualitative, ambiguous criteria (e.g., "Is this tone professional?", "Is this code secure?", "Does this argument make sense?") using an LLM as the judge. The "test" itself is probabilistic.

> Production Considerations

01

Reflection is expensive in terms of time. It can double or triple latency depending on the number of reflection cycles permitted. It is a trade-off: higher latency for higher accuracy.

02

Highly effective at reducing hallucinations. However, there is a risk of "degeneracy," where the agent critiques itself into a worse state or gets stuck in a loop of minor, inconsequential edits.

03

Increases token usage significantly. Use cheaper models for the drafting phase and stronger, more reasoning-capable models for the critique phase to optimize costs.

💡

Key Takeaway

Adapt: You are embedding the "Code Review" process directly into the runtime application. The system self-corrects before the human ever sees the output. You must design the "Critique Prompts" as carefully as you write unit tests.

BuildBuild a Self-Improving Code Generator. Create a system where an agent generates code, a critic reviews it, and the generator improves based on feedback — like an automated code review loop.

DebugDebug: Reflection. The critic finds 2 bugs but the coder never fixes them. The code ships with known issues. What's missing from the loop?

OptimizeOptimize a Reflection Loop. Your code generation pipeline runs Generator + Critic in a loop, averaging 2.5 iterations. Both use GPT-4o. Each code generation costs $0.035. Target: under $0.015 while maintaining quality above 88%.

PromptWrite a Self-Critique Prompt. You're building a code generation pipeline with a Reflection loop. After the Generator produces code, the Critic reviews it and suggests improvements. Write the system prompt for the Critic agent.

Frequently Asked Questions

When should I use the Reflection pattern?

Reflection (or Self-Correction) enables an agent to critique its own output (or the output of another agent) to identify errors, hallucinations, or areas for improvement, and then iteratively refine the result. It acts as an internal feedback loop, significantly increasing the quality and robustness of the final output.

How does Reflection relate to Unit Testing / Code Review / Test-Driven Development (TDD) Loop?

Both exist to catch errors before final delivery. The "Red-Green-Refactor" loop in TDD is structurally analogous to the "Draft-Critique-Revise" loop in Reflection. It is a quality assurance gate embedded in the development/execution process. However, there is a key divergence: Unit tests check against known, deterministic assertions (e.g., assert x == 5). Reflection checks against qualitative, ambiguous criteria (e.g., "Is this tone professional?", "Is this code secure?", "Does this argument make sense?") using an LLM as the judge. The "test" itself is probabilistic.

What are the production trade-offs of Reflection?

Reflection is expensive in terms of time. It can double or triple latency depending on the number of reflection cycles permitted. It is a trade-off: higher latency for higher accuracy. Highly effective at reducing hallucinations. However, there is a risk of "degeneracy," where the agent critiques itself into a worse state or gets stuck in a loop of minor, inconsequential edits. Increases token usage significantly. Use cheaper models for the drafting phase and stronger, more reasoning-capable models for the critique phase to optimize costs.

Further Reading

Blog · 3 min read

AI Agent Reflection Pattern: Self-Critique Explained (with Code)

The reflection pattern has an AI agent critique and revise its own output in a loop — generate, evaluate, improve. It's TDD for LLMs. Full walkthrough with code.

Next Pattern

[05] · Tool Use