Using Codex

CLI 功能

核对 CLI 中面向终端协作、上下文和任务执行的能力。

Codex 支持 chat 之外的 workflows。使用本指南了解每个 workflow 解锁什么能力,以及什么时候使用。

以 interactive mode 运行

Codex launches into a full-screen terminal UI that can 读取 your repository, make edits, 和 运行 commands as you iterate together。使用 it whenever you want a conversational workflow where you can 检查 Codex’s actions in real time.

codex

你也可以在 command line 中直接指定 initial prompt。

codex "Explain this codebase to me"

Session 打开后,你可以:

Send prompts, code snippets, 或 screenshots (see image inputs ) directly into the composer.

Watch Codex explain its plan 之前 making a change, 和 approve 或 reject steps inline.

读取 syntax-highlighted markdown code blocks 和 diffs in the TUI, then 使用 /theme 到 preview 和 save a preferred theme.

使用 /clear 到 wipe the terminal 和 启动 a fresh chat, 或 按 Ctrl+L 到 clear the screen 不使用 starting a 新的 conversation.

使用 /复制 或 按 Ctrl+O 到 复制 the latest completed Codex output。如果 a turn is still running, Codex copies the most 最近 finished output 而不是 in-progress text.

按 Tab while Codex is running 到 queue follow-up text, slash commands, 或 ! shell commands for the next turn.

Navigate draft history in the composer 使用 Up/Down;Codex restores prior draft text 和 image placeholders.

按 Ctrl+R 到 搜索 prompt history 从 the composer, then 按 Enter 到 accept a match 或 Esc 到 cancel.

按 Ctrl+C 或 使用 /exit 到 close the interactive session 当 you’re done.

恢复 conversations

Codex stores your transcripts locally so you can pick up where you left off 而不是 repeating context。使用 the 恢复 subcommand 当 you want 到 reopen an earlier thread 使用 the same repository state 和 instructions.

codex resume launches a picker of recent interactive sessions. Highlight a run to see its summary and press Enter to reopen it.

codex resume --all shows sessions beyond the current working directory, so you can reopen any local run.

codex resume --last skips the picker and jumps straight to your most recent session from the current working directory (add --all to ignore the current working directory filter).

codex resume <SESSION_ID> targets a specific run. You can copy the ID from the picker, /status, or the files under ~/.codex/sessions/.

Non-interactive automation runs can 恢复 too:

codex exec resume --last "Fix the race conditions you found" codex exec resume 7f9f9a2e-1b3c-4c7a-9b0e-.... "Implement the plan"

Each resumed 运行 keeps the original transcript, plan history, 和 approvals, so Codex can 使用 prior context while you supply 新的 instructions。Override working directory 使用 --cd 或 添加 extra roots 使用 --add-dir 如果 you need 到 steer the environment 之前 resuming.

将 TUI 连接到 remote app server

Remote TUI mode lets you 运行 the Codex app server on one machine 和 使用 the Codex terminal UI 从 another machine。启动 the app server 使用 a WebSocket listener:

codex app-server --listen ws://127.0.0.1:4500

Then 连接 the TUI 到 that endpoint:

codex --remote ws://127.0.0.1:4500

For access 从 another machine, bind the app server 到 a reachable interface 和 配置 WebSocket auth 之前 remote 使用:

TOKEN_FILE="$HOME/.codex/app-server-token" openssl rand -base64 32 > "$TOKEN_FILE" chmod 600 "$TOKEN_FILE" codex app-server --listen ws://0.0.0.0:4500 --ws-auth capability-token --ws-token-file "$TOKEN_FILE"

--remote accepts explicit ws://host:port, wss://host:port, unix://, and unix://PATH addresses. Use unix:// for Codex’s default local Unix socket or unix://PATH for an explicit local socket path. Plain WebSocket connections are appropriate for localhost and SSH port-forwarding workflows. For non-local clients, use WebSocket auth and put the connection behind TLS.

Codex supports these WebSocket authentication modes:

Capability token:启动 the server 使用 --ws-auth capability-token 和 either --ws-token-file /absolute/path 或 --ws-token-sha256 HEX.

Signed bearer token:启动 the server 使用 --ws-auth signed-bearer-token --ws-shared-secret-file /absolute/path, plus 可选 --ws-issuer, --ws-audience, 和 --ws-max-clock-skew-seconds.

The TUI sends the remote auth token as an Authorization:Bearer <token> header during the WebSocket handshake。Codex only accepts remote auth tokens over wss:// URLs 或 local-only ws:// URLs.

export CODEX_REMOTE_TOKEN="$(cat "$TOKEN_FILE")" codex --remote wss://remote-host:4500 --remote-auth-token-env CODEX_REMOTE_TOKEN

For SSH remote projects in the Codex app, 使用 Remote connections 。For managed remote-control clients, codex remote-control starts an app-server process with remote-control support enabled.

Models 和 reasoning

