Evaluator Optimizer A technical walkthrough details the Evaluator-Optimizer workflow, in which one LLM call generates a response and a second LLM call evaluates it and returns feedback in a loop until the output passes. The pattern is recommended when clear evaluation criteria exist and iterative refinement adds value, and the example implementation loops generate and evaluate calls until the evaluator returns "PASS", demonstrated on a task to implement a Stack with push(x), pop(), and getMin() in O(1) time. Evaluator-Optimizer Workflow Evaluator-Optimizer Workflow In this workflow, one LLM call generates a response while another provides evaluation and feedback in a loop. When to use this workflow When to use this workflow This workflow is particularly effective when we have: - Clear evaluation criteria - Value from iterative refinement The two signs of good fit are: - LLM responses can be demonstrably improved when feedback is provided - The LLM can provide meaningful feedback itself from util import extract xml, llm call def generate prompt: str, task: str, context: str = "" - tuple str, str : """Generate and improve a solution based on feedback.""" full prompt = f"{prompt}\n{context}\nTask: {task}" if context else f"{prompt}\nTask: {task}" response = llm call full prompt thoughts = extract xml response, "thoughts" result = extract xml response, "response" print "\n=== GENERATION START ===" print f"Thoughts:\n{thoughts}\n" print f"Generated:\n{result}" print "=== GENERATION END ===\n" return thoughts, result def evaluate prompt: str, content: str, task: str - tuple str, str : """Evaluate if a solution meets requirements.""" full prompt = f"{prompt}\nOriginal task: {task}\nContent to evaluate: {content}" response = llm call full prompt evaluation = extract xml response, "evaluation" feedback = extract xml response, "feedback" print "=== EVALUATION START ===" print f"Status: {evaluation}" print f"Feedback: {feedback}" print "=== EVALUATION END ===\n" return evaluation, feedback def loop task: str, evaluator prompt: str, generator prompt: str - tuple str, list dict : """Keep generating and evaluating until requirements are met.""" memory = chain of thought = thoughts, result = generate generator prompt, task memory.append result chain of thought.append {"thoughts": thoughts, "result": result} while True: evaluation, feedback = evaluate evaluator prompt, result, task if evaluation == "PASS": return result, chain of thought context = "\n".join "Previous attempts:", f"- {m}" for m in memory , f"\nFeedback: {feedback}" thoughts, result = generate generator prompt, task, context memory.append result chain of thought.append {"thoughts": thoughts, "result": result} Example Use Case: Iterative coding loop Example Use Case: Iterative coding loop evaluator prompt = """ Evaluate this following code implementation for: 1. code correctness 2. time complexity 3. style and best practices You should be evaluating only and not attemping to solve the task. Only output "PASS" if all criteria are met and you have no further suggestions for improvements. Output your evaluation concisely in the following format.