Skip to main content

Overview

Codex-LB supports multiple load balancing strategies to distribute requests across your pooled accounts. The strategy you choose affects throughput, fairness, and quota utilization.

Available Strategies

Usage-Weighted (Default)

The usage_weighted strategy prioritizes accounts with the lowest usage percentage, ensuring even distribution of quota consumption. How it works:
  1. Calculates current usage percentage for each account
  2. Sorts accounts by: secondary usage → primary usage → last selected time
  3. Selects the account with the lowest combined usage
Use cases:
  • Default for most deployments - Balances load fairly across all accounts
  • Quota-conscious workloads - Prevents early exhaustion of any single account
  • Heterogeneous account mix - Works well with different plan types (Plus, Team, Enterprise)
Example: With 3 accounts at 20%, 35%, and 60% usage, requests prioritize the 20% account first.
Usage percentages are calculated from ChatGPT’s own rate limit windows (typically 5-minute sliding windows for rate limits and weekly/monthly windows for quotas).

Round-Robin

The round_robin strategy rotates through accounts sequentially, selecting the least recently used account. How it works:
  1. Tracks last_selected_at timestamp for each account
  2. Sorts accounts by selection time (oldest first)
  3. Selects the account that was used longest ago
Use cases:
  • Testing and validation - Predictable distribution for debugging
  • Uniform account types - When all accounts have identical quotas
  • Low-utilization scenarios - When usage tracking overhead isn’t needed
Example: With 3 accounts last used at 10:00, 10:05, and 10:10, the next request goes to the 10:00 account.
Round-robin ignores usage percentages and can exhaust high-traffic accounts while leaving others idle. Only use this strategy when accounts have similar capacity and traffic patterns.

Configuring Strategy

Via Dashboard

  1. Navigate to Settings tab
  2. Find Load Balancing section
  3. Select routing strategy from dropdown:
    • usage_weighted (recommended)
    • round_robin
  4. Click Save Changes

Via API

Via Database

The strategy is stored in the dashboard_settings table:

Advanced Options

Prefer Earlier Reset Accounts

When enabled, this option adds an additional sorting dimension to prioritize accounts that will reset soonest. How it works:
Effect:
  • Groups accounts by days until reset (bucketed)
  • Within each bucket, applies usage-weighted sorting
  • Prefers accounts resetting in 1 day over those resetting in 7 days
Use case: Maximize long-term availability by exhausting soon-to-reset accounts before far-away resets. Configuration:
Enable “Prefer earlier reset” if you frequently hit quota limits. This ensures you use accounts that are about to reset, preserving capacity in accounts with longer reset windows.

Sticky Sessions

Sticky sessions bind a conversation thread to a specific account, ensuring continuity. Configuration:
Behavior:
  • Sticky key is derived from chatgpt-conversation-id header
  • First request in conversation selects account using configured strategy
  • Subsequent requests in same conversation reuse the same account
  • If pinned account becomes unavailable, a new account is selected
Sticky sessions are stored in the sticky_sessions table and survive server restarts.

Selection Algorithm

The complete selection algorithm (from app/core/balancer/logic.py:45-134):

Performance Considerations

Usage Refresh Overhead

Usage-weighted strategy requires periodic usage data refresh:
  • Refresh interval: Configurable via USAGE_REFRESH_INTERVAL_SECONDS (default: 300s)
  • API calls: One usage API call per account per interval
  • Database writes: One usage_history row per account per interval
For high-request-rate deployments (>1000 req/min), consider increasing the refresh interval to reduce overhead, or use round-robin for accounts with identical quotas.

Request Latency

The selection algorithm runs on every request:
  • Usage-weighted: O(n log n) sorting where n = number of accounts
  • Round-robin: O(n log n) sorting by timestamp
  • Typical overhead: Less than 1ms for pools up to 100 accounts

Strategy Comparison

Troubleshooting

Uneven Distribution

Symptom: Some accounts heavily used, others idle Causes:
  • Round-robin with heterogeneous account types
  • Sticky sessions concentrating traffic
  • Some accounts frequently rate-limited (check error logs)
Solution: Switch to usage-weighted strategy

All Accounts Exhausted

Symptom: “No available accounts” errors Causes:
  • Insufficient total quota for traffic volume
  • All accounts hitting rate limits or quota simultaneously
Solution:
  1. Add more accounts to pool
  2. Reduce traffic or implement request queuing
  3. Upgrade accounts to higher-tier plans

Selection Bias

Symptom: One account always selected first Causes:
  • Account has significantly lower usage than others
  • Clock skew affecting last_selected_at timestamps
Solution: Normal behavior for usage-weighted. If problematic, check usage data is refreshing correctly.

Technical Reference

Key source files:
  • app/core/balancer/logic.py:45-134 - Selection algorithm
  • app/modules/proxy/load_balancer.py:54-138 - Load balancer integration
  • app/db/models.py:30-36 - AccountStatus enum
  • app/core/balancer/types.py:21 - RoutingStrategy type