Contents

Evaluator–Optimizer

Also known as: Reflection, generator–critic loop, writer–reviewer loop

Who decides
Code
Shape
Loop

Intent

One call writes a candidate, and a second call judges it against criteria you wrote down and says what to fix. The writer revises, and your code repeats the round until the judge passes the work or a stop rule ends the loop.

Problem

You are building a coding assistant. A student asks: “Write a Python MinStack class with push, pop, top and get_min, each in O(1).”

The obvious design is one call to the model. It writes the class once, in a single pass, and stops when the output looks finished. The core idea comes back right: a second stack that tracks the minimum. The gaps come with it:

What is missing is a step that checks the draft against the requirements and sends it back with specific feedback.

A task goes to one LLM call, which returns a first draft with three gaps; a person has to find them by hand.Taskwrite MinStackOne LLM callwrites once, stopsFirst draftmin stack idea is correctpop() on empty returns Noneno docstrings or type hintsno input validationYou find the gaps by hand
Scroll to see all of it.
One call writes once and stops; the gaps in its draft are left for you to find.

Solution

Wrap the writing in a loop with a reviewer. A generator writes the candidate; an evaluator compares it with an explicit list of criteria and says what is wrong, where, and how to fix it. Plain code runs the rounds and decides when the loop ends.

On the MinStack task, a run looks like this:

  1. Generate. The generator gets the task and the full list of criteria: O(1) operations, a clear exception on an empty stack, type hints and docstrings, input validation. It writes a first draft.
  2. Check. Your code runs the tests and a type checker before any judge. Two tests fail because pop() returns None instead of raising.
  3. Evaluate. The evaluator receives the task, the criteria, the draft and the raw test output. It returns the verdict REVISE and a list of issues, such as “pop() returns None on an empty stack; raise IndexError” and “no docstrings or type hints”.
  4. Decide. Code, not a model, applies the stop rule. The verdict is not a pass and the cap of three rounds is not reached, so the loop goes on.
  5. Revise. The generator gets the task, its last draft and the feedback, and writes a full new version. The tests pass, the evaluator finds no issue against the criteria, and it returns PASS.
  6. Return. Code ends the loop on the pass and returns the class to the student, with no gap left to find by hand.

The nearest pattern, ReAct, also loops, but there the model picks its next action freely; here the shape (generate, check, evaluate, revise) is fixed in code, which makes this a workflow.

Structure

The task goes to the generator; its candidate passes through deterministic checks to the evaluator; the evaluator's verdict goes to a stop rule in code, which either sends feedback back to the generator or returns the best candidate.taskcandidatecandidate+ evidenceverdictpass / caprevise: feedback + historyTask+ criteriaGeneratorwrites, revisesDeterministic checkstests, compiler, schemaEvaluatorverdict + issuesStop rulein codeOutputbest so far123456
Scroll to see all of it.

Hover over a number, or tap it, to read what it is.

Solid arrows carry the work forward; the dashed accent arrow is the revision loop. The deterministic checks are optional, but they give the evaluator evidence it can trust.
  1. Generator. Writes the first draft and every revision. Its prompt holds every requirement the evaluator will check; on a revision it also gets its last attempt and the feedback.
  2. Deterministic checks. Tests, compilers, type checkers, linters or schema validators. They run before the judge, and their raw output is passed along as evidence.
  3. Evaluator. A model with a rubric, or a rule. It returns a verdict your code can branch on and a list of issues, each with where it is and how to fix it. It judges; it does not rewrite.
  4. Stop rule. Plain code that ends the loop on a pass, an iteration cap, a round with no improvement, or a token or time budget.
  5. Feedback and history. What goes back to the generator: the latest candidate and its issues in full, earlier rounds as one-line summaries so the context stays small.
  6. Output. The best candidate seen so far, which is not always the last one. If the loop ends without a pass, mark it as unchecked or hand it to a person.

Use it when / Avoid it when

Use it when

Avoid it when

How it fails

Self-correction breaks right answers

With no outside signal, the model second-guesses answers that were correct: Huang et al. measured GPT-3.5-Turbo on CommonSenseQA falling from 75.8 to 38.1 after one round of self-correction.

Mitigation: ground the evaluator in tests, tools or retrieval, and do not loop on reasoning tasks when none exist.

Rubber-stamping

The evaluator says the output looks good when it is wrong, so the loop passes on the first round and fixes nothing.

Mitigation: give the evaluator check output as evidence and run it in a fresh context or on a different model.

Nitpicking

An evaluator asked to find problems always finds some: the output grows with every round and the loop never reaches a pass.

Mitigation: limit it to the stated criteria, loop only on blocking issues, and allow a pass on the first round.

Gaming the checker

The generator satisfies the checks instead of the task, for example by hardcoding expected outputs or editing the tests.

Mitigation: make the tests read-only, and give the generator a way to report that the tests contradict the task.

Related patterns

Try it

Describe what the loop should produce, its criteria and the checks your code can run. The generator writes two system prompts, one for the generator and one for the evaluator, with shared criteria, plus the loop rules.

Further reading