CODEX CLI配置和使用技巧
官方文档:https://github.com/openai/codex
安装命令:
Install using npm
npm install -g @openai/codex
npm i -g @openai/codex --registry=https://registry.npmmirror.com
codex 配置
官方配置文档:https://github.com/openai/codex/docs/config.md
单个CLI配置
配置 Codex 需要修改两个文件:~/.codex/config.toml 和 ~/.codex/auth.json。
打开config.toml,添加以下内容:
model_provider = "OpenAI"
model = "gpt-5.4"
model_reasoning_effort = "xhigh"
disable_response_storage = true
sandbox_mode = "workspace-write"
windows_wsl_setup_acknowledged = true
model_context_window = 1000000
model_auto_compact_token_limit = 900000
[features]
plan_tool = true
apply_patch_freeform = true
view_image_tool = true
unified_exec = false
streamable_shell = false
rmcp_client = true
[model_providers.OpenAI]
name = "OpenAI"
base_url = "https://api.asxs.top/v1"
wire_api = "responses"
env_key = "CCH_API_KEY"
requires_openai_auth = true
[sandbox_workspace_write]
network_access = true
[windows]
sandbox = "elevated"
- 打开auth.json文件,添加一下内容:
{
"OPENAI_API_KEY": "your-api-key-here"
}
多个Provider配置
比如你在不同的商家那里买了不同的KEY,对应不同的baseurl,则可以这样配置:
注意: 以下端点配置仅为示例,具体配置以当前可用端点为准。端点可能因服务状态、限流等因素变化,请根据实际情况调整。
toml
#:schema https://developers.openai.com/codex/config-schema.json
# === 主要配置 ===
model = "gpt-5.4"
model_provider = "OpenAI" # 默认使用 OpenAI provider
review_model = "gpt-5.4"
model_reasoning_effort = "xhigh"
model_context_window = 1000000
model_auto_compact_token_limit = 900000
disable_response_storage = true
approval_policy = "never"
profile = "auto-max"
file_opener = "vscode"
web_search = "cached"
suppress_unstable_features_warning = true
# === 历史记录 ===
[history]
persistence = "save-all"
# === TUI 配置 ===
[tui]
notifications = true
# === Shell 环境策略 ===
[shell_environment_policy]
inherit = "all"
# === 沙箱配置 ===
[sandbox_workspace_write]
network_access = true
# === 功能开关 ===
[features]
unified_exec = true
undo = true
apply_patch_freeform = true
# === 配置文件 ===
[profiles.auto-max]
approval_policy = "never"
sandbox_mode = "workspace-write"
[profiles.review]
approval_policy = "on-request"
sandbox_mode = "workspace-write"
# === 通知 ===
[notice]
hide_gpt5_1_migration_prompt = true
# === 模型提供商配置(多 Provider 支持)===
# Provider 1: gmn 端点(默认)
[model_providers.OpenAI]
name = "OpenAI"
base_url = "https://gmn.chuangzuoli.com" # 示例端点,以实际可用为准
wire_api = "responses"
requires_openai_auth = true
# Provider 2: asxs 端点(备用)
[model_providers.asxs]
name = "asxs"
base_url = "https://api.asxs.top/v1" # 示例端点,以实际可用为准
wire_api = "responses"
experimental_bearer_token = "sk-your-api-key-here" # 直接配置 API Key
# === 项目信任 ===
[projects."/opt/claude/mystocks_spec"]
trust_level = "trusted"
# === MCP 服务器 ===
[mcp_servers.gitnexus]
command = "gitnexus"
args = ["mcp"]
- 认证文件:
~/.codex/auth.json
安全提示: API Key 应妥善保管,以下仅为格式示例,请勿使用已泄露的密钥。
{
"OPENAI_API_KEY": "sk-your-openai-api-key-here",
"ASXS_API_KEY": "sk-your-asxs-api-key-here"
}
多 Provider 配置(新特性)
1. 认证方式对比
Codex 支持两种认证方式:
| 方式 | 配置字段 | 说明 | 适用场景 |
|---|---|---|---|
| 环境变量 | env_key = "ASXS_API_KEY" |
从环境变量读取 API Key | 推荐,更安全 |
| 直接配置 | experimental_bearer_token = "sk-xxx" |
直接在配置中存储 API Key | 方便,无需设置环境变量 |
| OpenAI 认证 | requires_openai_auth = true |
使用 auth.json 中的 OPENAI_API_KEY | 兼容 OpenAI 端点 |
2. 配置示例
方式 A: 使用 experimental_bearer_token(推荐,简单)
[model_providers.asxs]
name = "asxs"
base_url = "https://api.asxs.top/v1"
wire_api = "responses"
experimental_bearer_token = "sk-your-api-key-here"
方式 B: 使用 env_key(推荐,更安全)
[model_providers.asxs]
name = "asxs"
base_url = "https://api.asxs.top/v1"
wire_api = "responses"
env_key = "ASXS_API_KEY"
然后设置环境变量:
export ASXS_API_KEY="sk-your-api-key-here"
方式 C: 使用 requires_openai_auth(兼容 OpenAI)
[model_providers.gmn]
name = "gmn"
base_url = "https://gmn.chuangzuoli.com"
wire_api = "responses"
requires_openai_auth = true
API Key 从 ~/.codex/auth.json 中的 OPENAI_API_KEY 读取。
3. Provider 切换方法
# 使用默认 provider(配置文件中的 model_provider 值)
codex exec "你的问题"
# 切换到 asxs provider(单次)
codex exec -c model_provider=asxs "你的问题"
# 交互模式启动(使用默认 provider)
codex
# 交互模式启动(切换 provider)
codex -c model_provider=asxs
调试成功关键点 (Key Learnings)
1. 端点配置要点
| 配置项 | 说明 | 示例值 |
|---|---|---|
base_url |
API 端点地址(不含 /responses) |
https://gmn.chuangzuoli.com |
wire_api |
协议类型,必须为 responses |
responses |
requires_openai_auth |
使用 OpenAI 认证方式 | true |
env_key |
环境变量名称 | ASXS_API_KEY |
experimental_bearer_token |
直接配置 API Key | sk-xxx |
重要说明:
base_url不需要包含/v1或/responses后缀,Codex 会自动追加wire_api必须设置为"responses",Codex v0.98.0+ 不再支持标准的 OpenAI"chat"协议
2. 常见端点配置示例
以下为不同中转服务的配置示例,具体以当前可用端点为准:
方案 A: gmn 端点
[model_providers.OpenAI]
name = "OpenAI"
base_url = "https://gmn.chuangzuoli.com"
wire_api = "responses"
requires_openai_auth = true
方案 B: asxs 端点
[model_providers.asxs]
name = "asxs"
base_url = "https://api.asxs.top/v1"
wire_api = "responses"
experimental_bearer_token = "sk-your-api-key-here"
3. 端点可用性检测
在配置前,建议先检测端点可用性:
# 检查端点连通性(返回 401 表示端点可达,需要认证)
curl -s -o /dev/null -w "%{http_code}" "https://YOUR_ENDPOINT/v1/responses"
# 查询端点支持的模型
curl -s "https://YOUR_ENDPOINT/v1/models" -H "Authorization: Bearer YOUR_API_KEY"
运行时是这样(假设你配置的provider的名字是asxs):
codex -c model_provider=asxs
恢复历史对话
codex resume 默认按“当前工作目录(cwd)”过滤历史,所以你在另一个目录里看起来就像“没记录”。
可以先试这几个命令,查看本机一共有多少个记录:
codex resume --all # 显示所有历史(不按 cwd 过滤)
codex resume --last # 直接恢复最近一次
比如,使用codex resume --all,就可以看到下面的历史:

