For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主导航

命令行选项

Codex 终端客户端的选项和标志

如何阅读本参考资料

本页列出了文档中介绍的所有 Codex CLI 命令和标志。您可以使用交互式表格按键名或说明搜索。每个部分都会显示选项的成熟度,并标出已弃用的选项和有风险的组合。

CLI 的大多数默认设置继承自 ~/.codex/config.toml。您在命令行中传入的任何 -c key=value 配置覆盖项都会在本次调用中 优先生效。请参阅配置 基础以了解更多 信息。

全局标志

Key
--add-dir
Type / Values
path
Details
Key
--ask-for-approval, -a
Type / Values
untrusted | on-request | never
Details
Key
--cd, -C
Type / Values
path
Details
Key
--config, -c
Type / Values
key=value
Details
Key
--dangerously-bypass-approvals-and-sandbox, --yolo
Type / Values
boolean
Details
Key
--dangerously-bypass-hook-trust
Type / Values
boolean
Details
Key
--disable
Type / Values
feature
Details
Key
--enable
Type / Values
feature
Details
Key
--image, -i
Type / Values
path[,path...]
Details
Key
--local-provider
Type / Values
lmstudio | ollama
Details
Key
--model, -m
Type / Values
string
Details
Key
--no-alt-screen
Type / Values
boolean
Details
Key
--oss
Type / Values
boolean
Details
Key
--profile, -p
Type / Values
string
Details
Key
--remote
Type / Values
ws://host:port | wss://host:port | unix:// | unix://PATH
Details
Key
--remote-auth-token-env
Type / Values
ENV_VAR
Details
Key
--sandbox, -s
Type / Values
read-only | workspace-write | danger-full-access
Details
Key
--search
Type / Values
boolean
Details
Key
--strict-config
Type / Values
boolean
Details
Key
PROMPT
Type / Values
string
Details

这些选项适用于基础 codex 命令。大多数选项会传递给子命令; 例外情况请参阅上述说明或相应命令的帮助。对于传递给子命令的标志, 请以相应命令的帮助为准。例如,codex exec --oss ... 会将 --oss 应用于 exec

命令概览

“成熟度”列使用实验性、测试版、 稳定和已弃用等功能成熟度标签。请参阅功能成熟度, 了解这些标签的含义。

成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Experimental
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Experimental
Details
成熟度
Stable
Details
成熟度
Experimental
Details
成熟度
Experimental
Details
成熟度
Experimental
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Experimental
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Deprecated
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Experimental
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Stable
Details
成熟度
Stable
Details

命令详情

codex(交互式)

运行 codex 而不指定子命令,会启动交互式终端 UI(TUI)。智能体接受上述全局标志和图像附件。网页搜索默认使用缓存模式;使用 --search 可切换为实时浏览。若要更顺畅地在本地工作,请使用 --sandbox workspace-write --ask-for-approval on-request

使用 --remote ws://host:port--remote wss://host:port,将 TUI 连接到通过 codex app-server --listen ws://IP:PORT 启动的 App Server。对于本地 Unix 套接字,使用 --remote unix:// 可连接默认套接字,使用 --remote unix://PATH 可明确指定路径。当服务器要求使用不记名 Token 进行 WebSocket 身份验证时,请添加 --remote-auth-token-env <ENV_VAR>

codex app-server

在本地启动 Codex App Server。它主要用于开发和调试,可能会在不另行通知的情况下发生变化。

Key
--analytics-default-enabled
Type / Values
boolean
Details
Key
--code-mode-host
Type / Values
ws://HOST/PATH | wss://HOST/PATH
Details
Key
--listen
Type / Values
stdio:// | ws://IP:PORT | unix:// | unix://PATH | off
Details
Key
--stdio
Type / Values
boolean
Details
Key
--ws-audience
Type / Values
string
Details
Key
--ws-auth
Type / Values
capability-token | signed-bearer-token
Details
Key
--ws-issuer
Type / Values
string
Details
Key
--ws-max-clock-skew-seconds
Type / Values
number
Details
Key
--ws-shared-secret-file
Type / Values
absolute path
Details
Key
--ws-token-file
Type / Values
absolute path
Details
Key
--ws-token-sha256
Type / Values
hexadecimal SHA-256 digest
Details