For most tasks in Codex, gpt-5.5 is the recommended model。It’s 打开AI’s newest frontier model for complex coding, computer 使用, knowledge work, 和 research workflows, 使用 stronger planning, tool 使用, 和 follow-through on multi-step tasks。For extra fast tasks, ChatGPT Pro subscribers have access 到 the GPT-5.3-Codex-Spark model in research preview.

切换 models mid-session 使用 the /model command, 或 specify one 当 launching the CLI.

codex --model gpt-5.5

Learn more about the models 可用 in Codex.

Feature flags

Codex includes a small 设置 of feature flags。使用 the features subcommand 到 inspect what’s 可用 和 到 持久化 changes in your configuration.

codex features list codex features enable unified_exec codex features disable shell_snapshot

codex features enable <feature> and codex features disable <feature> write to $CODEX_HOME/config.toml. The features subcommand doesn’t accept --profile.

Subagents

使用 Codex subagent workflows 到 parallelize larger tasks。For setup, role configuration ([agents] in config.toml), 和 examples, see Subagents .

Codex only spawns subagents 当 you explicitly ask it 到。Because each subagent does its own model 和 tool work, subagent workflows consume more tokens than comparable single-agent runs.

Image inputs

附加 screenshots 或 design specs so Codex can 读取 image details alongside your prompt。You can paste images into the interactive composer 或 provide files on command line.

codex -i screenshot.png "Explain this error"
codex --image img1.png,img2.jpg "Summarize these diagrams"

Codex accepts common formats such as PNG 和 JPEG。使用 comma-separated filenames for two 或 more images, 和 combine them 使用 text instructions 到 添加 context.

Image generation

Ask Codex 到 生成 或 edit images directly in the CLI。This works well for assets such as icons, banners, illustrations, sprite sheets, 和 placeholder art。如果 you want Codex 到 transform 或 extend an 已有 asset, 附加 a reference image 使用 your prompt.

You can ask in natural language 或 explicitly invoke the image generation skill by including $imagegen in your prompt.

Built-in image generation uses gpt-image-2, counts toward your general Codex usage limits, 和 uses included limits 3-5x faster on average than similar turns 不使用 image generation, depending on image quality 和 size。For details, see Pricing 。For prompting tips 和 model details, see the image generation guide .

如果要批量生成更多 images,请在 environment variables 中设置 OPENAI_API_KEY,并要求 Codex 通过 API 生成图片,这样会改按 API pricing 计费。

Syntax highlighting 和 themes

The TUI syntax-highlights fenced markdown code blocks 和 file diffs so code is easier 到 scan during reviews 和 debugging.

使用 /theme 到 打开 the theme picker, preview themes live, 和 save your selection 到 tui.theme in ~/.codex/config.toml。You can also 添加 custom .tmTheme files under $CODEX_HOME/themes 和 选择 them in picker.

运行 local code review

输入 /检查 in the CLI 到 打开 Codex’s 检查 presets。The CLI launches a dedicated reviewer that reads the diff you 选择 和 reports prioritized, actionable findings 不使用 touching your working tree。By 默认 it uses the 当前 session model;设置 review_model in config.toml 到 override.

检查 against a base branch lets you pick a local branch;Codex finds the merge base against its upstream, diffs your work, 和 highlights the biggest risks 之前 you 打开 a pull request.

检查 uncommitted changes inspects everything that’s staged, not staged, 或 not tracked so you can address issues 之前 committing.

检查 a commit lists 最近 commits 和 has Codex 读取 the exact change 设置 for the SHA you 选择.

Custom 检查 instructions accepts your own wording (例如, “Focus on accessibility regressions”) 和 runs the same reviewer 使用 that prompt.

Each 运行 shows up as its own turn in the transcript, so you can rerun reviews as the code evolves 和 compare the feedback.

Codex ships 使用 a first-party web 搜索 tool。For local tasks in the Codex CLI, Codex enables web 搜索 by 默认 和 serves results 从 a web 搜索 cache。The cache is an 打开AI-maintained index of web results, so cached mode returns pre-indexed results 而不是 fetching live pages。This reduces exposure 到 prompt injection 从 arbitrary live content, but you should still treat web results as untrusted。如果 you are using --yolo 或 another full access sandbox setting , web 搜索 默认s 到 live results。To fetch the most 最近 data, pass --search for a single 运行 或 设置 web_search = "live" in Config basics 。You can also 设置 web_search = "disabled" 到 turn the tool off.

You’ll see web_search items in the transcript 或 codex exec --json output whenever Codex looks something up.

使用 input prompt 运行

当 you just need a quick answer, 运行 Codex 使用 a single prompt 和 skip the interactive UI.

codex "explain this codebase"

Codex will 读取 working directory, craft a plan, 和 stream the response back 到 your terminal 之前 exiting。Pair this 使用 flags like --path 到 target a specific directory 或 --model 到 dial in the behavior up front.

Shell completions

Speed up everyday usage by installing the generated completion scripts for your shell:

codex completion bash codex completion zsh codex completion fish

