Reflection & Self-critique

Agentic AI Systems advanced Building with AI 11 min read

Overview

Reflection and self-critique is the mechanism by which AI agents evaluate their own outputs, identify errors or shortcomings, and autonomously retry or refine their approach. It transforms a “one-shot” LLM call into an iterative improvement loop — the agent generates an initial response, critiques it against the original objective and known constraints, and produces an improved version. This is the AI equivalent of a developer reviewing their own code before submitting a pull request.

The core insight behind reflection is that LLMs are often better at evaluating text than generating it on the first attempt. A model that fails to write correct code 40% of the time might successfully identify bugs in existing code 90% of the time. Reflection exploits this asymmetry by turning the model’s evaluation capability into a self-improvement loop.

For production AI systems, reflection is critical for reliability. Without it, agents propagate first-attempt errors through entire workflows. With reflection, agents can catch and correct mistakes before they cascade — reducing hallucination rates, improving code correctness, and increasing the overall success rate of multi-step tasks.

How It Works

The Basic Reflection Loop

At its simplest, reflection adds a second LLM call after every generation step. The first call produces output; the second call critiques it. If the critique identifies problems, the original output is regenerated with the critique as additional context.

// Simple reflection pattern
async function reflectiveGenerate(task, maxRetries = 3) {
  let output = await llm.generate(task);

  for (let i = 0; i < maxRetries; i++) {
    const critique = await llm.evaluate({
      task: task,
      output: output,
      prompt: "Identify any errors, omissions, or quality issues."
    });

    if (critique.passesQuality) break;

    output = await llm.generate({
      task: task,
      previousAttempt: output,
      feedback: critique.issues,
      prompt: "Fix these issues while preserving correct parts."
    });
  }
  return output;
}

The Reflexion Framework

Reflexion, introduced by Shinn et al. (NeurIPS 2023), formalizes reflection as “verbal reinforcement learning.” Instead of updating model weights (as in traditional RL), Reflexion stores natural-language feedback in an episodic memory buffer that persists across attempts. This feedback acts as a “semantic gradient” — telling the agent not just that it failed, but why and how to improve.

Reflexion Architecture

Three components work together: (1) an Actor (Ma) that generates actions and text, (2) an Evaluator (Me) that scores the Actor’s output (using unit tests, environment feedback, or LLM-as-judge), and (3) a Self-Reflection model (Msr) that converts evaluation signals into verbal feedback stored in memory. On the next attempt, the Actor receives its prior reflections as additional context.

The key innovation is the memory buffer. Traditional retry loops discard all information between attempts — the agent starts fresh each time. Reflexion preserves the agent’s “lessons learned” as natural language, which compounds over multiple attempts. The agent doesn’t just retry; it retries with knowledge of what went wrong before.

Types of Feedback Signals

Reflection systems can use different types of feedback to drive improvement:

Internal vs. External Evaluation

A critical design choice is whether the evaluator is the same model (internal) or a different system (external). Internal self-critique uses the same LLM to both generate and evaluate — this is simpler but can suffer from “blind spots” where the model consistently fails to catch its own errors. External evaluation uses unit tests, type checkers, linters, or a separate stronger model as the critic, providing more reliable feedback.

The Evaluation Asymmetry

Research shows that self-reflection consistently improves LLM agent performance across all models tested. However, the improvement is bounded by the model’s ability to accurately evaluate its own output. Using external validators (tests, type checkers, formal verification) provides stronger guarantees than pure self-critique.

Multi-Agent Reflection

Multi-Agent Reflexion (MAR) replaces single-agent self-critique with a structured debate among multiple persona-based critics. For example, one critic might focus on correctness, another on efficiency, and a third on edge cases. The diverse perspectives generate richer reflections that more effectively guide the agent toward improved strategies, similar to a code review by multiple team members with different expertise.

When to Reflect

Not every action warrants reflection — it doubles (or more) the token cost and latency. Effective systems use gating heuristics to decide when reflection is warranted:

Practical Applications

Self-Correcting Code Agents

Code generation agents are the most natural fit for reflection because they have access to strong external validators: compilers, test suites, and type checkers. An agent generates code, runs the tests, and if they fail, reflects on the error messages to produce a corrected version. Reflexion-based coding agents achieve significantly higher pass rates on benchmarks like HumanEval compared to single-attempt generation — often going from ~60% to ~90% pass rates within 3-4 reflection cycles.

Fact-Checking and Hallucination Reduction

Reflection can be used to reduce hallucination in RAG pipelines. After generating a response grounded in retrieved documents, a second pass checks each claim against the source material. Claims that cannot be traced back to a specific retrieved chunk are flagged for revision or removal. This self-verification step is far cheaper than deploying a separate fact-checking model.

When an initial search query returns poor results, reflective agents analyze why the results were unsatisfying and reformulate the query. For example, if a semantic search for “Apple revenue” returns results about fruit, the agent might reflect: “The query was ambiguous. I should add ‘Inc.’ or ‘AAPL’ to disambiguate.” This reflective query refinement is a core component of agentic search systems.

Production Guardrails

In production systems, reflection serves as a quality gate before outputs reach users. An agent generates a customer-facing response, then a reflection step checks for: PII leakage, policy violations, factual consistency, tone appropriateness, and completeness. Only responses that pass all checks are delivered. This pattern is used extensively in enterprise chatbots and automated content generation.

Key Takeaways

Further Reading