Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 39 ยท Planning and Reasoning

Reflection and Self-Critique

Let agents review and improve their own work: generate, critique, revise loops, external verifiers, reflection memory, and the limits of self-checking, with Python demos.

A first draft is rarely the best one, for people or for models. Reflection gives an agent a chance to look at what it produced, find problems and try again. The simplest form is a three-part loop: generate an answer, critique it, revise it. Used well, reflection catches missing requirements, wrong formats and failed tests. Used carelessly, it burns tokens while the model politely agrees with itself.

The generate, critique, revise loop

flowchart LR
  G["Generate"] --> C["Critique"]
  C --> D{"Good enough?"}
  D -- "no" --> R["Revise"]
  R --> C
  D -- "yes" --> O["Output"]

The loop ends when the critic accepts, or when the attempt budget runs out.

The critic can be the same model with a different prompt ("You are a strict reviewer. List concrete problems against this checklist"), a different model, a piece of ordinary code, or a human. The most important design choice is what the critic checks against.

Self-critique against a checklist

Vague prompts like "is this good?" make a model say yes. Give the critic explicit criteria and ask for a verdict plus specific issues.

TEXT
Review the draft against these requirements:
1. Under 80 words.   2. Mentions the refund deadline.   3. No promises about compensation.
Reply as JSON: {"passes": true|false, "issues": ["..."]}

Below, the critic is plain Python that checks the same kind of rules, and the "writer" is a scripted stand-in that improves when it receives feedback. The loop around them is the real structure.

Python A revise loop with a rule-based critic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice three safeguards: a maximum number of rounds, feedback that is specific and actionable, and a clear pass condition.

Prefer outside evidence over opinion

A model reviewing its own text only has its own judgment to rely on, which shares its blind spots. Reflection is much stronger when the critique comes from something that can actually check:

  • Tests: run the generated code against unit tests and feed failures back.
  • Validators: check JSON against a schema, SQL with EXPLAIN, links with an HTTP request.
  • Tools: recompute numbers with a calculator, verify quotes against the source text.
  • Humans: review high-stakes output.
Python Reflection driven by tests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The test failures are better feedback than any "please check your work" prompt, because they name exact inputs and outputs. This is how coding agents improve results: run, read the error, fix, run again. See the code review agent project.

Executing model-written code with exec is only acceptable in a sandbox like this browser runner. In production, run untrusted code in an isolated container with no secrets and no network, as discussed in executing tool calls safely.

Reflection memory

Some agents keep short written lessons from past failures ("the date format must be ISO; the API rejects trailing slashes") and include them in later prompts. This makes trial-and-error improve across attempts, and across sessions if stored in long-term memory. Keep these notes short and specific, and expire the ones that stop being true.

Limits and pitfalls

  • Sycophancy: a model asked to critique its own answer often approves it. Counter with explicit checklists, a separate reviewer prompt, or a different model.
  • Rewriting good answers into worse ones: more rounds are not always better. Stop when the critic passes.
  • Cost and latency: each round is at least one more model call. Set a budget, as in stop conditions.
  • Reflecting on the wrong thing: the critic should check the requirements that matter, not style trivia.
  • No ground truth: for open questions without a checker, reflection improves polish more than correctness.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three parts of a basic reflection loop?

    The agent produces an answer, reviews it and improves it until it passes or the budget ends.
  2. 2. Why is a unit-test failure better feedback than 'check your work'?

    External, specific evidence guides the fix far better than a general request.
  3. 3. Which safeguard prevents endless reflection?

    A round or budget limit ends the loop even if the critic is never satisfied.
  4. 4. What is sycophancy in self-critique?

    Self-review without clear criteria tends to agree with the draft.

Summary

  • Reflection is a generate, critique, revise loop that stops on a pass or a round limit.
  • Give critics explicit criteria and request structured verdicts with concrete issues.
  • External checks (tests, validators, calculators, humans) beat self-opinion.
  • Short reflection notes can carry lessons across attempts.
  • Beware sycophancy, runaway cost and polishing without ground truth.

Next: Replanning and Recovering from Failure