Overview
Codex-LB supports API key authentication to control access to your load balancer. Each key can have:- Model restrictions - Limit which models can be accessed
- Rate limits - Token, request, and cost limits per day/week/month
- Expiration dates - Automatic key deactivation
- Usage tracking - Monitor consumption per key
API key authentication is disabled by default. Enable it via Settings → API Key Auth Enabled.
Creating API Keys
Via Dashboard
- Navigate to Settings → API Keys
- Click Create API Key
- Configure:
- Name: Descriptive label (e.g., “Production App”)
- Allowed Models: Leave empty for all models, or select specific models
- Expiration: Optional expiration date
- Limits: Add rate limits (see Rate Limits)
- Click Create
- Copy the key immediately - it won’t be shown again!
Via API
Key Format
API keys follow this format:sk-clb-a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
Storage: Only the SHA-256 hash is stored in the database (from app/modules/api_keys/service.py:512-513):
Using API Keys
Authentication Header
Include the key in theAuthorization header:
Scope
API key authentication applies to:/v1/*(OpenAI-compatible endpoints)/backend-api/codex/*(ChatGPT-compatible endpoints)/backend-api/transcribe(Transcription endpoint)
/api/*(Dashboard API - uses session auth)/api/codex/usage(Uses bearer caller identity, not API keys)
Model Restrictions
Configuring Allowed Models
When creating or updating a key:- Empty/null: All models allowed
- Specific list: Only listed models allowed
Enforcement
Model restrictions are enforced in the proxy service layer:403 Forbidden
Model List Filtering
GET /v1/models automatically filters based on the authenticated key:
allowed_models list.
Fixed-Model Endpoints
For endpoints with implicit models (e.g., transcription):gpt-4o-transcribe for restriction checks.
Rate Limits
Limit Types
Codex-LB supports four limit types:Limit Windows
Creating Limits
Example: Daily token limitLimit Enforcement
Limits are enforced using a reservation system to prevent races:- Before request: Reserve estimated usage (8,192 tokens for token limits, $2 for cost limits)
- Process request: Forward to upstream API
- After response: Adjust reservation to actual usage
- On error: Release reservation
app/modules/api_keys/service.py:584-601:
Reservations prevent over-limit requests from starting, even under high concurrency. Actual usage is settled after the response, refunding unused quota.
Limit Applicability
Fromopenspec/specs/api-keys/spec.md:223-232:
model_filter=null: Applies to all requests (global limit)model_filter="gpt-4o": Applies only togpt-4orequests- Model-less endpoints (e.g.,
/v1/models): Only global limits apply
total_tokens=1M/day, model_filter=null(global)total_tokens=100K/day, model_filter="gpt-4o"(model-specific)
gpt-4o: Both limits enforced
Request for gpt-4o-mini: Only global limit enforced
Exceeding Limits
Error response:429 Too Many Requests
Header: Retry-After: 3600 (seconds until reset)
Automatic Reset
Limits reset using lazy evaluation:reset_at timestamp passes.
Managing API Keys
Listing Keys
The full key is never returned after creation. Only
key_prefix (first 15 characters) is shown.Updating Keys
nameallowed_modelsexpires_atis_activelimits
openspec/specs/api-keys/spec.md:259-265:
When updating API key limits, the system SHALL preserve existing usage state (Existing limits retain their counters; only new or modified limits reset.current_value,reset_at) for unchanged limit rules. Limit comparison key is(limit_type, limit_window, model_filter).
Disabling Keys
- Return
401 Unauthorizedon use - Remain in database for audit trail
- Can be re-enabled by setting
is_active: true
Regenerating Keys
If a key is compromised:Deleting Keys
Usage Tracking
Every API request records usage:RequestLog association: From openspec/specs/api-keys/spec.md:194-206:
The system SHALL record theView per-key request history:api_key_idin therequest_logstable for proxy requests authenticated with an API key.
Security Best Practices
Key Rotation
- Create new key with desired settings
- Update applications to use new key
- Monitor old key’s
last_used_attimestamp - Delete old key after migration complete
Principle of Least Privilege
- Model restrictions: Limit keys to only required models
- Rate limits: Set limits matching expected usage + margin
- Expiration: Use expiration dates for temporary access
Monitoring
Set up alerts for:- Keys approaching limits (>80% utilization)
- Keys with no recent usage (potential leak)
- Unusual traffic patterns (rapid usage spikes)
- 429 errors (limit exceeded)
Revoking Compromised Keys
If a key is exposed:- Immediately disable via
PATCHwithis_active: false - Investigate usage logs for unauthorized activity
- Regenerate or create new key
- Update legitimate applications
- Delete old key after verification
Global API Key Authentication
Enabling Authentication
API key authentication is controlled via settings:- All proxy requests require valid API key
- Dashboard API still uses session auth
- Missing or invalid keys return
401 Unauthorized
Disabling Authentication
- Proxy requests allowed without authentication
- Existing keys remain in database but aren’t enforced
- No usage tracking or rate limiting
Troubleshooting
401 Unauthorized
Causes:- Missing
Authorizationheader - Invalid key format
- Key deleted or disabled
- Key expired
403 Model Not Allowed
Causes:- Requested model not in
allowed_models - Model filter typo
allowed_models or use a different model.
429 Rate Limit Exceeded
Causes:- Hit daily/weekly/monthly limit
- Multiple limits stacked (global + model-specific)
Retry-After header) or increase limits.
Limits Not Resetting
Causes:- Reset logic runs on next validation (lazy)
- Clock drift on server
reset_at in database.
Related Features
- Dashboard Auth - Protect the admin dashboard
- Usage Tracking - Monitor account consumption
- Load Balancing - Account selection strategies
Technical Reference
Key source files:app/modules/api_keys/service.py- API key business logicapp/modules/api_keys/repository.py- Database operationsapp/modules/api_keys/schemas.py- API schemasapp/db/models.py:152-274- Database modelsopenspec/specs/api-keys/spec.md- Detailed specification