Spring AI · Agentic patterns

Evaluator-Optimizer Workflow

One model generates a solution, another evaluates it against explicit criteria, and the feedback drives the next attempt. A Spring AI example of the evaluator-optimizer pattern from "Building Effective Agents".

The loop

task → generator → evaluator → PASS or refine

Two roles

Generator

Produces the solution as a Generation record with its reasoning and the code.

Evaluator

Judges the solution against the criteria in its prompt and returns an EvaluationResponse.

Stack

LanguageJava 17 or later
FrameworkSpring Boot 4.1.1 with Spring AI
ModelsConfigurable through the Spring AI model starters (OpenAI, Anthropic, Ollama)
OutputStructured generation and evaluation mapped to records

Run it

export OPENAI_API_KEY=your-key
./mvnw spring-boot:run

The demo asks the generator for a Java stack with O(1) operations and prints each round of generation and evaluation.

Classes

EvaluatorOptimizerRuns the generate, evaluate and refine loop and returns a RefinedResponse.
GenerationRecord with the generator's thoughts and response.
EvaluationResponseRecord with the evaluation verdict and the feedback.