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:
- Edge cases are wrong.
pop()on an empty stack returnsNoneinstead of raising an error, so the bug shows up later, far from its cause. - Requirements are silently dropped. The draft has no docstrings, no type hints and no input validation, although a careful prompt asked for all three.
- You become the reviewer. Nothing in the design compares the draft with the request, so you read the code, run it and find each gap by hand.
What is missing is a step that checks the draft against the requirements and sends it back with specific feedback.
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:
- 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.
- Check. Your code runs the tests and a type checker before any judge. Two tests fail because
pop()returnsNoneinstead of raising. - Evaluate. The evaluator receives the task, the criteria, the draft and the raw test output. It returns the verdict
REVISEand a list of issues, such as “pop()returnsNoneon an empty stack; raiseIndexError” and “no docstrings or type hints”. - 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.
- 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. - 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
Hover over a number, or tap it, to read what it is.
- 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.
- Deterministic checks. Tests, compilers, type checkers, linters or schema validators. They run before the judge, and their raw output is passed along as evidence.
- 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.
- 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.
- 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.
- 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
- You can write the criteria down. A list of what a good output must do gives the evaluator something specific to check and the generator something specific to fix.
- An outside signal exists. Tests, compilers, schema validators or a search that confirms a claim give the evaluator facts to work from instead of opinion.
- One pass tends to drop requirements. Code, reports and constrained writing with many rules are where a reviewer reliably catches what the first draft missed.
- Quality matters more than speed. Each round adds at least two calls that must run one after the other.
Avoid it when
- The only feedback is the model rereading its own reasoning. On math or logic with no tools, sample several answers and vote with Parallelization instead.
- You cannot say what “better” means. Use a single call and review it yourself: without criteria the critic drifts toward taste and longer output.
- Latency is tight. Use one well-written call, or a prompt chain with a single check that passes or fails.
- A single good prompt already meets the bar. Use that single call, and add a loop only when it measurably helps.
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
- Prompt Chaining: a chain’s check gate only passes or fails; Evaluator–Optimizer is the version where a failed gate loops back to the writer with feedback.
- Parallelization: voting and best-of-N sample several candidates with no feedback, the cheaper baseline this loop has to beat at the same number of calls.
- ReAct: the model chooses its own actions, and coding agents often run a generate, test and fix loop inside it.
- Human-in-the-Loop: a person as the evaluator, or as the one who decides when the loop hits its cap without a pass.
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.
Open the generator on this pattern
Further reading
- Building effective agents (Anthropic, 2024): defines the evaluator–optimizer workflow and the two signs that a task fits it.
- Self-Refine: Iterative Refinement with Self-Feedback (Madaan et al., 2023): one model that writes, critiques and revises, with results per task.
- Large Language Models Cannot Self-Correct Reasoning Yet (Huang et al., 2024): why a loop without an outside signal can make reasoning answers worse.