Agentic AITypeScriptverifiedVerified

Evaluator-Optimizer Agent Pattern in TypeScript

An iterative refinement loop where an 'Evaluator' provides granular feedback on an 'Optimizer’s' output until quality thresholds are met.

How to Implement the Evaluator-Optimizer Agent Pattern in TypeScript

1Step 1: Define the Feedback, Optimizer, and Evaluator interfaces

interface Feedback {
  isPass: boolean;
  critique: string;
  score: number;
}

interface Optimizer {
  generate(task: string): Promise<string>;
  refine(current: string, feedback: string): Promise<string>;
}

interface Evaluator {
  check(output: string): Promise<Feedback>;
}

2Step 2: Implement the iterative refinement loop

const MAX_ITERATIONS = 5;

async function refinementLoop(
  task: string,
  optimizer: Optimizer,
  evaluator: Evaluator
): Promise<string> {
  let currentOutput = await optimizer.generate(task);

  for (let i = 0; i < MAX_ITERATIONS; i++) {
    const feedback = await evaluator.check(currentOutput);

    if (feedback.isPass) return currentOutput;

    currentOutput = await optimizer.refine(
      currentOutput,
      feedback.critique
    );
  }

  return currentOutput;
}

Evaluator-Optimizer Agent Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Evaluator-Optimizer Agent Pattern in the Real World

Think of a student writing an essay (Optimizer) and a teacher grading it with detailed feedback (Evaluator). The student revises based on the red-ink comments and resubmits. This cycle repeats until the essay meets the teacher’s standards—or the deadline (max iterations) is reached.