Overview
Codex-LB uses API key authentication for most endpoints. Authentication can be enabled or disabled globally, and when enabled, all proxy endpoints require a valid API key in the Authorization header.Authentication Configuration
Authentication is controlled by theapi_key_auth_enabled setting in the database. When authentication is disabled, all API endpoints are accessible without an API key.
API Key Format
API keys follow the format:sk-clb-is a fixed prefix identifying Codex-LB keys{random_token}is a 32-byte URL-safe base64-encoded random value
Authentication Method
Codex-LB uses HTTP Bearer token authentication. Include your API key in theAuthorization header:
Example Request
When API Keys Are Required
API keys are required for these endpoint groups:OpenAI-Compatible Endpoints (v1 Router)
All endpoints under/v1 require authentication when enabled:
POST /v1/chat/completionsPOST /v1/responsesPOST /v1/responses/compactGET /v1/modelsPOST /v1/audio/transcriptions
Security(validate_proxy_api_key) (app/modules/proxy/api.py:58-62)
Codex Backend Endpoints
All endpoints under/backend-api/codex require authentication when enabled:
POST /backend-api/codex/responsesPOST /backend-api/codex/responses/compactGET /backend-api/codex/modelsPOST /backend-api/transcribe
Security(validate_proxy_api_key) (app/modules/proxy/api.py:53-57, 67-71)
When API Keys Are Optional
Whenapi_key_auth_enabled is set to false, API keys are not required. The validate_proxy_api_key function returns None and the request proceeds without authentication checks (app/core/auth/dependencies.py:36-41).
Alternative Authentication (Usage Endpoint)
The/api/codex/usage endpoint uses a different authentication mechanism:
ChatGPT Token + Account ID
This endpoint requires:-
Authorization header with ChatGPT Bearer token:
-
chatgpt-account-id header with the account ID:
Depends(validate_codex_usage_identity) (app/modules/proxy/api.py:63-66)
Validation process (app/core/auth/dependencies.py:83-108):
- Extract Bearer token from Authorization header
- Extract account ID from
chatgpt-account-idheader - Verify the account ID exists and is active in the database
- Validate credentials by fetching usage from OpenAI’s API
Example Usage Request
API Key Validation
When a request includes an API key, the following validation occurs (app/core/auth/dependencies.py:36-52):1. Key Format Validation
The API key must be present in the Authorization header:2. Key Hash Lookup
The key is hashed using SHA-256 and looked up in the database:3. Active Status Check
The API key must haveis_active = true:
4. Expiration Check
If the key has an expiration date, it must not have passed:5. Model Access Validation
If the API key has anallowed_models restriction, the requested model must be in the list:
Rate Limit Enforcement
After authentication, rate limits are enforced for each request (app/modules/proxy/api.py:524-543):Limit Types
- Requests - Maximum number of requests per time window
- Total Tokens - Combined input + output tokens
- Input Tokens - Maximum input tokens
- Output Tokens - Maximum output tokens
- Cost USD - Maximum spend in microdollars (1 USD = 1,000,000 microdollars)
Time Windows
- Daily - Resets every 24 hours
- Weekly - Resets every 7 days
- Monthly - Resets every 30 days
Model-Specific Limits
Limits can be scoped to specific models using themodel_filter field. A limit applies to a request if:
Usage Reservation
Each request reserves quota upfront (app/modules/api_keys/service.py:329-371):- Check each applicable limit
- Reserve a portion of the remaining quota
- Create a usage reservation ID
- After response, finalize with actual token counts
- Adjust the reserved amount to match actual usage
- Tokens: 8,192 tokens per request
- Cost: 2,000,000 microdollars ($2.00) per request
- Requests: 1 request
Authentication Errors
Authentication failures return a 401 status code with an OpenAI-formatted error:Missing API Key
Invalid API Key
Expired API Key
Model Not Allowed
Status Code: 403Rate Limit Exceeded
Status Code: 429Security Best Practices
Key Storage
- API keys are stored as SHA-256 hashes in the database
- Only the key prefix (first 15 characters) is stored in plaintext for identification
- The full plaintext key is only returned once when the key is created or regenerated
Key Rotation
API keys can be regenerated via the management API:- Generates a new random token
- Updates the hash and prefix
- Preserves all other key attributes (limits, allowed models, etc.)
Last Used Tracking
Thelast_used_at timestamp is updated each time a key successfully completes a request, allowing administrators to identify unused keys.
Next Steps
- Managing API Keys - Learn how to create and manage API keys
- Rate Limiting - Configure rate limits for API keys
- Error Handling - Handle authentication errors properly