Routing Workflow Pattern
This project implements the Routing workflow pattern as described in Building Effective Agents by Anthropic. The pattern enables intelligent routing of inputs to specialized handlers based on content classification.
Overview
The Routing workflow pattern is designed for complex tasks where different types of inputs are better handled by specialized processes. It uses an LLM to analyze input content and route it to the most appropriate specialized prompt or handler.
Key Benefits
- Separation of Concerns: Each route can be optimized for specific types of input
- Improved Accuracy: Specialized prompts handle what they do best
- Scalable Architecture: Easy to add new routes and specialized handlers
- Better Resource Utilization: Route simpler tasks to lighter models
When to Use
This workflow is particularly effective when:
- You have distinct categories of input that require different handling
- Classification can be handled accurately by an LLM
- Different types of input require different expertise or processing approaches
Usage Example
@Autowired
private ChatClient chatClient;
// Create the workflow
RoutingWorkflow workflow = new RoutingWorkflow(chatClient);
// Define specialized prompts for different types of input
Map<String, String> routes = Map.of(
"billing", "You are a billing specialist. Help resolve billing issues...",
"technical", "You are a technical support engineer. Help solve technical problems...",
"general", "You are a customer service representative. Help with general inquiries..."
);
// Process input
String input = "My account was charged twice last week";
String response = workflow.route(input, routes);
Common Use Cases
-
Customer Support
- Route queries to appropriate departments (billing, technical, general)
- Direct urgent issues to priority handling
- Forward complex cases to specialist teams
-
Content Moderation
- Route content to appropriate review processes
- Direct sensitive content to human moderators
- Send routine content through automated checks
-
Query Optimization
- Route simple questions to smaller, faster models
- Direct complex queries to more capable models
- Forward specialized topics to domain-specific handlers
Implementation Details
The implementation consists of two main components:
-
RoutingWorkflow: The main class that implements the routing logic- Analyzes input using LLM
- Selects appropriate route
- Processes input with specialized prompt
-
RouteResponse: Record class that encapsulates routing decisions- Stores reasoning behind route selection
- Maintains selected route information
References
- Building Effective Agents - Anthropic Research
- Spring AI Documentation