codex app-server --listen stdio:// 保留默认通过 stdio 传输 JSONL 的行为,codex app-server --stdio 是该传输方式的别名。--listen ws://IP:PORT 为 App Server 客户端启用 WebSocket 传输。服务器接受 ws:// 格式的监听 URL;当客户端使用 wss:// 连接时,请使用 TLS 终止或安全代理。使用 --listen unix:// 可在 Codex 的默认 Unix 套接字上接受 WebSocket 握手,使用 --listen unix:///absolute/path.sock 则可选择套接字路径。如果您要为客户端绑定生成模式,请添加 --experimental 以包含受功能开关控制的字段和方法。

添加 --code-mode-host wss://code-mode.example.com/host,将 App Server 连接到 远程 Code Mode 主机,而不是启动本地主机。此出站连接 独立于 --listen,由 App Server 进程中的所有线程共享。 仅对 localhost 或通过 SSH 转发的主机使用 ws://

codex remote-control

运行 codex remote-control,在前台启动远程控制。 使用 codex remote-control start 启动本地 app-server 守护进程并启用远程控制, 使用 codex remote-control stop 停止该进程。 托管远程控制客户端和 SSH 远程工作流会使用这些命令; 构建本地协议客户端时, 这些命令不能替代 codex app-server --listen

守护进程运行后,使用 codex remote-control pair 创建并打印短期有效的手动配对码。 为任意远程控制命令添加 --json, 即可获得机器可读的输出。对于 pair,JSON 响应包含 pairingCodemanualPairingCodeenvironmentIdexpiresAt

codex app

在 macOS 或 Windows 上从终端启动 ChatGPT 桌面应用。 在 macOS 上,Codex 可以打开指定的工作空间路径; 在 Windows 上,Codex 会打印要打开的路径。

Key
--download-url
Type / Values
url
Details
Key
PATH
Type / Values
path
Details

codex app 会打开已安装的 ChatGPT 桌面应用;如果尚未安装, 则会启动安装程序。在 macOS 上,Codex 会打开指定的工作空间路径; 在 Windows 上,Codex 会打印安装后要打开的路径。

codex debug app-server send-message-v2

使用内置的 app-server 测试客户端,通过 app-server 的 V2 线程/轮次流程发送一条消息。

Key
USER_MESSAGE
Type / Values
string
Details

此调试流程以 experimentalApi: true 初始化,启动一个线程、发起一个轮次,并以流式方式传输服务器通知。使用此流程可在本地重现并检查 app-server 协议的行为。

codex debug models

以 JSON 格式打印 Codex 可见的原始模型目录。

Key
--bundled
Type / Values
boolean
Details

如果您只想查看当前二进制文件内置的目录,而不从远程模型端点刷新,请使用 --bundled

codex debug prompt-input

以 JSON 格式准确呈现模型实际可见的提示输入列表。 调试指令发现、会话上下文或提示构建时,可使用此命令。

Key
--image, -i
Type / Values
path[,path...]
Details
Key
PROMPT
Type / Values
string
Details

codex apply

将 Codex 云端聊天中的最新差异应用到本地代码仓库。您必须完成身份验证并拥有该聊天的访问权限。

Key
TASK_ID
Type / Values
string
Details

Codex 会打印已应用补丁的文件;如果 git apply 失败(例如因冲突),则会以非零退出码退出。

codex review

以非交互方式运行代码审查。请仅选择一个审查目标, 或将自定义审查指令作为提示传入。

Key
--base
Type / Values
branch
Details
Key
--commit
Type / Values
SHA
Details
Key
--strict-config
Type / Values
boolean
Details
Key
--title
Type / Values
string
Details
Key
--uncommitted
Type / Values
boolean
Details
Key
PROMPT
Type / Values
string | - (read stdin)
Details

--uncommitted--base--commit 和自定义 PROMPT 互斥。 --title 只能与 --commit 一起使用。

codex archivecodex unarchive

按会话 ID 或会话名称归档或恢复已保存的交互式会话。 如果您想清理会话选择器而不删除对话记录,请使用这些命令。 会话 ID 优先于会话名称。

codex archive <SESSION>
codex unarchive <SESSION>
Key
--remote
Type / Values
ws://host:port | wss://host:port | unix:// | unix://PATH
Details
Key
--remote-auth-token-env
Type / Values
ENV_VAR
Details
Key
SESSION
Type / Values
session ID | session name
Details

codex delete

按会话 ID 或会话名称永久删除已保存的交互式会话。 仅当您想删除对话记录,而不是将其从活跃会话列表中隐藏时, 才使用此命令。

codex delete <SESSION>
codex delete <SESSION_UUID> --force
Key
--force
Type / Values
boolean
Details
Key
--remote
Type / Values
ws://host:port | wss://host:port | unix:// | unix://PATH
Details
Key
--remote-auth-token-env
Type / Values
ENV_VAR
Details
Key
SESSION
Type / Values
session ID | session name
Details

