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
- generateThe generator writes a solution, guided by the task and any earlier feedback
- evaluateThe evaluator returns a verdict (PASS, NEEDS_IMPROVEMENT or FAIL) and feedback
- refineUntil the verdict is PASS, the feedback goes back to the generator
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
| Language | Java 17 or later |
|---|---|
| Framework | Spring Boot 4.1.1 with Spring AI |
| Models | Configurable through the Spring AI model starters (OpenAI, Anthropic, Ollama) |
| Output | Structured 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
EvaluatorOptimizer | Runs the generate, evaluate and refine loop and returns a RefinedResponse. |
|---|---|
Generation | Record with the generator's thoughts and response. |
EvaluationResponse | Record with the evaluation verdict and the feedback. |