For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
ModelsChatRankingsDocs
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
  • Get Started
    • Quickstart: Build a Chat App
    • Enterprise Quickstart
    • Free Models Router
  • Working with Coding Agents
    • Automatic Code Review
    • Claude Code
    • Claude Desktop
    • Codex CLI
    • Cursor
    • Hermes Agent
    • Junie CLI
    • MCP Servers
    • OpenClaw 🦞
    • OpenCode
  • Building Agents
    • Add Human-in-the-Loop Controls
    • Build a Long-Horizon Agent
    • Build Your Own Agent TUI
    • Build Your Own Headless Agent
  • Video Generation
    • Choose a Video Generation Model
    • Generate and Download a Video from Text
    • Get Video Results with Webhooks
    • Guide a Video with Reference Images
    • Turn an Image into a Video
    • Use Provider-Specific Video Options
  • Evaluate & Optimize
    • Distillation
    • RAG with Embeddings & Rerank
    • Red Teaming
  • Administration
    • Activity Export
    • API Key Rotation
    • Crypto API
    • Organization Management
    • Usage Accounting
    • User Tracking
LogoLogo
ModelsChatRankingsDocs
On this page
  • What is Codex CLI?
  • Quick Start
  • Step 1: Install Codex CLI
  • Step 2: Get Your OpenRouter API Key
  • Step 3: Configure Codex for OpenRouter
  • Step 4: Set Your API Key
  • Step 5: Start Codex
  • Configuration Reference
  • Core Settings
  • OpenRouter Provider Block
  • Project Trust Levels
  • Why Use OpenRouter with Codex CLI?
  • Provider Failover
  • Organizational Controls
  • Usage Visibility
  • Model Flexibility
  • Troubleshooting
  • Resources
Working with Coding Agents

Codex CLI

Use Codex CLI with OpenRouter
Was this page helpful?
Previous

Cursor

Use Cursor with OpenRouter
Next
Built with

What is Codex CLI?

Codex CLI is OpenAI’s open-source local coding agent that runs in your terminal. It supports multiple model providers, including OpenRouter, so you can use OpenRouter’s unified API, provider failover, and organizational controls with Codex’s agentic coding workflows.

Quick Start

Step 1: Install Codex CLI

Follow the Codex CLI installation instructions to install the CLI on your system.

Step 2: Get Your OpenRouter API Key

  1. Sign up or log in at OpenRouter
  2. Navigate to your API Keys page
  3. Create a new API key
  4. Copy your key (starts with sk-or-...)

Step 3: Configure Codex for OpenRouter

Codex uses a config.toml file, typically located at ~/.codex/config.toml. Create or edit this file with the following configuration:

1model_provider = "openrouter"
2model_reasoning_effort = "high"
3model="~openai/gpt-latest"
4
5[model_providers.openrouter]
6name = "openrouter"
7
8base_url="https://openrouter.ai/api/v1"
9env_key="OPENROUTER_API_KEY"

Step 4: Set Your API Key

Export your OpenRouter API key in your shell profile:

$# Add to ~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish
$export OPENROUTER_API_KEY="sk-or-..."

Codex reads the API key from the environment variable specified in env_key (default: OPENROUTER_API_KEY). Ensure this is set before starting Codex.

Step 5: Start Codex

Navigate to your project directory and run:

$cd /path/to/your/project
$codex

Your requests will now be routed through OpenRouter.

Configuration Reference

Core Settings

SettingDescriptionExample
model_providerProvider to use for model requests"openrouter"
modelOpenRouter model ID"~openai/gpt-latest"
model_reasoning_effortReasoning effort level for Codex models"low", "medium", "high", "xhigh"
show_raw_agent_reasoningWhether to display reasoning tokens in the UItrue or false
personalityAgent personality preset"pragmatic", "helpful", etc.

OpenRouter Provider Block

1[model_providers.openrouter]
2name = "openrouter"
3base_url = "https://openrouter.ai/api/v1"
4env_key = "OPENROUTER_API_KEY"
  • base_url: OpenRouter API endpoint. Use https://openrouter.ai/api/v1 for production.
  • env_key: Environment variable name for your API key.

Project Trust Levels

Codex supports per-project trust levels. Add project paths to control what the agent can access:

1[projects."/path/to/trusted/project"]
2trust_level = "trusted"
3
4[projects."/path/to/untrusted/project"]
5trust_level = "untrusted"
  • trusted: Agent has full access (e.g., run commands, edit files).
  • untrusted: Agent has restricted access for safety.

Why Use OpenRouter with Codex CLI?

Provider Failover

OpenRouter routes requests across multiple providers. If one provider is unavailable or rate-limited, OpenRouter can fail over to another, keeping your coding sessions uninterrupted.

Organizational Controls

For teams, OpenRouter provides centralized budget management. Set spending limits, allocate credits, and prevent unexpected cost overruns across developers using Codex.

Usage Visibility

Track Codex usage in real-time via the OpenRouter Activity Dashboard. Monitor costs, token usage, and request patterns.

Model Flexibility

Point model at any OpenRouter slug (e.g. ~openai/gpt-latest, ~anthropic/claude-sonnet-latest) or a pinned version to switch models without changing your Codex installation—just update config.toml.

Troubleshooting

  • Auth Errors: Ensure OPENROUTER_API_KEY is set and valid. Check at openrouter.ai/keys.
  • Model Not Found: Verify the model ID on openrouter.ai/models. Use the exact format (e.g., ~openai/gpt-latest).
  • Privacy: OpenRouter does not log your source code prompts unless you opt-in to prompt logging. See our Privacy Policy for details.

Resources

  • Codex CLI on GitHub
  • OpenRouter Codex Models
  • OpenRouter Activity Dashboard
  • OpenRouter API Documentation