Setup guide - Documented by OpenRouter
Use OpenRouter in Codex CLI
OpenRouter's official Codex CLI guide adds an openrouter model_provider to ~/.codex/config.toml with base_url https://openrouter.ai/api/v1. The key comes from a command-based auth block, which also makes Codex fetch the OpenRouter model catalog.
Last verified 7 steps1 official source
Steps
- Step 1.
Install Codex CLI by following the instructions in the openai/codex GitHub repo.
- Step 2.
Create an OpenRouter API key at openrouter.ai/keys (it starts with sk-or-...).
- Step 3.
Create or edit ~/.codex/config.toml.
TOMLmodel_provider = "openrouter" model_reasoning_effort = "high" model="~openai/gpt-sol-latest" [model_providers.openrouter] name = "openrouter" base_url="https://openrouter.ai/api/v1" [model_providers.openrouter.auth] command = "sh" args = ["-c", "echo $OPENROUTER_API_KEY"] - Step 4.
On Windows, use the PowerShell auth block instead.
TOML[model_providers.openrouter.auth] command = "powershell" args = ["-NoProfile", "-Command", "Write-Output $env:OPENROUTER_API_KEY"] - Step 5.
Export your key in your shell profile (~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish).
Terminalexport OPENROUTER_API_KEY="sk-or-..." - Step 6.
On Windows, set it as a user-level variable, then fully restart Codex.
Terminalsetx OPENROUTER_API_KEY "sk-or-..." - Step 7.
Start Codex in your project.
Terminalcd /path/to/your/project codex
Caveats
- A plain env_key = "OPENROUTER_API_KEY" also authenticates, but Codex then won't fetch the OpenRouter model catalog, and non-OpenAI models show the "Unknown model" fallback-metadata warning. OpenRouter recommends the command-based auth block.
- A 401 with "Missing Authentication header" usually means the auth command failed to run.
- model_reasoning_effort accepts "low", "medium", "high", "xhigh", "max" per the OpenRouter guide.
- The Codex desktop app (ChatGPT for macOS and Windows) has a separate OpenRouter guide.
More setup guides
Same tool, other providers
Related reading
- Model Routing Recipes: Practical Config Patterns to Cut AI SpendModel routing recipes with paste-ready OpenRouter and LiteLLM configs for task tiers, fallback chains, cost ceilings and cache-aware routing that cut AI spend.
- OpenRouter Fusion Makes Model Panels Real. Use Them Like Escalation, Not AutopilotOpenRouter Fusion turns multi-model panels into an API feature. Use it as an escalation lane for prompts where a wrong answer is expensive, not as the default.
- OpenRouter in 2026: Review, Setup, and When Model Routing PaysOpenRouter review and setup - one API key for 300+ models with fallbacks and provider routing, what it costs, and when to go direct to a provider instead.
- LLM Routers Compared: LiteLLM vs Portkey vs OpenRouter in 2026LiteLLM vs Portkey vs OpenRouter compared for production AI apps: cost tracking, fallbacks, caching, and load balancing, plus when to use each router.
- Codex CLI 0.163: Instant Steering Is Now the DefaultCodex CLI 0.163.0 makes mid-response steering the default and adds searchable /config and a leader key. What changed and what to check before upgrading.
- Codex 'gpt-5.5 Model Is Not Supported' Error: The FixCodex says gpt-5.5 is not supported with a ChatGPT account because OpenAI retired it from ChatGPT sign-in on October 14. The fix and what still works.
FAQ
How do I use OpenRouter in Codex CLI?
OpenRouter's official Codex CLI guide adds an openrouter model_provider to ~/.codex/config.toml with base_url https://openrouter.ai/api/v1. The key comes from a command-based auth block, which also makes Codex fetch the OpenRouter model catalog.
Which OpenRouter model ids work in Codex CLI?
The official docs read for this guide list ~openai/gpt-sol-latest and ~anthropic/claude-sonnet-latest. Requests go to https://openrouter.ai/api/v1.
Is this an official OpenRouter and Codex CLI setup?
OpenRouter documents this setup in its own docs. It was last checked against 1 official page on October 17, 2026.
Sources
Last verified against 1 official page.
Newsletter
New models, set up the day they ship
One email per week. New model releases, the coding tools that support them and how to wire them up.