--force 只能与会话 UUID 一起使用。通过名称指定会话时,仍需确认, 以免 Codex 在未提示的情况下删除重名或名称有歧义的会话。

codex cloud

在终端中与 Codex 云端聊天交互。默认命令会打开交互式选择器;codex cloud exec 会直接提交任务,codex cloud list 则返回最近的聊天,供脚本处理或快速查看。

Key
--attempts
Type / Values
1-4
Details
Key
--env
Type / Values
ENV_ID
Details
Key
QUERY
Type / Values
string
Details

身份验证使用与主 CLI 相同的凭据。如果任务提交失败,Codex 会以非零退出码退出。

codex cloud list

列出最近的云端聊天,并可按需进行筛选和分页。

Key
--cursor
Type / Values
string
Details
Key
--env
Type / Values
ENV_ID
Details
Key
--json
Type / Values
boolean
Details
Key
--limit
Type / Values
1-20
Details

纯文本输出先打印任务 URL,随后打印状态详情。自动化场景请使用 --json。JSON 负载包含一个 tasks 数组和一个可选的 cursor 值。每个任务都包含 idurltitlestatusupdated_atenvironment_idenvironment_labelsummaryis_reviewattempt_total

codex completion

生成 Shell 补全脚本并将输出重定向到适当位置,例如 codex completion zsh > "${fpath[1]}/_codex"

Key
SHELL
Type / Values
bash | zsh | fish | power-shell | elvish
Details

codex doctor

在提交支持请求前,或排查 Codex 安装故障时, 生成本地诊断报告。报告会检查安装、配置、身份验证、运行时、 Git、终端、app-server 和线程清单 是否正常。

Key
--all
Type / Values
boolean
Details
Key
--ascii
Type / Values
boolean
Details
Key
--json
Type / Values
boolean
Details
Key
--no-color
Type / Values
boolean
Details
Key
--summary
Type / Values
boolean
Details

codex features

管理存储在 $CODEX_HOME/config.toml 中的功能标志。enabledisable 命令会持久保存更改,使其应用于后续会话。 features 子命令不接受 --profile

Key
Disable subcommand
Type / Values
codex features disable <feature>
Details
Key
Enable subcommand
Type / Values
codex features enable <feature>
Details
Key
List subcommand
Type / Values
codex features list
Details

codex exec

对于应无需人工交互即可完成的脚本或 CI 运行,请使用 codex exec(或简写形式 codex e)。

Key
--cd, -C
Type / Values
path
Details
Key
--color
Type / Values
always | never | auto
Details
Key
--dangerously-bypass-approvals-and-sandbox, --yolo
Type / Values
boolean
Details
Key
--dangerously-bypass-hook-trust
Type / Values
boolean
Details
Key
--ephemeral
Type / Values
boolean
Details
Key
--full-auto
Type / Values
boolean
Details
Key
--ignore-rules
Type / Values
boolean
Details
Key
--ignore-user-config
Type / Values
boolean
Details
Key
--image, -i
Type / Values
path[,path...]
Details
Key
--json, --experimental-json
Type / Values
boolean
Details
Key
--local-provider
Type / Values
lmstudio | ollama
Details
Key
--model, -m
Type / Values
string
Details
Key
--oss
Type / Values
boolean
Details
Key
--output-last-message, -o
Type / Values
path
Details
Key
--output-schema
Type / Values
path
Details
Key
--profile, -p
Type / Values
string
Details
Key
--sandbox, -s
Type / Values
read-only | workspace-write | danger-full-access
Details
Key
--skip-git-repo-check
Type / Values
boolean
Details
Key
-c, --config
Type / Values
key=value
Details
Key
PROMPT
Type / Values
string | - (read stdin)
Details
Key
Resume subcommand
Type / Values
codex exec resume [SESSION_ID]
Details

Codex 默认输出经过格式化的内容。添加 --json 可接收以换行符分隔的 JSON 事件(每次状态变化对应一个事件)。可选的 resume 子命令可让您继续执行非交互式任务。使用 --last 选择当前工作目录下最近的会话,或添加 --all 以搜索所有会话:

Key
--all
Type / Values
boolean
Details
Key
--image, -i
Type / Values
path[,path...]
Details
Key
--last
Type / Values
boolean
Details
Key
PROMPT
Type / Values
string | - (read stdin)
Details
Key
SESSION_ID
Type / Values
uuid | session name
Details

codex execpolicy

