Overview
Routing in switchAILocal determines which credential and provider handle each incoming request. The system supports multiple routing strategies, intelligent fallback, and per-model quota management.Routing Configuration
Configure routing behavior inconfig.yaml:
Routing Strategies
The routing strategy determines how multiple credentials for the same provider are selected.Round-Robin
Distributes requests evenly across all available credentials.How Round-Robin Works
How Round-Robin Works
The RoundRobinSelector (Behavior:
sdk/switchailocal/auth/selector.go) maintains per-model cursors:- First request to
gpt-4uses credential A - Second request to
gpt-4uses credential B - Third request to
gpt-4uses credential C - Fourth request to
gpt-4wraps back to credential A
Round-robin is tracked per model. Requests to
gpt-4 and gpt-3.5-turbo maintain independent cursors.Fill-First
Uses the first available credential until it’s exhausted or in cooldown, then moves to the next.How Fill-First Works
How Fill-First Works
The FillFirstSelector always picks the first available credential:Behavior:
- All requests use credential A
- When A hits quota → switch to credential B
- When B hits quota → switch to credential C
- When A recovers → switch back to A
Credential Selection Process
The Auth Manager follows a multi-step process to select credentials:1. Provider Matching
2. Model Support Filtering
3. Status Filtering
- Not disabled (
auth.Disabled == false) - Not unavailable (
auth.Unavailable == false) - Past retry time (
auth.NextRetryAfter < now) - Model-specific state (if tracked)
4. Strategy Application
The selected strategy picks one credential from the available pool:Multi-Provider Routing
You can specify multiple providers for the same model:gpt-4o:
- Manager tries OpenAI provider first
- If OpenAI is in cooldown → tries OpenRouter
- Rotates starting provider on next request
Per-model provider rotation ensures even distribution when multiple providers offer the same model.
Intelligent Routing (Cortex Phase 2)
When Intelligence is enabled, routing becomes content-aware:Classification Flow
Intent Classification
Intent Classification
The Intelligence Service uses the router model to classify requests:Intent mapping:
- coding: Code generation, debugging, refactoring
- reasoning: Complex problem-solving, math, logic
- fast: Simple queries, casual conversation
- secure: Privacy-sensitive, runs locally only
- vision: Image analysis, OCR, visual tasks
Quota Management
Quota tracking prevents retry storms when providers hit rate limits.Quota States
Each credential tracks quota status:Backoff Schedule
Exponential backoff prevents hammering rate-limited providers:Model-Level Quotas
Quotas are tracked per-model for fine-grained control:Retry Logic
Configurable retry behavior for transient failures:Retry Decision Logic
Retry Decision Logic
- Not the final attempt
- At least one credential will recover within
maxWait - Error is retryable (408, 429, 500, 502, 503, 504)
Fallback Chains
Automatic fallback when quota is exceeded:- Try next credential for same model
- Try preview model with same credential
- Try preview model with next credential
- Return cooldown error
Load Balancing
Distribute requests across providers:round-robin strategy:
- Request 1 → groq key-A
- Request 2 → groq key-B
- Request 3 → openrouter key-A
- Request 4 → openrouter key-B
- Request 5 → groq key-A (rotation)
Custom Selectors
Implement custom routing logic:Custom selectors receive only available credentials (already filtered by status and cooldown).
Next Steps
Authentication
Learn about credential lifecycle and refresh
Providers
Configure provider-specific settings
Intelligence
Enable semantic routing and classification
Configuration
Complete routing configuration reference