Overview
The Router component determines which LLM provider should handle each incoming request. Routing decisions are based on themodel or model_hint field in the request, allowing clients to control which provider is used.
Router Implementation
TheRouter class in app/core/router.py manages provider selection:
Model Hints
The router supports semantic hints that map to provider characteristics:Gemini Provider Hints
These hints route requests to Google’s Gemini (cloud-based):online- Routes to cloud-based providergemini- Explicitly selects Geminifast- Optimizes for speed (cloud providers are typically faster)
Ollama Provider Hints
These hints route requests to Ollama (local/self-hosted):local- Routes to self-hosted providerollama- Explicitly selects Ollamasecure- Prioritizes data privacy (local processing)
Routing Priority
The router follows this priority order:- Explicit
modelfield - If present, takes precedence overmodel_hint - Model hint - Semantic hint for provider selection
- Default fallback - Routes to Ollama if no hint is provided
The
model field takes precedence over model_hint, allowing clients to override hints with explicit provider selection.Routing Decision Flow
Provider List Return
Theroute() method returns a list of providers:
- Fallback chains - Try multiple providers in sequence
- Load balancing - Distribute requests across multiple instances
- A/B testing - Route requests to different providers for comparison
Integration with ChatService
TheChatService uses the router to get providers and iterates through them with retry logic:
app/core/service.py:55-67
Usage Examples
Request with Gemini Provider
Request with Ollama Provider
Default Routing (No Hint)
Provider Interface
All providers implement theLLMProvider base class interface, ensuring consistent behavior:
Adding New Providers
To add a new provider:- Create a new provider class implementing
LLMProvider - Register it in the
Router.__init__()providers dictionary - Add routing logic in
Router.route()with appropriate hints
Best Practices
Use semantic hints for flexibility
Use semantic hints for flexibility
Use hints like
fast, secure, local instead of explicit provider names. This allows you to change the underlying provider without updating client code.Set appropriate defaults
Set appropriate defaults
The default fallback routing ensures requests never fail due to missing hints. Choose a default that matches your primary use case.
Document provider characteristics
Document provider characteristics
Make sure clients understand what each hint means (speed, privacy, cost) so they can make informed routing decisions.
Next Steps
Architecture
Understand the full system architecture
Caching
Learn how responses are cached
Rate Limiting
Explore rate limiting implementation
API Reference
Complete API documentation