Skip to content
Sign Up Free
Coding and development

Codex CLI/App: custom provider, API Key, and Base URL

Configure Codex CLI and Codex App with config.toml, auth.json, a BetterToken API Key, and the Responses Base URL. Includes setup, examples, and common fixes.

Codex CLI and Codex App share config.toml and auth.json for local tasks. This setup uses a BetterToken API Key through OpenAI Responses.

Key settings

FieldValue
API Keyauth.json → OPENAI_API_KEY
Base URLhttps://bettertoken.ai/v1
Modelgpt-6-astra

Prerequisites

Sign up and get a BetterToken API Key

Already have an account? Sign in, top up, then create a key in the console.

Install

You can use the official standalone installers for macOS/Linux or Windows without Node.js. If you prefer npm, install Node.js first, then run:

Terminal window
npm install -g @openai/codex

Locate and back up your files

If you use a custom CODEX_HOME, edit config.toml and auth.json in that directory. Otherwise use ~/.codex on macOS / Linux or %USERPROFILE%\.codex on Windows. CLI and App must use the same directory. WSL has its own user directory.

Fully quit Codex before editing. Back up both files to a private location. Create missing files if needed. Replace existing settings instead of appending duplicate keys or tables; keep unrelated settings.

Command-line setup

The BetterToken Automatic setup script writes the Codex provider configuration. It requires Node.js and prompts for the API Key or Model ID when they are not passed as arguments.

macOS / Linux

Terminal window
curl -fsSL "https://bettertoken.ai/install-codex-provider.sh" | bash

Windows

Terminal window
iwr "https://bettertoken.ai/install-codex-provider.ps1" -OutFile "$env:TEMP\install-codex-provider.ps1"; powershell -ExecutionPolicy Bypass -File "$env:TEMP\install-codex-provider.ps1"

If you omit the model, the script lists the available text models for you to choose from. The command you copy from the key’s Setup dialog in the workspace already includes the API Key and selected model. To skip the list and specify a model directly:

  • macOS / Linux: Replace the final bash in the command with bash -s -- --model YOUR_MODEL_ID.
  • Windows: Add -Model YOUR_MODEL_ID to the end of the command.
What the script changes and how to restore your settings
  • Top level of config.toml. The script writes model_provider, model, review_model (defaults to model), cli_auth_credentials_store = "file", model_reasoning_effort = "high", model_context_window, and model_auto_compact_token_limit. It sets the last two to 1000000 and 900000 for GPT models and 128000 and 110000 for other models. Use --context-window and --auto-compact-token-limit to set the confirmed limits for your model.
  • [model_providers.bettertoken]. The script writes the fields from the manual setup below and the three settings under Optional tuning.
  • auth.json. The script writes auth_mode = "apikey" and OPENAI_API_KEY. It preserves the other contents.
  • Backup and restore. Before writing, the script saves config.toml.bak.<timestamp> and auth.json.bak.<timestamp> in the same directory. To restore, replace each file with its backup.
  • Built-in test. The script calls /v1/responses directly to verify your API Key and model. This request bypasses Codex, so also verify the connection in Codex.

Next step. Quit Codex completely, including background processes. Reopen it and follow Verify the connection.

Manual setup

Edit config.toml

This guide uses user-level config.toml. Put root keys before all TOML tables, not inside [projects."<path>"]; that table stores project trust_level. The selected provider ID must match its table name. For file authentication, remove env_key and experimental_bearer_token from the selected provider.

model_provider = "bettertoken"
model = "gpt-6-astra"
cli_auth_credentials_store = "file"
[model_providers.bettertoken]
name = "BetterToken"
base_url = "https://bettertoken.ai/v1"
wire_api = "responses"
requires_openai_auth = true
supports_websockets = false

This example uses GPT-6 Astra (gpt-6-astra). For another model, copy its complete ID from the model plaza and replace model. GPT, Kimi, and GLM use the same file-auth structure. Automatic setup supports all three.

