Spring AI · Agentic patterns
Orchestrator-Workers Workflow
A central model reads the task, decides which subtasks are needed, and hands each one to a worker. A Spring AI example of the orchestrator-workers pattern from "Building Effective Agents".
The flow
- planThe orchestrator analyzes the task and returns subtasks as structured output
- executeEach subtask goes to a worker prompt with its type and guidelines
- assembleThe analysis and worker outputs are returned together
task → orchestrator → workers → result
Two roles
Orchestrator
Decides the subtasks at runtime. Its output maps to the OrchestratorResponse record.
Workers
Each one produces the content for a single subtask, guided by the original task and its own description.
Stack
| Language | Java 17 or later |
|---|---|
| Framework | Spring Boot 4.1.1 with Spring AI |
| Models | Anthropic Claude through the Spring AI model starter |
| Output | Structured orchestrator output mapped to a record |
Run it
export ANTHROPIC_API_KEY=your-key
./mvnw spring-boot:run
The demo asks the orchestrator to plan a product description for an eco-friendly water bottle, runs the workers, and prints the results.
Classes
OrchestratorWorkers | Runs the orchestrator, the worker calls, and returns a FinalResponse. |
|---|---|
Task | Record with the subtask type and description. |
Application | Wires the ChatClient and runs a sample task. |