The Routing Pattern provides a standardized solution for building intelligent, context-aware agent systems by introducing conditional logic into an agent's operational framework.
---
title: Routing — Domain-Specific Handlers
---
%%{init: {'look':'handDrawn','theme':'base','themeVariables':{'background':'#f5ecd9','primaryColor':'#ede0bd','primaryBorderColor':'#6b4423','primaryTextColor':'#3e2723','lineColor':'#6b4423','clusterBkg':'#efe5cd','clusterBorder':'#c5b393','fontFamily':'Caveat, Patrick Hand, cursive'}}}%%
flowchart LR
Q([user request])
R([response])
Cls{classifier<br/>LLM}
subgraph handlers ["specialist handlers"]
B[booking_handler]
I[info_handler]
U[unclear_handler]
end
Q --> Cls
Cls -- "booking" --> B
Cls -- "info" --> I
Cls -- "unclear" --> U
B & I & U --> R
The pattern operates through three key steps:
- Analysis: The system first analyzes an incoming query to determine its intent or nature
- Decision: Based on this analysis, it dynamically directs the flow of control to the most appropriate specialized tool, function, or sub-agent
- Execution: The selected component handles the request using domain-specific logic
The routing decision can be driven by various approaches:
- LLM-based routing: Prompting language models to classify intent
- Rule-based routing: Applying predefined conditional logic
- Semantic routing: Using embedding-based similarity matching
- Hybrid approaches: Combining multiple methods for robust decision-making
- Flexibility: Transforms static, predetermined execution paths into dynamic workflows
- Context awareness: Selects the best possible action based on current state and input
- Scalability: Easily add new specialized handlers without modifying core logic
- Maintainability: Separates routing logic from handler implementation
Rule of Thumb: Use the Routing pattern when an agent must decide between multiple distinct workflows, tools, or sub-agents based on user input or current state.
- Customer support systems: Distinguish between sales inquiries, technical support, and account management
- Multi-domain chatbots: Route queries to specialized knowledge bases (HR, IT, Finance)
- Intent-based workflows: Direct users to booking, information retrieval, or troubleshooting flows
- Triage systems: Classify and prioritize incoming requests by urgency or category
- API orchestration: Select the appropriate backend service based on request type
- Single-purpose agents with one clear workflow
- Simple sequential processing without branching logic
- Systems where all requests follow identical paths
- Classification accuracy: Ensure routing decisions are reliable and well-tested
- Fallback handling: Always include a default path for unclear or ambiguous inputs
- Performance: Consider caching routing decisions for similar queries
- Observability: Log routing decisions for debugging and optimization
- Error handling: Gracefully handle failures in individual handlers
User Input → Router (Intent Classification) → Handler Selection → Execution → Response
↓
[Booking Handler]
[Info Handler]
[Support Handler]
[Default Handler]
- Chain of Thought: For sequential reasoning before routing
- Tool Use: For executing specialized actions after routing
- Orchestration: For coordinating multiple agents post-routing
This pattern is essential for building sophisticated agent systems that can intelligently handle diverse user needs.
If you're behind a corporate SSL-inspecting proxy, run examples with:
AGENTIC_DISABLE_SSL=1 bash run.sh