How to use Codex CLI with OpenRouter A developer documented how to point OpenAI's Codex CLI at OpenRouter by defining a custom model provider in ~/.codex/config.toml, setting base_url to OpenRouter's API endpoint with wire_api = "responses", and supplying an OpenRouter key through command-backed authentication. The guide notes that OpenRouter's Responses API is stateless and that a ChatGPT subscription or OpenAI API key does not cover OpenRouter usage, which is billed against OpenRouter credits. Originally published at alwaysagent.app https://alwaysagent.app/codex-openrouter . The original is kept up to date. Yes, Codex CLI works with OpenRouter through a custom model provider. Set OpenRouter's API base URL in your user-level config.toml, export your key, and choose an OpenRouter model. Here is the setup, how to verify it, and what changes when you switch providers. Yes. Define a custom provider in ~/.codex/config.toml with base url = "https://openrouter.ai/api/v1" and wire api = "responses" . Supply your OpenRouter key and select an OpenRouter model ID. OpenRouter publishes a Codex CLI guide https://openrouter.ai/docs/cookbook/coding-agents/codex-cli and documents an OpenAI-compatible Responses API https://openrouter.ai/docs/api reference/responses/overview at https://openrouter.ai/api/v1/responses . In Codex, enter the base URL without the final /responses . OpenRouter describes this API as stateless , with reasoning, tool calling, and web search integration. That documents an API interface; it does not promise identical behavior from every model. The configuration below follows the official documentation checked on November 23, 2026. OpenRouter lets you use one API key across models from different suppliers. Codex remains your terminal coding agent; the custom provider changes where its model requests go. A ChatGPT subscription does not fund this OpenRouter setup. Likewise, an OpenAI API key belongs to OpenAI's billing system. Choose the connection whose billing and model access you want. Before a long task, run a small representative one and check its cost in OpenRouter. The billing FAQ https://openrouter.ai/docs/faq explains how token usage is charged. | Connection | Billing | Model choice | Setup | |---|---|---|---| | ChatGPT sign-in | ChatGPT plan access and applicable limits | OpenAI models available to your account or workspace | Use the built-in OpenAI provider and run codex login | | OpenAI API key | Usage billed to your OpenAI Platform account | OpenAI models your API account can access | Use the built-in OpenAI provider and API-key login | | OpenRouter | Usage charged against OpenRouter credits | OpenRouter catalog; verify tool support and compatibility | Define a custom provider and supply an OpenRouter key | The terminal examples use Bash or Zsh. If Codex is not installed, OpenAI's official repository https://github.com/openai/codex documents npm install -g @openai/codex , alongside standalone installers and Homebrew. Open the API Keys page linked from OpenRouter's Codex guide and create a key. Add credits for paid model requests. Use an OpenRouter key for this provider. Set the variable in the same terminal that will launch Codex. Replace the placeholder below with your key. For later terminals, add the export to your Bash or Zsh startup file. Create ~/.codex if needed, then merge the provider configuration below into ~/.codex/config.toml . Preserve your other settings. Keep top-level model and model provider assignments before table headers. Start a new session from your project directory after saving the file. The selected provider sends model requests to OpenRouter. Enter /status to see session details such as context usage and rate limits. Ask Codex to inspect a small file, then check the OpenRouter Activity dashboard for the request, model, token usage, and cost to confirm which model served it. Terminal: set your key export OPENROUTER API KEY="