Agentic AIC++verifiedVerified

Evaluator-Optimizer Agent Pattern in C++

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 C++

1Step 1: Define the Feedback struct and callable types

struct Feedback {
    bool isPass;
    std::string critique;
    double score;
};

using Optimizer = std::function<std::string(const std::string&)>;
using Refiner   = std::function<std::string(const std::string&, const std::string&)>;
using Evaluator = std::function<Feedback(const std::string&)>;

2Step 2: Implement the iterative refinement loop

constexpr int MAX_ITERATIONS = 5;

std::string refinementLoop(const std::string& task,
                           Optimizer generate,
                           Refiner refine,
                           Evaluator evaluate) {
    std::string current = generate(task);

    for (int i = 0; i < MAX_ITERATIONS; ++i) {
        Feedback fb = evaluate(current);
        if (fb.isPass) return current;
        current = refine(current, fb.critique);
    }

    return current;
}

3Step 3: Demonstrate the loop with stubs

int main() {
    auto result = refinementLoop(
        "Write a haiku",
        [](const std::string& task) { return "first draft of " + task; },
        [](const std::string& cur, const std::string& fb) {
            return cur + " [refined: " + fb + "]";
        },
        [n = 0](const std::string&) mutable -> Feedback {
            return {++n >= 3, "needs more detail", static_cast<double>(n) * 30.0};
        }
    );
    std::cout << "Final: " << result << "\n";
}

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.