返回工具目录
CLI / Agent

Codex CLI

OpenAI 终端编程(wire_api=responses,含 openrouter-free)

/v1/responses一键写入配置docs/ai-gateway-editor-codex.md
创建 API Key
Base URLhttps://api.4stoken.cn
鉴权方式Authorization: Bearer sk-...
协议/v1/responses
Download

下载安装教程

先安装工具本体,再运行 4SToken 配置脚本。Windows 与 macOS / Linux 分开操作。

安装前准备
准备已安装 Node.js LTS 与 npm
准备已执行 npm install -g @openai/codex
准备已创建 Portal Client Key
准备目标 modelCode 已在 Gateway 配置可用路由
Windows
1安装 Node.js LTS:winget install OpenJS.NodeJS.LTS,或从 nodejs.org 下载 LTS 安装包
2关闭并重新打开 PowerShell,确认 node -v 与 npm -v 可正常输出
3执行 npm.cmd uninstall -g codex 后,再执行 npm.cmd install -g @openai/codex
4确认 codex --version 可正常输出
5打开 PowerShell 执行本页 Windows 脚本
6脚本会写入 %USERPROFILE%\.codex\config.toml
macOS / Linux
1安装 Node.js LTS:macOS 可执行 brew install node;Linux 可使用发行版包管理器或 NodeSource LTS
2确认 node -v 与 npm -v 可正常输出
3执行 npm uninstall -g codex 后,再执行 npm install -g @openai/codex
4确认 codex --version 可正常输出
5执行本页 macOS / Linux 脚本写入 ~/.codex/config.toml
6需要 chat-only 上游时设置 GATEWAY_CODEX_WIRE_API=chat 后重跑脚本
Install

配置命令

将命令中的 YOUR_API_KEY 替换成你的 Client Key。

macOS / Linux
curl -fsSL https://4stoken.cn/docs/scripts/codex.sh | GATEWAY_BASE_URL='https://api.4stoken.cn' bash -s -- YOUR_API_KEY
Windows PowerShell
$f = Join-Path $env:TEMP 'codex.ps1'; Invoke-WebRequest -Uri 'https://4stoken.cn/docs/scripts/codex.ps1' -OutFile $f -UseBasicParsing; powershell -NoProfile -ExecutionPolicy Bypass -File $f -ApiKey YOUR_API_KEY
Verify

配置检查

按顺序检查:先测接口连通,再做模拟调用,最后回到具体软件核对配置是否真正生效。

连通性检查

先确认 Key、Base URL 与模型列表接口可达。

执行模型列表查询,确认返回 200 且能看到可用模型
curl https://api.4stoken.cn/v1/models -H "Authorization: Bearer YOUR_API_KEY"

模拟调用

绕开客户端先打一次最小请求,确认网关协议与模型路由正确。

执行最小请求,确认没有 401、403、404、模型不存在或无路由错误
curl https://api.4stoken.cn/v1/responses -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json" -d "{\"model\":\"gpt-5.5\",\"input\":\"hello\"}"

其他

回到具体软件内确认配置文件、界面设置或重启步骤已经生效。

执行 codex --version
执行 codex --profile openrouter-free
如默认线路失败,再用 codex --profile chat 验证 chat-only 上游
FAQ

常见问题

Windows 首次使用 Codex CLI 时,最常见的是 PowerShell 执行策略拦截 npm 生成的 codex.ps1

方案

永久允许当前用户运行本地脚本

如果你经常需要运行 Codex,可只修改当前用户的执行策略,不影响整台电脑的其他用户。

1

以管理员身份打开 PowerShell:右键点击开始菜单,选择 Windows PowerShell(管理员)。

2

执行下面命令。

3

提示时输入 Y 确认。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
4

关闭管理员窗口,重新打开普通 PowerShell。

5

codex --profile openrouter-free

问题

Windows PowerShell 提示禁止运行脚本

运行 codex --profile openrouter-free 时,PowerShell 报错无法加载 codex.ps1。

常见提示包含:因为在此系统上禁止运行脚本、about_Execution_Policies、PSSecurityException、UnauthorizedAccess。

无法加载文件 C:\Users\...\AppData\Roaming\npm\codex.ps1PSSecurityException / UnauthorizedAccess
Models

模型映射

Codex 模型名系统实际模型
gpt-5.5gpt-5.5
gpt-5.4gpt-5.4
gpt-5.4-minigpt-5.4-mini
gpt-5.4-nanogpt-5.4-nano
gpt-5.3-codexgpt-5.3-codex
claude-sonnet-4-6claude-sonnet-4-6
claude-opus-4-8claude-opus-4-8
claude-opus-4-7claude-opus-4-7
claude-opus-4-6claude-opus-4-6
claude-haiku-4-5claude-haiku-4-5
openrouter-freeopenrouter-free
Notes

接入边界

  • 脚本仅修改本机工具配置文件,可通过上方源码链接审计。
  • 执行前可设置 GATEWAY_BASE_URL 覆盖默认网关地址。
  • GET /v1/realtime 未开放,不影响编程工具接入。