Spring AI · Agentic patterns

Routing Workflow

Classify an input first, then answer it with a prompt written for that category. A Spring AI example of the routing pattern from "Building Effective Agents".

The routing

input → classifier → one route → answer

Two variations

Classifier

The model reads the input and returns its reasoning and one route name, as structured output.

Handlers

Each route has its own prompt. The router sends the input to the one the classifier chose.

Stack

LanguageJava 17 or later
FrameworkSpring Boot 4.1.1 with Spring AI
ModelsAnthropic Claude through the Spring AI model starter
OutputStructured classifier output mapped to a record

Run it

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

The demo runs three sample support tickets through the router and prints each answer.

Classes

RoutingWorkflowClassifies the input and runs the selected route's prompt.
RoutingResponseRecord with reasoning and selection.
ApplicationDefines the four routes and runs the sample tickets.