选中一个回车,就可以选择在哪里打开它

一个完整的配置文件示例:
以gpt-5.4为例:配置文件完整示例
#:schema https://developers.openai.com/codex/config-schema.json
# === 主要配置 ===
model = "gpt-5.4"
model_provider = "OpenAI"
review_model = "gpt-5.4"
model_reasoning_effort = "xhigh"
model_context_window = 1000000
model_auto_compact_token_limit = 900000
disable_response_storage = true
approval_policy = "never"
profile = "auto-max"
file_opener = "vscode"
web_search = "cached"
suppress_unstable_features_warning = true
[history]
persistence = "save-all"
[tui]
notifications = true
[shell_environment_policy]
inherit = "all"
[sandbox_workspace_write]
network_access = true
[features]
unified_exec = true
undo = true
apply_patch_freeform = true
[profiles.auto-max]
approval_policy = "never"
sandbox_mode = "workspace-write"
[profiles.review]
approval_policy = "on-request"
sandbox_mode = "workspace-write"
[notice]
hide_gpt5_1_migration_prompt = true
# === Provider 1: gmn 端点(默认)===
[model_providers.OpenAI]
name = "OpenAI"
base_url = "https://gmn.chuangzuoli.com"
wire_api = "responses"
requires_openai_auth = true
# === Provider 2: asxs 端点(备用)===
[model_providers.asxs]
name = "asxs"
base_url = "https://api.asxs.top/v1"
wire_api = "responses"
experimental_bearer_token = "sk-your-api-key-here"
# === 项目信任 ===
[projects."/opt/claude/mystocks_spec"]
trust_level = "trusted"
# === MCP 服务器 ===
[mcp_servers.gitnexus]
command = "gitnexus"
args = ["mcp"]
浙公网安备 33010602011771号