Your First Router¶
Build a production-grade customer support router in 10 minutes.
What You'll Build¶
A customer support router that intelligently routes queries to three specialized agents:
- Billing — payments, invoices, refunds
- Technical Support — bugs, errors, crashes
- Sales — pricing, features, upgrades
By the end you'll have a working router making decisions in under 10ms.
Prerequisites¶
- Python 3.9+
pip install stratarouter openai
Step 1: Define Routes¶
from stratarouter import Router, Route
router = Router(dimension=384, threshold=0.5)
billing = Route(
id="billing",
description="Questions about payments, invoices, refunds, and billing",
keywords=["payment", "invoice", "refund", "charge", "bill"],
examples=[
"Where is my invoice?",
"I need a refund",
"Update my payment method",
"Cancel my subscription",
"Why was I charged twice?"
]
)
technical = Route(
id="technical_support",
description="Bug reports, errors, crashes, and technical issues",
keywords=["bug", "error", "crash", "broken", "not working"],
examples=[
"The app crashes on startup",
"Getting an error message",
"Feature X is not working",
"Error code 500 on dashboard"
]
)
sales = Route(
id="sales",
description="Questions about pricing, features, and purchasing",
keywords=["price", "cost", "buy", "upgrade", "plan", "feature"],
examples=[
"How much does it cost?",
"What features are included?",
"Can I upgrade my plan?",
"Enterprise pricing?"
]
)
router.add_route(billing)
router.add_route(technical)
router.add_route(sales)
Step 2: Build the Index¶
# Build index once at startup (handles embeddings automatically)
router.build_index()
print(f"Index built — {len(router.routes)} routes ready")
Step 3: Route Queries¶
def route_query(query: str):
result = router.route(query)
print(f"Query: '{query}'")
print(f"Route: {result.route_id}")
print(f"Confidence: {result.confidence:.2%}")
print(f"Latency: {result.latency_ms:.2f}ms\n")
return result
route_query("Where is my invoice from last month?")
route_query("The app keeps crashing when I login")
route_query("How much does the premium plan cost?")
Expected output:
Query: 'Where is my invoice from last month?'
Route: billing
Confidence: 94.23%
Latency: 1.23ms
Query: 'The app keeps crashing when I login'
Route: technical_support
Confidence: 96.71%
Latency: 0.87ms
Query: 'How much does the premium plan cost?'
Route: sales
Confidence: 92.15%
Latency: 1.05ms
Step 4: Connect to Handlers¶
class BillingAgent:
def handle(self, query):
return "I'll help with your billing question. Looking up your account..."
class TechnicalAgent:
def handle(self, query):
return "Let me help troubleshoot. Can you share the full error message?"
class SalesAgent:
def handle(self, query):
return "Happy to discuss pricing and features. What's your team size?"
AGENTS = {
"billing": BillingAgent(),
"technical_support": TechnicalAgent(),
"sales": SalesAgent()
}
def handle_customer_query(query: str):
result = router.route(query)
agent = AGENTS.get(result.route_id)
if agent:
response = agent.handle(query)
print(f"[{result.route_id.upper()}] {response}")
return response
return "No agent available."
Step 5: Add Fallback Logic¶
def handle_with_fallback(query: str, threshold: float = 0.7):
result = router.route(query)
if result.confidence < threshold:
print(f"Low confidence ({result.confidence:.2%}) — escalating to human")
return "ESCALATE_TO_HUMAN"
agent = AGENTS.get(result.route_id)
return agent.handle(query) if agent else "ESCALATE_TO_HUMAN"
handle_with_fallback("I need help") # Low confidence → escalate
handle_with_fallback("My payment failed") # High confidence → billing
What You've Learned¶
Routes
Created routes with descriptions, keywords, and examples.
Index Building
Built the HNSW index for fast similarity search.
Routing
Routed queries with confidence scores in milliseconds.
Fallbacks
Added fallback logic for low-confidence cases.