Demonstrates intelligent request routing where incoming requests are dynamically routed to specialized agents based on content analysis. Perfect for handling diverse inputs that require different types of expertise.
- Intelligent Routing: Dynamic agent selection based on content
- Content Classification: Analyzing requests to determine expertise needed
- Specialist Agents: Domain-specific expert agents
- Fallback Handling: Graceful handling of unclassified requests
- Scalable Architecture: Easy addition of new routing rules and agents
router-pattern/
├── README.md # This guide
├── requirements.txt # Dependencies
├── .env.example # Environment template
├── main.py # Main application
└── docs/
└── routing_strategies.md # Advanced routing patterns
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env with your LLM settings
# Run the example
python main.py Input Request
│
▼
RequestRouter
(Analyzer)
│
┌────────────────┼────────────────┐
▼ ▼ ▼
TechnicalAgent CreativeAgent BusinessAgent
(Programming) (Writing) (Strategy)
│ │ │
└────────────────┼────────────────┘
▼
Final Response
- Content Analysis: RequestRouter analyzes incoming requests
- Dynamic Routing: Routes to appropriate specialist agent
- Expert Response: Specialist provides domain-specific expertise
- Fallback Support: GeneralAgent handles unclassified requests
# Input: "How do I optimize my database queries?"
#
# Step 1 - RequestRouter:
# → Analyzes: "database queries" + "optimize"
# → Classification: Technical/Programming
# → Routes to: TechnicalAgent
#
# Step 2 - TechnicalAgent:
# → Provides: Database optimization techniques
# → Includes: Code examples and best practices
# → Returns: Expert technical adviceAutomated routing with multiple example requests:
python main.py
# Select option 1See routing decisions for various request types:
python main.py
# Select option 2Chat with automatic intelligent routing:
python main.py
# Select option 3- Automatic expert selection
- Content-based routing decisions
- Optimal agent utilization
- Reduced response time
- Easy addition of new specialists
- Domain-specific knowledge
- Specialized system prompts
- Expert-level responses
- Fallback for unclassified requests
- Error recovery mechanisms
- Graceful degradation
- Comprehensive coverage
- Direct routing to experts
- Reduced processing overhead
- Efficient resource utilization
- Faster response times
@app.agent(
name="SpecialistAgent",
description="Expert in specific domain",
system_prompt="You are an expert in [domain]. Focus on..."
)
async def specialist_agent():
pass@app.agent(
name="RequestRouter",
system_prompt="""
Analyze requests and route to specialists:
- Technical questions → TechnicalAgent
- Creative tasks → CreativeAgent
- Business strategy → BusinessAgent
- Fallback → GeneralAgent
"""
)
async def request_router():
passasync def route_and_process(request: str):
async with app.run_context() as rt:
# Get routing decision
router_input = Message(role="user", content=request)
router_result = await rt.call_agent("RequestRouter", router_input)
selected_agent = router_result.current_message.content.strip()
# Call selected specialist
response = await rt.call_agent(selected_agent, router_input)
return response.current_message.content- Route inquiries to appropriate departments
- Technical, billing, sales specialists
- Escalation path management
- Automated triage systems
- Route content by type and complexity
- Writing, editing, technical documentation
- Multi-language content routing
- Quality-specific handling
- Route questions to domain experts
- Technical, business, market research
- Specialized analysis requirements
- Expert opinion aggregation
- Department-specific routing
- Expertise-based distribution
- Workflow optimization
- Resource allocation
@app.agent(
name="Level1Router",
system_prompt="Route to department: Tech, Business, Creative, or General"
)
@app.agent(
name="TechRouter",
system_prompt="Route technical requests: Backend, Frontend, DevOps, or Database"
)@app.agent(
name="ConfidenceRouter",
system_prompt="""
Route based on confidence:
- High confidence (90%+): Direct to specialist
- Medium confidence (70-90%): Route with fallback
- Low confidence (<70%): Route to GeneralAgent
"""
)@app.agent(
name="PriorityRouter",
system_prompt="""
Route based on urgency and importance:
- Critical: Route to senior specialists
- High: Route to regular specialists
- Normal: Route to appropriate agent
- Low: Queue for batch processing
"""
)- Train router on diverse examples
- Use clear routing criteria
- Implement feedback loops
- Monitor routing decisions
- Optimize routing agent prompts
- Cache routing decisions
- Parallel routing evaluation
- Fast specialist selection
- Balance specialist workloads
- Monitor agent performance
- Dynamic specialist scaling
- Expert availability tracking
# Track routing decisions
routing_log = []
async def debug_routing(request: str):
router_result = await rt.call_agent("RequestRouter", request)
selected_agent = router_result.current_message.content
routing_log.append({
"request": request,
"selected_agent": selected_agent,
"timestamp": datetime.now()
})# Analyze routing patterns
def analyze_routing_stats():
agent_usage = {}
for entry in routing_log:
agent = entry["selected_agent"]
agent_usage[agent] = agent_usage.get(agent, 0) + 1
print("Agent Usage Statistics:")
for agent, count in agent_usage.items():
print(f" {agent}: {count} requests")- Test with diverse request types
- Validate specialist responses
- Check routing consistency
- Monitor misclassification rates
After mastering router patterns:
- Try Discussion Patterns: Discussion Pattern Example
- Advanced Routing: Smart Home Orchestration
- Multi-Agent Systems: DevOps Incident Response
- Build Custom Routers: Create your own intelligent routing systems
- Clear Categories: Define distinct specialist domains
- Fallback Strategy: Always provide fallback options
- Routing Transparency: Make routing decisions visible
- Expert Quality: Ensure specialists are truly expert-level
- Start with 3-5 specialist categories
- Use clear, unambiguous routing criteria
- Test routing accuracy extensively
- Monitor and adjust routing rules
- Avoid overlapping specialist domains
- Don't over-complicate routing logic
- Ensure fallback agent is robust
- Monitor for routing bias
This example is provided under the MIT License.