Agents & CLIsGuide reviewed 2026-10-05

Codex CLI

Point OpenAI's Codex CLI at RouterPlex.

Follow updates Share setup
On this page

Set it up with your coding agent

Paste your key into the prompt's key field, then copy the prompt into Claude Code, Codex, Cursor, or any coding agent. It configures the tool and keeps testing until a real request goes through RouterPlex, fixing environment problems along the way. Prefer to do it by hand? Follow the manual setup below; your key fills those commands too.

prompt

Optional. It stays in this browser tab and is never saved or sent. No key yet? Create one in guided setup.

Set up Codex CLI on this machine to use RouterPlex, an OpenAI- and Anthropic-compatible AI gateway. Keep working until a real request from Codex CLI succeeds through RouterPlex.
 
My RouterPlex API key: YOUR_ROUTERPLEX_API_KEY
 
Key handling:
- If the key line above is a placeholder rather than a real key (real keys start with sk-), use my ROUTERPLEX_API_KEY environment variable. If that is empty too, ask me for the key once, then continue.
- Store the key only in user-level config or my user environment. Never put it in a project file, never commit it, and do not repeat the full key in your replies (show at most its first 8 characters).
 
Config handling:
- Before changing an existing config file or shell startup file, copy it to a .bak file next to it and show me the diff. Merge into what is there; never replace my other settings, providers, or MCP servers.
- If a step needs something only I can do, such as a click in a settings window, print the exact steps instead of guessing.
 
Full guide: https://docs.routerplex.com/codex-cli
 
Step 1. Prove the key and the RouterPlex gateway work before changing anything else. Run:
curl -sS https://api.routerplex.com/v1/chat/completions -H "Authorization: Bearer YOUR_ROUTERPLEX_API_KEY" -H "Content-Type: application/json" -d '{"model":"claude-sonnet-4-6","max_tokens":32,"messages":[{"role":"user","content":"Reply with exactly: RouterPlex connected."}]}'
On Windows PowerShell, use curl.exe with the body in a file, or Invoke-RestMethod.
- A reply containing "RouterPlex connected.": go on.
- 401: the key is wrong or revoked. Stop and ask me for a correct key.
- 402: my balance or trial credit is used up. 403: this key reached its own spending limit. Stop and tell me; retrying cannot fix these.
- Network, DNS, TLS, or proxy errors: find and fix the cause (proxy variables, VPN, firewall, system clock, outdated CA certificates), then run it again.
 