保存 execpolicy 规则文件前,请先检查这些文件。codex execpolicy check 接受一个或多个 --rules 标志(例如,可指定 ~/.codex/rules 下的文件),并输出 JSON,显示最严格的判定结果以及所有匹配规则。添加 --pretty 可格式化输出。execpolicy 命令目前处于预览阶段。

Key
--pretty
Type / Values
boolean
Details
Key
--rules, -r
Type / Values
path (repeatable)
Details
Key
COMMAND...
Type / Values
var-args
Details

codex login

使用 ChatGPT 账户、API 密钥或访问令牌对 CLI 进行身份验证。不指定任何标志时,Codex 会打开浏览器以启动 ChatGPT OAuth 流程。

Key
--device-auth
Type / Values
boolean
Details
Key
--with-access-token
Type / Values
boolean
Details
Key
--with-api-key
Type / Values
boolean
Details
Key
status subcommand
Type / Values
codex login status
Details

如果存在凭据,codex login status 会以退出码 0 退出,这对自动化脚本很有用。

codex logout

移除已保存的 API 密钥凭据和 ChatGPT 身份验证凭据。此命令没有标志。

codex mcp

管理存储在 ~/.codex/config.toml 中的模型上下文协议服务器条目。

Key
add <name>
Type / Values
-- <command...> | --url <value>
Details
Key
get <name>
Type / Values
--json
Details
Key
list
Type / Values
--json
Details
Key
login <name>
Type / Values
--scopes scope1,scope2
Details
Key
logout <name>
Details
Key
remove <name>
Details

add 子命令同时支持 stdio 和可流式 HTTP 两种传输方式:

Key
--bearer-token-env-var
Type / Values
ENV_VAR
Details
Key
--env KEY=VALUE
Type / Values
repeatable
Details
Key
--oauth-client-id
Type / Values
CLIENT_ID
Details
Key
--oauth-resource
Type / Values
RESOURCE
Details
Key
--url
Type / Values
https://…
Details
Key
COMMAND...
Type / Values
stdio transport
Details

OAuth 操作(loginlogout)仅适用于可流式 HTTP 服务器,且服务器必须支持 OAuth。

codex plugin

安装、列出和移除来自已配置市场的插件。

Key
add <plugin[@marketplace]>
Type / Values
[--marketplace, -m NAME] [--json]
Details
Key
list
Type / Values
[--marketplace, -m NAME] [--available --json] [--json]
Details
Key
marketplace
Details
Key
remove <plugin[@marketplace]>
Type / Values
[--marketplace, -m NAME] [--json]
Details

codex plugin add --json 会打印 pluginIdnamemarketplaceNameversioninstalledPathauthPolicycodex plugin list --json 会打印 installedavailable 两个数组。条目包含 pluginIdnamemarketplaceNameversioninstalledenabledsourceinstallPolicyauthPolicy;如果可用,还包含 marketplaceSource,其中记录 已配置的市场源类型和值。codex plugin remove --json 会打印 pluginIdnamemarketplaceName

codex plugin marketplace

管理 Codex 可浏览并可从中安装插件的市场源。

Key
add <source>
Type / Values
[--ref REF] [--sparse PATH] [--json]
Details
Key
list
Type / Values
[--json]
Details
Key
remove <marketplace-name>
Type / Values
[--json]
Details
Key
upgrade [marketplace-name]
Type / Values
[--json]
Details

codex plugin marketplace add 接受 GitHub 简写形式,例如 owner/repoowner/repo@ref,也接受 HTTP 或 HTTPS Git URL、SSH Git URL 和本地市场根目录。 使用 --ref 固定 Git 引用,并重复指定 --sparse PATH, 对基于 Git 的市场代码仓库进行稀疏检出。

codex plugin marketplace list 会打印当前范围内的市场名称和根目录, 包括隐式发现的默认市场 以及已配置的市场快照。

在市场的 add、list、upgrade 或 remove 命令中添加 --json, 即可获得便于自动化处理的输出。市场 add 命令的 JSON 包含 marketplaceNameinstalledRootalreadyAdded;list 命令的 JSON 包含一个 marketplaces 数组, 其条目包含 nameroot 和可选的 marketplaceSource;upgrade 命令的 JSON 包含 selectedMarketplacesupgradedRootserrors;remove 命令的 JSON 包含 marketplaceNameinstalledRoot

codex mcp-server

codex mcp-server 已弃用。请改用 Codex App Server。要在 Claude Code 中调用 Codex,请使用 适用于 Claude Code 的 Codex 插件, 该插件使用 App Server。

