在 Codex CLI 中使用 oairoute
- OpenAI 官方的命令行编程助手
- 支持通过配置覆盖 API 端点
- 与 Claude Code 用法接近,同为终端内的编码代理
第一步:获取 API Key
在 OAIRoute 控制台 创建。
Create one in the OAIRoute console.
第二步:安装
npm install -g @openai/codexnpm install -g @openai/codex第三步:配置端点与密钥
编辑 ~/.codex/config.toml(Windows 在 %USERPROFILE%\.codex\config.toml):
model = "gpt-4.1"
model_provider = "oairoute"
[model_providers.oairoute]
name = "oairoute"
base_url = "https://api.oairoute.com/v1"
env_key = "OAIROUTE_API_KEY"然后在环境变量里放入密钥:
你的 OAIRoute Key。变量名要与配置文件里的 env_key 一致。
export OAIROUTE_API_KEY="sk-xxxxxxxx"Windows PowerShell:
[Environment]::SetEnvironmentVariable("OAIROUTE_API_KEY", "sk-xxxxxxxx", "User")Edit ~/.codex/config.toml (on Windows, %USERPROFILE%\.codex\config.toml):
model = "gpt-4.1"
model_provider = "oairoute"
[model_providers.oairoute]
name = "oairoute"
base_url = "https://api.oairoute.com/v1"
env_key = "OAIROUTE_API_KEY"Then put the key in an environment variable:
Your OAIRoute key. The variable name must match env_key in the config file.
export OAIROUTE_API_KEY="sk-xxxxxxxx"Windows PowerShell:
[Environment]::SetEnvironmentVariable("OAIROUTE_API_KEY", "sk-xxxxxxxx", "User")第四步:验证
codex "解释一下当前目录的项目结构"codex "Explain the project structure in the current directory"配置要点
| 字段 | 说明 |
|---|---|
model | 默认模型,可被命令行 --model 覆盖 |
base_url | 必须是完整端点,带 /v1 |
env_key | 环境变量名,不是密钥本身 |
wire_api | 可选,chat 表示用 Chat Completions,responses 表示用 Responses |
env_key 里填的是变量名,不是 sk-xxx 本身。这个字段最容易写错,写错后表现是「已认证但一直 401」。
| Field | Description |
|---|---|
model | Default model; can be overridden with --model on the command line |
base_url | Must be the full endpoint, including /v1 |
env_key | The name of the environment variable, not the key itself |
wire_api | Optional; chat uses Chat Completions, responses uses Responses |
env_key holds the variable name, not the sk-xxx string itself. This is the easiest field to get wrong; the symptom is "authenticated but permanently 401".
常见问题
提示 401
环境变量没生效,或 env_key 与变量名不一致。重开终端后再试。
提示 404
base_url 少了 /v1,或者多了结尾斜杠。
工具调用报错
改用 wire_api = "chat"。部分模型的 Responses 结构支持还不完整。
401 error
The environment variable didn't take effect, or env_key doesn't match the variable name. Reopen the terminal and try again.
404 error
base_url is missing /v1, or has a trailing slash.
Tool calls error out
Switch to wire_api = "chat". Some models don't fully support the Responses schema yet.