Skip to main content

Overview

CLI Proxy API intelligently routes requests across multiple credentials to maximize availability and balance load. The routing system handles:
  • Credential selection - Choosing which account to use
  • Load balancing - Distributing requests evenly
  • Quota management - Handling rate limits and daily quotas
  • Automatic failover - Retrying with different credentials
  • Model aliasing - Mapping model names

Routing Strategies

Two built-in strategies control credential selection:

Round-Robin (Default)

Distributes requests evenly across all available credentials:
config.yaml
How it works:
Best for:
  • Even distribution across accounts
  • Maximizing total quota usage
  • Avoiding concentration on single account

Fill-First

Uses the first credential until it hits quota, then moves to next:
config.yaml
How it works:
Best for:
  • Staggering rolling-window limits (e.g., chat message caps)
  • Minimizing active accounts
  • Preserving specific accounts for peak times

Credential States

Each credential can be in one of four states:

Ready

Credential is available and will be selected by routing strategy.

Cooldown

Credential exceeded quota and is temporarily blocked:
sdk/cliproxy/auth/conductor.go
Cooldown behavior:
  1. Detect quota error (HTTP 429 or provider-specific message)
  2. Calculate backoff using exponential strategy:
  3. Enter cooldown for calculated duration
  4. Return to ready after cooldown expires
Example cooldown sequence:

Blocked

Manually blocked via Management API or attributes.

Disabled

Permanently disabled (e.g., deleted auth file).

Priority-Based Selection

Credentials can have priority levels:
~/.cli-proxy-api/gemini_oauth_high-priority@gmail.com.json
~/.cli-proxy-api/gemini_oauth_low-priority@gmail.com.json
Selection order:
  1. Priority 10 accounts selected first
  2. Priority 1 accounts used as fallback
  3. Priority 0 (default) used last
Round-robin operates within each priority level:

Model Prefix Routing

Force specific credentials using model prefixes:

Configuring Prefixes

config.yaml

Using Prefixes

Force Prefix Mode

Require prefixes for all requests:
config.yaml
When enabled, unprefixed requests only use credentials without a prefix.

Model Aliasing

Map client model names to provider model names:

Global OAuth Aliases

config.yaml
fork: false (default):
fork: true:

API Key Aliases

config.yaml

Model Pools (Internal Failover)

Map multiple upstream models to the same alias:
config.yaml
Behavior:
  1. Client requests best-model
  2. Round-robin selects: claude-3.5-sonnet
  3. If fails before producing output → retry with gemini-pro
  4. If fails again → retry with gpt-4
  5. If all fail → return error

Model Exclusion

Hide models from the model list:

OAuth Exclusions

config.yaml

API Key Exclusions

config.yaml
Wildcard patterns:
  • model-name - Exact match
  • prefix-* - Matches prefix-anything
  • *-suffix - Matches anything-suffix
  • *substring* - Matches any-substring-here

Automatic Failover

When a request fails, CLI Proxy API automatically retries:

Retry Configuration

config.yaml

Retry Logic

Retry flow:
  1. Attempt 1: Credential A → Fails (quota exceeded)
  2. Attempt 2: Credential B → Fails (503 error)
  3. Attempt 3: Credential C → Fails (timeout)
  4. Attempt 4: Credential D → Success ✓
Retry conditions: Retries occur for these HTTP status codes:
  • 403 - Forbidden
  • 408 - Request Timeout
  • 429 - Too Many Requests (quota)
  • 500 - Internal Server Error
  • 502 - Bad Gateway
  • 503 - Service Unavailable
  • 504 - Gateway Timeout

Quota Failover

Special handling for quota-related errors:
config.yaml
switch-project: When true, quota errors trigger immediate retry with next credential:
switch-preview-model: When true, falls back to preview models:

Multi-Provider Routing

Some models are available from multiple providers:
config.yaml
Selection order:
  1. Filter to credentials offering the requested model
  2. Apply routing strategy within available credentials
  3. Round-robin across providers (not just accounts)

Request Metadata

Control routing via request metadata:

Pin to Specific Credential

Forces the request to use credential with ID auth-id-123.

Track Selected Credential

Callback receives the ID of the selected credential.

Model Registry

The registry dynamically tracks which credentials can serve which models:
Dynamic visibility:

Streaming Bootstrap Retries

For streaming requests, retries happen before the first byte is sent:
config.yaml
Bootstrap retry flow:
  1. Attempt 1: Credential A → Error before streaming
  2. Attempt 2: Credential B → Starts streaming → Success
Once streaming starts, no more retries (client already receiving data).

Performance Considerations

Scheduler Optimization

The scheduler pre-builds selection views:
sdk/cliproxy/auth/scheduler.go
Benefits:
  • O(1) credential selection (no sorting on hot path)
  • Efficient priority handling
  • Fast cooldown management

Concurrency

Routing decisions are lock-free for read paths:
sdk/cliproxy/auth/conductor.go
Multiple requests select credentials concurrently without contention.

Debugging Routing

Enable debug logging:
config.yaml
Logs include:
  • Credential selection decisions
  • Cooldown state changes
  • Retry attempts
  • Provider routing
Example log:

Best Practices

Round-robin maximizes total quota usage by spreading load across all accounts evenly.
Fill-first prevents hitting multiple accounts’ daily message limits simultaneously.
Keep low-priority accounts as emergency backup when primary accounts hit quota.
Assign each team a prefixed credential pool to prevent quota conflicts.
Set max-retry-credentials to prevent excessive retry attempts that delay errors.

Next Steps

Configuration

Configure routing behavior

Model Mappings

Set up model aliases and pools

Providers

Learn about provider-specific features

Management API

Monitor routing via API