Classification-Based Routing
Classification is one of the most common ways to route items through different processing paths. This lesson covers techniques for reliable classification-based routing in prompt chains.
The Classification-Route Pattern
Input → Classify → Route → Process (Path A, B, or C) → Output
This pattern separates the "what is this?" decision from the "how should we handle it?" logic.
Designing Classification Steps
Clear Category Definitions
Define categories precisely to avoid ambiguity:
Loading Prompt Playground...
Multi-Label Classification
When items can belong to multiple categories:
const multiLabelResult = {
labels: [
{ category: 'technical', confidence: 0.85 },
{ category: 'billing', confidence: 0.45 },
{ category: 'urgent', confidence: 0.92 }
],
primaryLabel: 'technical',
flags: ['urgent'] // Labels that act as modifiers
};
Hierarchical Classification
When categories have subcategories:
Category: Support
└── Subcategory: Technical
└── Type: Bug Report
└── Component: Authentication
Loading Prompt Playground...
Routing Strategies
Direct Mapping
Simple one-to-one category to handler:
const routingTable = {
'technical': handleTechnical,
'billing': handleBilling,
'sales': handleSales,
'feedback': handleFeedback
};
async function route(classification) {
const handler = routingTable[classification.category];
if (!handler) {
return routingTable['default'];
}
return handler;
}
Rule-Based Routing
Complex rules combining multiple factors:
const routingRules = [
{
name: 'vip_urgent',
conditions: {
customerTier: 'enterprise',
urgency: ['high', 'critical']
},
destination: 'vip_escalation_queue'
},
{
name: 'technical_with_logs',
conditions: {
category: 'technical',
hasAttachments: true,
attachmentTypes: ['log', 'screenshot']
},
destination: 'engineering_triage'
},
// More rules...
];
Score-Based Routing
Route based on combined scoring:
async function scoreBasedRoute(classification, metadata) {
const scores = {
automation: calculateAutomationScore(classification, metadata),
humanReview: calculateHumanScore(classification, metadata),
escalation: calculateEscalationScore(classification, metadata)
};
// Route to highest scoring destination
const destination = Object.entries(scores)
.sort(([, a], [, b]) => b - a)[0][0];
return destination;
}
Handling Classification Uncertainty
Confidence Thresholds
async function routeWithConfidence(classification) {
if (classification.confidence >= 0.9) {
// High confidence: Proceed automatically
return { action: 'auto', path: classification.category };
}
if (classification.confidence >= 0.7) {
// Medium confidence: Proceed with logging
return { action: 'auto_logged', path: classification.category };
}
if (classification.confidence >= 0.5) {
// Low confidence: Suggest but verify
return { action: 'suggest', paths: [classification.category, classification.secondChoice] };
}
// Very low confidence: Human review
return { action: 'human_review', reason: 'Low classification confidence' };
}
Reclassification Strategies
When initial classification is uncertain:
Loading Prompt Playground...
Multi-Stage Classification
Coarse-to-Fine Classification
Start broad, then refine:
async function coarseToFineClassify(input) {
// Stage 1: Broad categories
const coarse = await classifyCoarse(input);
// Returns: 'support', 'sales', 'other'
// Stage 2: Fine categories within the broad category
const fine = await classifyFine(input, coarse.category);
// Returns specific subcategory based on coarse result
return {
coarse: coarse.category,
fine: fine.category,
combinedConfidence: coarse.confidence * fine.confidence
};
}
Ensemble Classification
Combine multiple classifiers:
async function ensembleClassify(input) {
// Run multiple classification approaches
const results = await Promise.all([
keywordClassifier(input),
semanticClassifier(input),
patternClassifier(input)
]);
// Combine results using voting or weighted average
const combined = combineClassifications(results, {
weights: [0.2, 0.5, 0.3] // Semantic classifier weighted highest
});
return combined;
}
Building Robust Classification Prompts
Include Examples
Few-shot classification is more reliable:
Loading Prompt Playground...
Handle Ambiguity Explicitly
const classificationPrompt = `
Classify this message. If it clearly fits one category, return that category.
If it could fit multiple categories, return all that apply with confidence scores.
If it doesn't fit any category well, return 'uncertain' with an explanation.
Categories: ${categories.join(', ')}
Message: "${input}"
`;
Exercise: Build a Classification Router
Design a complete classification-routing system:
Loading Prompt Playground...
Key Takeaways
- Classification-based routing separates categorization from processing
- Define categories precisely with clear criteria
- Support multi-label and hierarchical classification when needed
- Use confidence thresholds to handle uncertainty
- Consider coarse-to-fine or ensemble approaches for better accuracy
- Include examples in classification prompts for reliability
- Always have fallback paths for unclassified items
Next, we'll explore dynamic chain construction.

