Overview
TheModelRouter trait enables intelligent routing of requests to different LLM providers and models based on task complexity. It analyzes messages using four factors:
- Message length - Short messages → simple models, long messages → complex models
- Keyword detection - Emergency/medical terms → complex models, greetings → simple models
- Memory context - Whether memory context is needed
- Explicit complexity hints - Caller-specified complexity level
crates/oneclaw-core/src/orchestrator/router.rs
Complexity Enum
Defines the complexity level of a task for routing decisions.Complexity Levels
- Simple - Quick responses, greetings, status checks (≤5 words)
- Medium - Standard conversations with memory context
- Complex - Analysis, comparisons, reasoning tasks (keywords: “analyze”, “compare”, “why”, “explain”)
- Critical - Emergency/safety scenarios (keywords: “emergency”, “critical”, “urgent”, “danger”)
ModelChoice Struct
The result of a routing decision.ModelRouter Trait
Core trait for routing requests to appropriate models.Methods
route()
Location: router.rs:38
Selects a provider and model based on task complexity.
Parameters:
complexity: Complexity- The complexity level of the task
Result<ModelChoice>- The selected provider, model, and routing reason
DefaultRouter Implementation
Smart router that maps complexity levels to provider/model pairs. Location:router.rs:54-117
Constructor Methods
new()
Create with explicit route mapping.
from_config()
Location: router.rs:73-96
Create from configuration - maps complexity to configured providers.
Routing Logic
Location:router.rs:98-117
The router iterates through configured routes, matching on complexity level:
Complexity Analysis
Location:router.rs:120-149
Utility function to analyze messages and determine complexity.
- Critical keywords (highest priority): “emergency”, “critical”, “urgent”, “danger”, “alert”, “shutdown”, “failure”, “fatal”
- Complex keywords: “analyze”, “compare”, “why”, “explain”, “trend”, “diagnose”, “recommend”, “evaluate”
- Memory context: If memory context exists and message > 5 words → Medium
- Message length: ≤5 words → Simple, otherwise Medium
NoopRouter
Location:router.rs:42-51
No-operation router that always returns a placeholder.
Usage Examples
Basic Routing
Automatic Complexity Detection
Integration with Config
Future Enhancements
Sprint 7-8: Multi-provider routing strategy:- Simple → Local models (Ollama)
- Medium → Local models (Ollama with larger context)
- Complex → Cloud models (OpenAI)
- Critical → Best cloud model with verification
See Also
- ContextManager - Assembles prompts with memory context
- ChainExecutor - Multi-step LLM reasoning
- Provider - LLM provider abstraction