← Back to Articles
Agentic Architectural Patterns • Part 4

Single Agent Patterns: Reflection and Self-Correction

Intent

Convert failure signals into improved subsequent attempts within the same episode, without weight updates.

Introduction

Even with careful planning and step-by-step logic, AI agents frequently generate incorrect code, trigger API validation failures, or encounter compilation issues. The Reflection and Self-Correction pattern offers an in-context learning mechanism to recover from these errors. By feeding detailed failure signals back to the model as verbal feedback, the agent learns to correct its mistakes dynamically in the next iteration—all without requiring expensive model weight training or fine-tuning.

Structure of the Pattern

The Reflection architecture establishes an iterative refinement cycle containing three main components:

PASS (Exit) FAIL (Critique) Condition Retry Generator LLM Synthesis Candidate Attempt Output Critic / Test Validation layer Feedback Episodic Memory Final Output

Formalized under research paradigms like **Reflexion** [1], which models self-correction as verbal reinforcement learning, and **Self-Refine** [2], which demonstrates feedback loops on single outputs, this pattern mimics the human development cycle: write code, run tests, read exceptions, and repair the logic.

Trade-offs

Although reflection cycles are highly effective for correcting concrete bugs, they introduce specific trade-offs:

Cheap & Training-Free

Enables model adaptation during inference. There is no need for costly parameter updates, fine-tuning, or retraining pipelines.

Grounded Verification

Highly robust when critiques are anchored in deterministic outputs, such as compilation stack traces, validation logs, or unit test failures.

Illusory Self-Critique

Without external grounding (e.g. asking the model to evaluate its own textual output), correction rates drop. Models struggle to accurately judge their own generation biases.

Context Window Bloat & Instability

Repeated failures rapidly fill the context window with long exception traces. Furthermore, if a model mistakes an intermittent network glitch for a structural bug, it may rewrite working code.

Known Uses

Reflection is a key tool in software engineering agents and coding assistants:

References