How to Use OpenAI Codex CLI with OpenRouter
OpenRouter ·
On this page
Codex CLI runs an agentic coding loop in your terminal, and it already supports custom OpenAI-compatible providers. That hook is all you need to route it through OpenRouter.
The payoff is one API key in front of 300+ models, automatic provider failover, and consolidated usage tracking, with no change to Codex itself. The setup is a small config.toml block, but Codex has two requirements that trip people up if you miss them. This walks through the full setup and the two errors you’re most likely to hit.
Point Codex at OpenRouter in five steps
Install Codex CLI from the openai/codex repo, then create a key on your API Keys page. It starts with sk-or-.
Open ~/.codex/config.toml, creating it if it doesn’t exist, and add this:
# ~/.codex/config.toml
model = "openai/gpt-5.3-codex"
model_provider = "openrouter"
model_reasoning_effort = "high"
[model_providers.openrouter]
name = "OpenRouter"
base_url = "https://openrouter.ai/api/v1"
env_key = "OPENROUTER_API_KEY"
wire_api = "responses"
Two fields need attention. model must be a complete OpenRouter slug including the provider prefix, copied from the models page. And wire_api must be "responses", which we get to below.
One more placement rule: model_provider and model_providers only take effect in your user-level ~/.codex/config.toml. Codex ignores them in a project-local .codex/config.toml and prints a startup warning.
Then export your key in the shell profile Codex loads, run codex in a project, and send a test prompt:
export OPENROUTER_API_KEY="sk-or-..."
cd /path/to/your/project
codex
Open the Activity dashboard and confirm the request shows up with the right model name and a token count. If it does, you’re routed.
Set wire_api to responses
Codex used to speak the older chat/completions protocol, but OpenAI deprecated that path and removed it in February 2026. A custom provider with wire_api = "chat", or with no wire_api at all, now fails on startup.
Setting wire_api = "responses" puts Codex on the Responses API, which is what OpenRouter expects. The config block above already includes it. One related gotcha: the provider IDs openai, ollama, and lmstudio are reserved, so you can’t reach OpenRouter by overriding the built-in openai provider’s base URL. Define a new provider like openrouter instead.
Pin a Codex model and watch the spend
Codex models on OpenRouter share a 400K context window, so the choice comes down to price against task difficulty. Current rates from the model catalog, before the platform fee:
| OpenRouter slug | Input $/M | Output $/M |
|---|---|---|
openai/gpt-5.3-codex | $1.75 | $14 |
openai/gpt-5.1-codex | $1.25 | $10 |
openai/gpt-5.1-codex-mini | $0.25 | $2 |
Reach for gpt-5.1-codex-mini on iterative or exploratory work, and gpt-5.3-codex on the hardest tasks. You can also point model at any non-Codex slug, say anthropic/claude-sonnet-4.6, without touching anything else.
Agentic sessions burn more tokens than a prompt’s length suggests, because the model reprocesses repo files, tool outputs, and reasoning traces on every turn. Three controls keep that predictable. Set a spending guardrail on the key so requests are rejected once you hit a daily or monthly cap. Match the model to the task, since gpt-5.3-codex costs 7x more per output token than gpt-5.1-codex-mini. And drop model_reasoning_effort to "low" or "medium" on routine edits.
The fee math is light. OpenRouter doesn’t mark up provider pricing, so you pay the rates above plus a 5.5% fee on credit purchases. A focused session that reads 200K input tokens and writes 50K output on gpt-5.3-codex runs about $1.05 in token cost, and the credit fee adds about 6 cents. Failed requests aren’t billed.
Fix model_not_found
model_not_found is the other common error. Work through these in order:
- The slug isn’t exact. It has to match an OpenRouter slug character for character. Copy it straight from openrouter.ai/models.
- The prefix is missing. Codex slugs look like
openai/gpt-5.3-codex. Theopenai/prefix is required;gpt-5.3-codexalone won’t match. - The shorthand points elsewhere. The
~openai/gpt-latestalias tracks OpenAI’s latest general model, which may not be the Codex variant you want, so pin a Codex slug explicitly. - The config is in the wrong file. Move
model_providerandmodel_providersto your user-level~/.codex/config.toml.
When routing through OpenRouter pays off
OpenRouter earns its place in a Codex workflow when you want to switch quickly between many models, try open-source models alongside the OpenAI defaults, get failover across 60+ providers, see real-time usage visibility, or set team cost controls from one dashboard. Switching models is a one-line change to model in config.toml, with no new key and no reinstall. You can also run BYOK, routing through your own provider key for 5% of what the provider would bill, a fee waived for the first 1M requests each month.
Frequently asked questions
Can Codex CLI be used with OpenRouter?
Yes. Add a [model_providers.openrouter] block in your user-level ~/.codex/config.toml, point base_url at https://openrouter.ai/api/v1, set model_provider = "openrouter" and wire_api = "responses", then pin a model slug. Codex routes through OpenRouter from that point on.
Why do I get model_not_found with Codex and OpenRouter?
The model value has to be an exact OpenRouter slug including the provider prefix, like openai/gpt-5.3-codex. A bare gpt-5.3-codex is the most common cause. The provider block also has to live in your user-level ~/.codex/config.toml, not a project-local one.
Do I need an OpenAI subscription to use Codex CLI through OpenRouter?
No. Once you configure the custom provider and export OPENROUTER_API_KEY, requests route through and bill on OpenRouter. No separate OpenAI plan is required.
How much does Codex cost through OpenRouter?
You pay the provider’s per-token rate plus a 5.5% fee on credit purchases, with no provider markup. For example, gpt-5.3-codex is $1.75 per million input tokens and $14 per million output tokens before that fee. Failed requests aren’t billed.
What is wire_api and why does it need to be set?
wire_api controls which API protocol Codex uses to talk to a provider. As of February 2026, Codex removed support for the older chat value, so custom providers must set wire_api = "responses" or Codex errors on startup.