Lesson 7 of 9 · Patterns
Reflection
The reflection pattern for AI agents: draft, critique against clear criteria and revise. When critics should be code or a model, and how to avoid endless loops.
- 5 min read
- Intermediate
- Reviewed
- Explain the reflection pattern: generate, critique, revise
- Write a critic with clear, checkable criteria
- Decide when the critic should be code, a model or a test
- Limit reflection loops and know their cost
Draft, check, improve
People rarely get things right in one go: they write, reread, and fix. Agents can do the same.
The pattern has three parts:
- A generator produces a draft.
- A critic checks it against explicit criteria and returns specific problems.
- The generator revises using that feedback.
Reflection in action
The writer below is a scripted stand-in for a model; the critic is plain code with four requirements for a maintenance announcement:
# ---- Reflection: generate, critique against clear criteria, revise ----
DRAFTS = iter([
"Hi team, the server will be down.",
"Hi team, the billing server will be down on Saturday 21 Nov from 08:00 to 10:00 UTC for upgrades.",
("Hi team, the billing server will be down on Saturday 21 Nov from 08:00 to 10:00 UTC for upgrades. "
"Payments made in that window will be processed afterwards. Questions: #it-help."),
])
def writer(task, feedback):
"""Stands in for an LLM that writes (and rewrites, given feedback)."""
return next(DRAFTS)
def critic(text):
"""Checks a draft against the requirements. Here it's code; it can also be an LLM with a checklist."""
problems = []
if "Saturday" not in text and "Sunday" not in text:
problems.append("say which day")
if "UTC" not in text:
problems.append("give the time with a time zone")
if "#it-help" not in text:
problems.append("say where to ask questions")
if len(text.split()) > 60:
problems.append("keep it under 60 words")
return problems
task = "Announce the billing server maintenance to staff"
feedback = []
for round_number in range(1, 4):
draft = writer(task, feedback)
feedback = critic(draft)
print(f"Round {round_number}: {draft[:70]}{'...' if len(draft) > 70 else ''}")
print(" critic:", feedback or "all requirements met")
if not feedback:
breakRound 1 missed three requirements; round 2 fixed two; round 3 passed. The critic's feedback was specific (“give the time with a time zone”), which is what makes revision work. “Make it better” gives the generator nothing to act on.
Choosing the critic
| Critic | Good for | Example |
|---|---|---|
| Code checks | Things you can test exactly | Required fields, length, format, valid JSON |
| Tests and tools | Correctness you can run | Unit tests for code, a SQL query that must return rows |
| A model with a checklist | Judgement that's hard to code | “Does the reply answer every question in the email? Is the tone polite?” |
| A person | High-stakes or subjective decisions | Legal wording, final approval |
The strongest critics are external: tests that run, data that can be checked. A model critiquing its own output with no new information tends to approve it, or to make changes that aren't improvements.
Common mistakes
Write the critic for a reflection loop: a checklist the draft must pass.
def critic(text):
# Return a list of problems with a product description:
# "mention the price" if there's no "€" in the text
# "too long" if it has more than 25 words
# "no call to action" if it doesn't contain "Order"
return []
print(critic("A warm wool scarf in five colours."))
print(critic("A warm wool scarf in five colours, €29. Order today for delivery by Friday."))
What makes a critic most useful in a reflection loop?
Summary
- Reflection: generate, critique, revise, until the output passes or a limit is reached.
- Critics should give specific, checkable feedback.
- Prefer external critics (code checks, tests, data); use a model with a checklist for judgement calls.
- Limit rounds and measure whether reflection pays for its extra cost.
Next: routing requests to the right specialist.
Finished reading? Tick it off to track your progress.