Overview
Account rotation is the core mechanism that selects which account to use for each request. The system uses a hybrid selection algorithm that combines:- Health scores - Track success/failure patterns
- Token buckets - Prevent preemptive rate limiting
- Freshness - Distribute load across accounts
- Session affinity - Maintain consistency within conversations
Selection Algorithm
Hybrid Scoring Formula
The account selection uses a weighted scoring system (lib/rotation.ts:318-393):
- Health weight: 2 - Moderate preference for healthy accounts
- Token weight: 5 - Strong preference for accounts with quota headroom
- Freshness weight: 2 - Balanced load distribution
Selection Flow
Implementation
Fromlib/rotation.ts:318-393:
Health Score Tracking
Score Dynamics
Health scores range from 0-100 with passive recovery (lib/rotation.ts:17-127):
Passive Recovery
Unused accounts gradually recover health:- Account hits rate limit: health drops to 60 (-10)
- After 1 hour idle: health recovers to 62 (+2)
- After 10 hours idle: health fully recovers to 100
- After 20 hours idle: capped at 100
Token Bucket Rate Limiting
Client-Side Token Tracking
Prevent requests before server-side rate limits hit (lib/rotation.ts:131-261):
Token Consumption
Each request consumes one token:Token Refills
Tokens automatically refill over time:- Account starts: 50 tokens
- After 10 requests: 40 tokens remaining
- After 1 minute: 46 tokens (40 + 6 refilled)
- After 10 minutes: 50 tokens (capped at max)
Token Refunds
Network errors (not rate limits) can refund tokens within 30 seconds:Failover Mechanisms
Automatic Rotation Triggers
Accounts rotate automatically when:Rate Limit (429)
Rate Limit (429)
Action:
- Mark account rate-limited with reset time
- Drain token bucket (-10 tokens)
- Reduce health score (-10 points)
- Rotate to next available account
- Account becomes available after
Retry-Afterexpires - Tokens refill at 6/minute
- Health recovers at 2/hour
Auth Failure (401/403)
Auth Failure (401/403)
Action:
- Attempt token refresh
- If refresh fails 3+ times: mark account for cooldown
- Rotate to next account
- Manual re-authentication:
codex auth login - Auto-retry after cooldown period
Server Error (5xx)
Server Error (5xx)
Action:
- Reduce health score (-20 points)
- Rotate to next account
- Do NOT refund token (server-side failure)
- Health recovers passively (2/hour)
- Automatic retry after health improves
Network Error
Network Error
Action:
- Refund token if within 30s window
- Reduce health score (-20 points)
- Rotate to next account
- Immediate retry with different account
- Token refunded, no quota impact
Cooldown System
Accounts can be temporarily disabled (lib/accounts.ts:565-583):
auth_failure- Multiple authentication failuresquota_exhausted- Primary + secondary quota both exhaustedmanual- Manually disabled viacodex auth disable <index>
Availability Checks
Account Availability
An account is available when ALL conditions are met:- Account exists
- Account not manually disabled
- No active rate limits for model family
- Not in cooldown period
Rate Limit Expiry
Expired rate limits are automatically cleared:Monitoring and Diagnostics
View Account Health
Health Dashboard
Runcodex auth to see:
Configuration
Tuning Rotation Behavior
Customize selection weights in your Codex config:Related Concepts
Quota Management
Learn how quotas are tracked and prevent rate limits
Session Recovery
Understand session affinity and recovery
Multi-Account OAuth
See how accounts are authenticated
Settings Reference
View all configuration options