对于现有集成,此命令会通过 stdio 将 Codex 作为 MCP 服务器运行,以供其他工具连接。它会继承全局配置覆盖项,并在下游客户端关闭连接时退出。

codex resume

按 ID 继续交互式会话,或恢复最近的聊天。除非您传入 --all,否则 codex resume 会将 --last 的范围限定为当前工作目录。它接受与 codex 相同的全局标志,包括模型和沙盒覆盖项。

如果当前工作目录与会话保存的目录不同, Codex 会询问要使用哪个目录。将 tui.resume_cwd 设置为 "current""session",即可沿用相应选择而不再提示。显式指定的 --cd-C) 覆盖项优先于 tui.resume_cwd

Key
--all
Type / Values
boolean
Details
Key
--include-non-interactive
Type / Values
boolean
Details
Key
--last
Type / Values
boolean
Details
Key
SESSION_ID
Type / Values
uuid | session name
Details

codex fork

从之前的交互式会话派生一个新聊天。默认情况下,codex fork 会打开会话选择器;添加 --last 则改为从您最近的会话派生。

当前工作目录与会话保存的目录不同时,codex fork 使用的 工作目录选择提示和 tui.resume_cwd 设置与 codex resume 相同。

Key
--all
Type / Values
boolean
Details
Key
--last
Type / Values
boolean
Details
Key
SESSION_ID
Type / Values
uuid
Details

codex sandbox

使用沙盒辅助工具,按 Codex 内部采用的同一套策略运行命令。

macOS seatbelt

Key
--allow-unix-socket
Type / Values
path
Details
Key
--cd, -C
Type / Values
DIR
Details
Key
--config, -c
Type / Values
key=value
Details
Key
--include-managed-config
Type / Values
boolean
Details
Key
--log-denials
Type / Values
boolean
Details
Key
--permission-profile, -P
Type / Values
NAME
Details
Key
--profile, -p
Type / Values
NAME
Details
Key
COMMAND...
Type / Values
var-args
Details

Linux Landlock

Key
--cd, -C
Type / Values
DIR
Details
Key
--config, -c
Type / Values
key=value
Details
Key
--include-managed-config
Type / Values
boolean
Details
Key
--permission-profile, -P
Type / Values
NAME
Details
Key
--profile, -p
Type / Values
NAME
Details
Key
COMMAND...
Type / Values
var-args
Details

Windows

Key
--cd, -C
Type / Values
DIR
Details
Key
--config, -c
Type / Values
key=value
Details
Key
--include-managed-config
Type / Values
boolean
Details
Key
--permission-profile, -P
Type / Values
NAME
Details
Key
--profile, -p
Type / Values
NAME
Details
Key
COMMAND...
Type / Values
var-args
Details

codex update

当已安装的版本支持自更新时,检查并应用 Codex CLI 更新。调试构建会打印一条消息,提示您改为安装发布版构建。

标志组合和安全提示

  • 对于可完全在工作空间内执行的无人值守本地任务,请使用 --sandbox workspace-write;除非您在专用沙盒虚拟机中运行,否则请避免使用 --dangerously-bypass-approvals-and-sandbox
  • 需要授予 Codex 更多目录的写入权限时,请优先使用 --add-dir,而不要强制使用 --sandbox danger-full-access
  • 在 CI 中搭配使用 --json--output-last-message,以捕获机器可读的进度信息和最终的自然语言摘要。

交互式快捷操作

  • 输入 @ 可在工作空间中搜索文件,并将其路径添加到提示中。
  • UpDown 可恢复历史草稿。
  • Ctrl+R 搜索提示历史记录,然后按 Enter 使用匹配项,或按 Esc 取消。
  • Ctrl+O 或运行 /copy,可复制 Codex 最近完成的输出。
  • 在行首添加 !,即可按当前审批和沙盒设置运行本地 Shell 命令。
  • Codex 工作期间,按 Tab 可将后续提示、斜杠命令或 Shell 命令排入队列,供下一轮处理。
  • Codex 工作期间,按 Enter 可向当前轮次添加新指令。
  • 编辑器为空时,按两次 Esc 可编辑上一条用户消息,并从该处派生聊天。
  • Ctrl+C 或运行 /exit 可关闭会话。
  • Codex CLI 概览:安装、升级和小技巧。
  • 基础配置:持久保存模型、提供商等默认设置。
  • 高级配置:配置方案、提供商、沙盒调优和集成。
  • AGENTS.md:从概念层面介绍 Codex 智能体的能力和最佳实践。