Edit auth.json

auth.json is in the same directory as config.toml. If auth.json does not exist, create it in that directory. Set its contents to the following JSON, replace YOUR_API_KEY with your BetterToken API Key, and save the file.

{
"auth_mode": "apikey",
"OPENAI_API_KEY": "YOUR_API_KEY"
}

Configuration parameters

ParameterPurpose and locationDefaults and examples
model_providerUser config root; selects the provider ID defined below.Defaults to openai; this guide uses bettertoken.
modelRoot; the full Model ID from the model plaza.Example: gpt-6-astra; replace as needed.
cli_auth_credentials_storeRoot; chooses the login credential store.This guide sets file to read auth.json in the same directory.
nameProvider table; a display name only.BetterToken is not a Model ID.
base_urlProvider table; the model API Base URL.Use https://bettertoken.ai/v1; Codex appends /responses.
wire_apiProvider table; the request protocol.Only responses is currently supported; it is also the default.
requires_openai_authProvider table; uses OpenAI authentication and its credential store.Defaults to false; this guide uses true. It is not a switch that preserves login files.
supports_websocketsProvider table; Responses WebSocket support.Defaults to false; this guide keeps HTTP/SSE transport.
auth_modeIn auth.json; selects the authentication mode.Set to apikey here; this replaces official ChatGPT authentication.
OPENAI_API_KEYIn auth.json; the API authentication credential.Replace YOUR_API_KEY with your BetterToken API Key.

File authentication here switches to API Key mode. To keep your official account, use official-login preservation; do not mix the two modes. They differ by authentication goal, not by CLI, App, or Extension.

Optional tuning

These three settings are optional tuning, not connection requirements. Merge them into the existing [model_providers.bettertoken] table when needed; do not create a duplicate table.

request_max_retries = 4
stream_max_retries = 8
stream_idle_timeout_ms = 300000
ParameterPurpose and locationDefaults and examples
request_max_retriesProvider table; maximum HTTP request retries.Default: 4; adjust for your connection.
stream_max_retriesProvider table; reconnection retries after a stream interruption.Default: 5; 8 is an optional custom value.
stream_idle_timeout_msProvider table; stream inactivity timeout in milliseconds.Default: 300000; this is an idle timeout, not a total task deadline.

Configuration layers and legacy keys

Do not put model or reasoning settings inside [projects."<path>"]. Project-specific models can use root keys in a trusted project’s .codex/config.toml; provider and authentication settings remain in user config. Current named profiles use NAME.config.toml under CODEX_HOME, selected with codex --profile NAME, not legacy [profiles.NAME] tables or the root profile selector.

Verify the connection

Save both files. In the terminal, start a new Codex CLI session directly:

Terminal window
codex

Fully quit Codex App, including background processes, then reopen it and create a new local task. Send a short prompt in each client you use. A normal response verifies that client’s connection.

Codex CLI 0.164.0 also passed environment variable authentication tests, including file-reading tool calls: env_key worked with requires_openai_auth = false and with that field omitted.

Switch models

Change model and, optionally, review_model. Save the file, stop the current Codex process, and start it again. Use the Model ID from the Setup dialog or model plaza.

Common errors

For 401, 403, or a sign-in prompt, check the active directory, the Key in auth.json, auth_mode = apikey, cli_auth_credentials_store = file, and requires_openai_auth = true. For provider or model errors, check the matching provider ID, https://bettertoken.ai/v1, wire_api = responses, and the model’s availability. If changes do not apply, check configuration overrides and confirm CLI and App use the same CODEX_HOME.

Advanced setup

Codex Extension

CLI, App, and Extension share configuration when they use the same CODEX_HOME. Choose authentication by goal: API Key access or preserving official sign-in.

Codex Extension setup guide

Official reference

Technical details

Responses protocol

Codex uses OpenAI Responses. Set base_url to https://bettertoken.ai/v1 and keep wire_api = "responses". Codex appends /responses.