Skip to main content

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 in config.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.
The RoundRobinSelector (sdk/switchailocal/auth/selector.go) maintains per-model cursors:
Behavior:
  1. First request to gpt-4 uses credential A
  2. Second request to gpt-4 uses credential B
  3. Third request to gpt-4 uses credential C
  4. Fourth request to gpt-4 wraps back to credential A
Use case: Distribute load evenly, maximize quota utilization Example:
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.
The FillFirstSelector always picks the first available credential:
Behavior:
  1. All requests use credential A
  2. When A hits quota → switch to credential B
  3. When B hits quota → switch to credential C
  4. When A recovers → switch back to A
Use case: Stagger subscription caps, optimize for rolling time windows Example:
Fill-first works well with providers that have daily/monthly quotas rather than per-minute rate limits.

Credential Selection Process

The Auth Manager follows a multi-step process to select credentials:

1. Provider Matching

Provider names are normalized (lowercased, deduplicated).

2. Model Support Filtering

Credentials are filtered based on model support from the registry.

3. Status Filtering

Checks:
  • 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:
When both providers support gpt-4o:
  1. Manager tries OpenAI provider first
  2. If OpenAI is in cooldown → tries OpenRouter
  3. 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

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:
Set quota-exceeded.switch-project: true to automatically switch to another credential when quota is hit.

Model-Level Quotas

Quotas are tracked per-model for fine-grained control:
Behavior:

Retry Logic

Configurable retry behavior for transient failures:
Retry conditions:
  • 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:
Fallback order:
  1. Try next credential for same model
  2. Try preview model with same credential
  3. Try preview model with next credential
  4. Return cooldown error

Load Balancing

Distribute requests across providers:
With 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