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

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

LanguageJava 17 or later
FrameworkSpring Boot 4.1.1 with Spring AI
ModelsAnthropic Claude through the Spring AI model starter
OutputStructured 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

OrchestratorWorkersRuns the orchestrator, the worker calls, and returns a FinalResponse.
TaskRecord with the subtask type and description.
ApplicationWires the ChatClient and runs a sample task.