≈ Unit Testing / Code Review / Test-Driven Development (TDD) Loop
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.
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.
1def generate_code(spec):2 return codegen_module.generate(spec)3 # If it's buggy, the user finds out later in production.1def generate_robust_code(spec):2 # Draft Phase3 code = coder_agent.run(spec)45 # Reflection Loop (The "Code Review")6 for _ in range(MAX_RETRIES):7 # The Critic Agent acts as the Unit Test Suite8 critique = reviewer_agent.run(code)910 if critique.is_approved():11 return code12 else:13 # Feedback injection: The loop enables self-correction14 code = coder_agent.run(spec, feedback=critique.comments)1516 raise MaxRetriesExceededError("Unable to generate valid code")Traditional SWE
Unit Testing / TDD Loop
Agentic Pattern
Reflection
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.
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.
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.
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.
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.
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.
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.