Architecture Overview
Codex Multi-Auth is a plugin that intercepts OpenAI SDK calls and routes them through ChatGPT’s Codex backend with OAuth authentication and intelligent multi-account rotation.System Architecture
Core Subsystems
1. Authentication (lib/auth/)
Purpose: OAuth 2.0 + PKCE flow for ChatGPT account authentication
Key Files:
auth.ts: Token exchange, refresh, JWT decodingserver.ts: Local callback server (port 1455)browser.ts: Platform-specific browser launch
2. Account Management (lib/accounts.ts)
Purpose: Multi-account pool with health scoring, cooldowns, and rotation logic
Selection Algorithm:
- Filter out accounts in cooldown
- Filter out accounts with active rate limits
- Apply session affinity (prefer same account for same thread)
- Score by health (0-100) + quota availability
- Apply PID offset for fair rotation
- Select highest-scoring account
- Starts at 100
- Decrements on failures (network, server, auth)
- Resets to 100 on success
- Accounts below threshold get cooldown
3. Storage (lib/storage.ts)
Purpose: V3 JSON storage with per-project/global scoping and worktree resolution
Storage Paths:
4. Request Pipeline (lib/request/)
See Request Pipeline for detailed 7-step flow.
Key Transformations:
- Model normalization (e.g.,
gpt-5.3-codex→gpt-5-codex) - Inject Codex system instructions (model-family specific)
- Enforce
stream: true,store: false - Add
reasoning.encrypted_contenttoinclude - Filter orphaned tool outputs
- Apply fast-session optimizations
5. Failure Handling (lib/request/failure-policy.ts)
See Failure Handling for circuit breaker and retry details.
Failure Types:
- auth-refresh: Rotate account + cooldown, remove after 3 consecutive failures
- network: Rotate + refund token + cooldown (6s default)
- server: Rotate + refund token + cooldown (4s or retry-after)
- rate-limit: Rotate + mark rate-limited (no cooldown, use reset time)
- empty-response: Retry same account or rotate (failover-mode dependent)
6. Circuit Breaker (lib/circuit-breaker.ts)
Purpose: Isolate failing accounts to prevent cascade failures
States:
- Closed: Normal operation
- Open: Account blocked (threshold reached)
- Half-Open: Testing recovery (limited attempts)
Runtime Features
Session Affinity (lib/session-affinity.ts)
Prefers the same account for the same conversation thread to maintain reasoning continuity.
Live Account Sync (lib/live-account-sync.ts)
Watches account storage file for changes and reloads without restart.
Proactive Refresh (lib/refresh-guardian.ts)
Refreshes tokens before expiry to avoid auth delays during requests.
Preemptive Quota Scheduler (lib/preemptive-quota-scheduler.ts)
Defers requests when quota is low to avoid hitting hard limits.
Configuration Sources
Priority order (highest to lowest):- Environment variables:
CODEX_MODE,CODEX_AUTH_* - Plugin config (
~/.codex/multi-auth/settings.json) - Runtime model config (passed to plugin loader)
- Defaults (see
lib/config.ts)
Error Observability
Logging Stages (source/lib/constants.ts:31):Related Documentation
- OAuth Flow - OAuth 2.0 + PKCE implementation
- Request Pipeline - 7-step transformation flow
- Failure Handling - Circuit breaker and retry logic