运行 the completion script in your shell configuration file 到 设置 up completions for 新的 sessions。例如, 如果 you 使用 zsh, you can 添加 the following 到 the end of your ~/.zshrc file:

# ~/.zshrc eval "$(codex completion zsh)"

启动 a 新的 session, 输入 codex, 和 按 Tab 到 see the completions。如果 you see a command not found:compdef error, 添加 autoload -Uz compinit && compinit 到 your ~/.zshrc file 之前 the eval "$(codex completion zsh)" line, then restart your shell.

Approval modes

Approval modes define how much Codex can do 不使用 stopping for confirmation。使用 /permissions inside an interactive session 到 切换 modes as your comfort level changes.

Auto (默认) lets Codex 读取 files, edit, 和 运行 commands within working directory。It still asks 之前 touching anything outside that scope 或 using the network.

读取-only keeps Codex in a consultative mode。It can browse files but won’t make changes 或 运行 commands until you approve a plan.

Full Access grants Codex the ability 到 work across your machine, including network access, 不使用 asking。使用 it sparingly 和 only 当 you trust repository 和 task.

Codex always surfaces a transcript of its actions, so you can 检查 或 roll back changes 使用 your usual git workflow.

用脚本运行 Codex

Automate workflows 或 wire Codex into your 已有 scripts 使用 the exec subcommand。This runs Codex non-interactively, piping the final plan 和 results back 到 stdout.

codex exec "fix the CI failure"

Combine exec 使用 shell scripting 到 build custom workflows, such as 自动 updating changelogs, sorting issues, 或 enforcing editorial checks 之前 a PR ships.

使用 Codex cloud

The codex cloud command lets you triage and launch Codex cloud tasks without leaving the terminal. Run it with no arguments to open an interactive picker, browse active 或 finished tasks, 和 应用 the changes 到 your local project.

You can also 启动 a task directly 从 the terminal:

codex cloud exec --env ENV_ID "Summarize open bugs"

添加 --attempts (1–4) 到 request best-of-N runs 当 you want Codex cloud 到 生成 多个 solution。例如, codex cloud exec --env ENV_ID --attempts 3 "Summarize 打开 bugs".

Environment IDs come 从 your Codex cloud configuration—使用 codex cloud and press Ctrl+O 到 选择 an environment 或 the web dashboard 到 确认 the exact value。Authentication follows your 已有 CLI login, 和 command exits non-zero 如果 submission fails so you can wire it into scripts 或 CI.

Slash commands

Slash commands give you quick access 到 specialized workflows like /检查, /fork, /side, 或 your own reusable prompts。Codex ships 使用 a curated 设置 of built-ins, 和 you can create custom ones for team-specific tasks 或 personal shortcuts.

See the slash commands guide 到 browse the catalog of built-ins, learn how 到 author custom commands, 和 understand where they live on disk.

Prompt editor

当 you’re drafting a longer prompt, it can be easier 到 切换 到 a full editor 和 then send the result back 到 the composer.

In prompt input, 按 Ctrl+G 到 打开 the editor defined by the VISUAL environment variable (或 EDITOR 如果 VISUAL isn’t 设置).

Model Context Protocol (MCP)

连接 Codex 到 more tools by configuring Model Context Protocol servers。添加 STDIO 或 streaming HTTP servers in ~/.codex/config.toml, 或 manage them 使用 the codex mcp CLI commands—Codex launches them 自动 当 a session starts 和 exposes their tools next 到 the built-ins。You can even 运行 Codex itself as an MCP server 当 you need it inside another agent.

See Model Context Protocol 例如 configurations, 支持的 auth flows, 和 a more detailed guide.

Tips 和 shortcuts

输入 @ in the composer 到 打开 a fuzzy file 搜索 over workspace root;按 Tab 或 Enter 到 drop the highlighted path into your message.

按 Enter while Codex is running 到 inject 新的 instructions into the 当前 turn, 或 按 Tab 到 queue follow-up input for the next turn。Queued input can be a normal prompt, a slash command such as /检查, 或 a ! shell command。Codex parses queued slash commands 当 they 运行.

Prefix a line 使用 ! 到 运行 a local shell command (例如, !ls)。Codex treats the output like a user-provided command result 和 still applies your approval 和 sandbox settings.

Tap Esc twice while the composer is empty 到 edit your previous user message。Continue pressing Esc 到 walk further back in the transcript, then hit Enter 到 fork 从 that point.

Launch Codex 从 any directory using codex --cd <path> 到 设置 the working root 不使用 running cd first。The active path appears in the TUI header.

Expose more writable roots 使用 --add-dir (例如, codex --cd apps/frontend --add-dir ../backend --add-dir ../shared) 当 you need 到 coordinate changes across 多个 project.

Make sure your environment is already 设置 up 之前 launching Codex so it doesn’t spend tokens probing what 到 activate。例如, source your Python virtual environment (或 other language environments), 启动 any 必需 daemons, 和 export the environment variables you expect 到 使用 ahead of time.

站内延伸阅读