Step 2. Configure Codex CLI:
- Run codex --version. If Codex is missing, install it with the official installer (curl -fsSL https://chatgpt.com/codex/install.sh | sh).
- Merge this into my user-level ~/.codex/config.toml (Windows: %USERPROFILE%\.codex\config.toml; use $CODEX_HOME/config.toml if CODEX_HOME is set). Keep my other settings, put model and model_provider above any [table] header, and update an existing [model_providers.routerplex] block instead of adding a second one:
 
model_provider = "routerplex"
model = "gpt-5.6-terra"
model_reasoning_effort = "low"
 
[model_providers.routerplex]
name = "RouterPlex"
base_url = "https://api.routerplex.com/v1"
wire_api = "responses"
env_key = "ROUTERPLEX_API_KEY"
 
- wire_api = "responses" is required. env_key makes Codex read the key from ROUTERPLEX_API_KEY. Leave requires_openai_auth unset; setting it makes Codex demand a ChatGPT login.
- If I pick a non-OpenAI model, also set model_context_window to its context_tokens minus max_output_tokens from https://routerplex.com/api/models, and drop model_reasoning_effort for models without reasoning.
- Make ROUTERPLEX_API_KEY permanent for my user so new terminals get it:
- macOS/Linux: add export ROUTERPLEX_API_KEY='<key>' (keep the word export: a plain ROUTERPLEX_API_KEY=... line is invisible to programs started from the shell) to the startup file of the shell I use (check $SHELL: zsh uses ~/.zshrc; bash uses ~/.bashrc, plus ~/.bash_profile or ~/.profile if they do not source ~/.bashrc). Update an existing line instead of adding a second one.
- Windows: run [Environment]::SetEnvironmentVariable('ROUTERPLEX_API_KEY', '<key>', 'User'). Only terminals and apps started after this see it.
- WSL, SSH, or a dev container: set it inside that environment, where the tool actually runs.
 
Step 3. Test Codex CLI itself from a new terminal or a restarted app, so you test the saved setup rather than the shell you changed:
From an empty temporary folder: codex exec --skip-git-repo-check --model 'gpt-5.6-terra' 'Reply with exactly: RouterPlex connected. Do not read files or run tools.'
Expected reply: "RouterPlex connected."
 
Step 4. If step 1 works but step 3 fails, the problem is Codex CLI's configuration or environment, not RouterPlex. Check these, fix what is wrong, and repeat step 3:
- The key or base URL is set in one place but not where Codex CLI starts: zsh vs bash startup files, login vs non-login shells, a desktop launcher, a background service, Windows vs WSL, or a remote SSH or container environment.
- Another setting wins: OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_API_BASE, ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL, or a project-level config that overrides the user-level one.
- Codex CLI is outdated, or still needs a full restart to read the new settings.
- If an environment variable cannot be made to reach Codex CLI reliably and Codex CLI accepts a key in its user-level config, put the key there instead.
 
Keep repeating steps 3 and 4 until the test reply comes back.
 
Step 5. The test above sends no tools, but real sessions send every tool, MCP server, and plugin schema with each request, and that is where models differ. From a folder with a README.md (create a temporary one containing "# RouterPlex tool check" if needed), run:
codex exec --model gpt-5.6-terra "List your available tools, then read ./README.md and reply with its first heading."
To test without touching my real Codex setup, copy ${CODEX_HOME:-~/.codex} to a temporary folder, run the command with CODEX_HOME set to that folder, then delete the folder (it contains my Codex credentials).
It passes when the reply lists the tools and quotes the heading. If it fails while step 3 passes, the tool schemas are the problem, not the connection. Tell me the exact error; the chosen model may not handle Codex CLI's tools, so suggest another tool-capable model from https://routerplex.com/models.
 
Stop early only for a 401, 402, or 403, or for a step only I can do, such as a click in a settings window or a login. Then tell me exactly what to do, wait for me, and continue testing afterwards.
 
When it works, tell me exactly which files you changed and which values you set (key shown by its first 8 characters only) so I can undo it, where the key is stored, and the final test output. Remind me that every request shows up in https://routerplex.com/dashboard/usage.

Manual setup

Use RouterPlex's Responses API with a user-level Codex custom provider. Start with the example model, then try other catalog models on a small task before relying on their agent behavior.

1. Install or update

macOS / Linux

bash
curl -fsSL https://chatgpt.com/codex/install.sh | sh

Windows PowerShell

powershell
powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"

Reopen your terminal and run codex --version. These commands use the standalone installer. If you chose the npm install on Windows, use codex.cmd when PowerShell blocks codex.ps1.

2. Save the provider

Edit the user-level config.toml:

  • macOS/Linux: ~/.codex/config.toml
  • Windows: %USERPROFILE%\.codex\config.toml
  • Custom location: config.toml inside CODEX_HOME, if set.

Merge the settings below. Put model and model_provider above any [table] headers; update the existing RouterPlex block if present. Current Codex ignores provider/auth configuration in project .codex/config.toml files.

toml
model_provider = "routerplex"
model = "gpt-5.6-terra"
model_reasoning_effort = "low"
 
[model_providers.routerplex]
name = "RouterPlex"
base_url = "https://api.routerplex.com/v1"
wire_api = "responses"
env_key = "ROUTERPLEX_API_KEY"

wire_api = "responses" is required for this path, and it is the only value current Codex accepts. env_key reads your RouterPlex key from the process environment; do not insert the raw key into the TOML file. Leave requires_openai_auth unset: setting it makes Codex ask for a ChatGPT login instead of using your RouterPlex key.

3. Set your key and test

Use an empty folder and replace YOUR_ROUTERPLEX_API_KEY with your key.

macOS / Linux

bash
export ROUTERPLEX_API_KEY='YOUR_ROUTERPLEX_API_KEY'
codex exec --skip-git-repo-check --model 'gpt-5.6-terra' 'Reply with exactly: RouterPlex connected. Do not read files or run tools.'

Windows PowerShell

powershell
$env:ROUTERPLEX_API_KEY = 'YOUR_ROUTERPLEX_API_KEY'
codex exec --skip-git-repo-check --model 'gpt-5.6-terra' 'Reply with exactly: RouterPlex connected. Do not read files or run tools.'

Check the reply and Usage. Then run codex in your project. Set the variable again in a new terminal. See environment setup for persistent settings and WSL.

Switch models and profiles

Use codex --model claude-sonnet-4-6 to change only the model while keeping the provider. Your key must allow the selected model.

For Codex 0.134.0 and later, a profile is a separate file alongside config.toml, for example routerplex-sonnet.config.toml:

toml
model = "claude-sonnet-4-6"

Launch with codex --profile routerplex-sonnet. Keep shared provider settings in the base config. Older [profiles.name] examples do not apply to current releases; update Codex before using this profile layout.

Non-OpenAI models

Codex knows the context windows of OpenAI models only. For any other model, add its window so Codex compacts at the right time, using context_tokens minus max_output_tokens from the public catalog:

toml
model = "claude-sonnet-4-6"
model_context_window = 936000 # 1,000,000 context - 64,000 max output

Remove model_reasoning_effort when you switch to a model without reasoning. Put these lines in a profile file (below) to keep them separate from your GPT settings.

Test with your real tools before you switch

The short test sends no tools, but real sessions send every MCP server's tool schemas, which is where models differ. To test a new model or key without touching your real config, sessions, or login, copy your Codex home into a temporary folder and run one tool round trip there:

bash
TEST_HOME="$(mktemp -d)"
cp -R "${CODEX_HOME:-$HOME/.codex}/." "$TEST_HOME/"
CODEX_HOME="$TEST_HOME" codex exec --model gpt-5.6-terra "List your available tools, then read ./README.md and reply with its first heading."
rm -rf "$TEST_HOME"

Run it from a project that has a README.md. It passes when the reply lists your tools and quotes the heading. The copy contains your Codex credentials, so delete it afterwards.

Troubleshooting

Check key presence without printing its value using environment setup. Restart Codex after changing configuration. If the route or model is wrong, inspect user and project overrides. Responses compatibility is separate from a model's ability to follow Codex tool instructions.

For the VS Code extension, use its guide, especially the environment inheritance steps.

A longer walkthrough of the same custom provider is in Codex CLI custom provider.

Official references: Codex installation, custom providers and profiles.

Codex CLI · RouterPlex Docs