上手 · 装 CLI,跑通第一个本地循环Hands On · Install the CLI, Close the First Local Loop.
一条 npm 或 brew 命令装上,codex login 用 ChatGPT 账号登录 —— 订阅额度直接用,不用碰 API key。然后跑通第一个循环:用「目标、上下文、约束、完成标准」四件套说清一件活,先 /plan 看它的方案,放它动手,/diff 看结果。中途想改主意不用等:回车是当场纠偏,Tab 是排队下一轮。这一章还有会话的日常:/status 看配置、/compact 压上下文、/fork 岔开试另一条路、/side 问句闲话不弄脏正题。One npm or brew command installs it; codex login signs in with your ChatGPT account — subscription quota, no API key needed. Then close the first loop: state one job with goal, context, constraints, and done-when; check its plan with /plan; let it act; read /diff. Change your mind mid-run without waiting: Enter steers the current turn, Tab queues the next. Also the session's daily moves: /status for configuration, /compact to shrink context, /fork to branch an approach, /side for a quick aside that doesn't dirty the main thread.
9 分钟 · 初稿 2026.079 Min · Drafted 2026.07
四个面里,CLI 是理解 Codex 最快的入口 —— 装一条命令、登一次录、跑通一个循环,你就摸到了引擎本身,而不是某个界面。这一章带你走完这三步:用 ChatGPT 账号登录(不用碰 API key),用「目标、上下文、约束、完成标准」四件套说清第一件活,先看它的计划再放它动手;末了是会话的日常手感 —— 中途改主意、压上下文、岔开试另一条路。Of the four fronts, the CLI is the fastest way to understand Codex — one install command, one login, one closed loop, and you've touched the engine itself rather than some interface. This chapter walks the three steps: sign in with your ChatGPT account (no API key needed), state your first job with goal, context, constraints, and done-when, see its plan before letting it act; then the daily feel of a session — changing your mind mid-run, shrinking context, branching an approach.
— I
装上、登录:两条路,先走订阅这条Install and Sign In: Two Paths, Take the Subscription One.
CLI 的一句话定位是「跑在你终端里的轻量编码 agent」,一条命令装上:1注 1Note 1openai/codex 仓库(GitHub)—— Codex CLI 的一句话定位是「跑在你终端里的轻量编码 agent」,主体 Rust 写成;安装:npm install -g @openai/codex,或 brew install --cask codex,或 curl 官方安装脚本。仓库同时指路:要在 VS Code / Cursor / Windsurf 里用,装 IDE 扩展;要云端 agent,去 chatgpt.com/codex。截至 2026-07-17。openai/codex repo (GitHub) — the Codex CLI one-liner is 'lightweight coding agent that runs in your terminal,' written mostly in Rust; install with npm install -g @openai/codex, or brew install --cask codex, or the official curl install script. The repo also points: for VS Code / Cursor / Windsurf, install the IDE extension; for the cloud agent, go to chatgpt.com/codex. As of 2026-07-17.The CLI's one-liner is 'lightweight coding agent that runs in your terminal,' installed with one command:1注 1Note 1openai/codex 仓库(GitHub)—— Codex CLI 的一句话定位是「跑在你终端里的轻量编码 agent」,主体 Rust 写成;安装:npm install -g @openai/codex,或 brew install --cask codex,或 curl 官方安装脚本。仓库同时指路:要在 VS Code / Cursor / Windsurf 里用,装 IDE 扩展;要云端 agent,去 chatgpt.com/codex。截至 2026-07-17。openai/codex repo (GitHub) — the Codex CLI one-liner is 'lightweight coding agent that runs in your terminal,' written mostly in Rust; install with npm install -g @openai/codex, or brew install --cask codex, or the official curl install script. The repo also points: for VS Code / Cursor / Windsurf, install the IDE extension; for the cloud agent, go to chatgpt.com/codex. As of 2026-07-17.
三选一;之后 codex 启动交互界面Pick one; then codex launches the interactive UI
登录有两条路,选错会多花冤枉钱。用 codex login 走浏览器流程、以 ChatGPT 账号登录 —— 直接吃你的订阅额度,这是已经付着 ChatGPT 的人该走的路。另一条是 API key(把 key 管道给 codex login --with-api-key):按 token 计费、走标准 API 价,且不含云端功能(GitHub 审查、Slack 那些),它是为 CI 这类自动化环境准备的(第 8 章),不是日常。2注 2Note 2OpenAI Codex 文档 · Authentication —— 两种登录:用 ChatGPT 账号登录走订阅额度,用 API key 登录走按量计费。本地三面(桌面应用 / CLI / IDE)两种都支持;Codex cloud 必须用 ChatGPT 登录。CLI 用 codex login(浏览器流程),或把 key 通过 stdin 管道给 codex login --with-api-key;凭据缓存在 ~/.codex/auth.json(当密码看待)或系统钥匙串。API key 走标准 API 价、不含云端功能(GitHub 审查、Slack 等)。截至 2026-07-17。OpenAI Codex Docs · Authentication — two sign-ins: sign in with ChatGPT for subscription quota, or with an API key for usage-based billing. The three local fronts (desktop app / CLI / IDE) support both; Codex cloud requires ChatGPT sign-in. The CLI uses codex login (browser flow), or pipe the key through stdin to codex login --with-api-key; credentials cache in ~/.codex/auth.json (treat like a password) or the OS keyring. An API key uses standard API pricing and excludes cloud features (GitHub review, Slack, etc.). As of 2026-07-17.还有一条硬边界记牢:Codex cloud 只认 ChatGPT 登录 —— 想用第 7 章的云端,API key 不行。2注 2Note 2OpenAI Codex 文档 · Authentication —— 两种登录:用 ChatGPT 账号登录走订阅额度,用 API key 登录走按量计费。本地三面(桌面应用 / CLI / IDE)两种都支持;Codex cloud 必须用 ChatGPT 登录。CLI 用 codex login(浏览器流程),或把 key 通过 stdin 管道给 codex login --with-api-key;凭据缓存在 ~/.codex/auth.json(当密码看待)或系统钥匙串。API key 走标准 API 价、不含云端功能(GitHub 审查、Slack 等)。截至 2026-07-17。OpenAI Codex Docs · Authentication — two sign-ins: sign in with ChatGPT for subscription quota, or with an API key for usage-based billing. The three local fronts (desktop app / CLI / IDE) support both; Codex cloud requires ChatGPT sign-in. The CLI uses codex login (browser flow), or pipe the key through stdin to codex login --with-api-key; credentials cache in ~/.codex/auth.json (treat like a password) or the OS keyring. An API key uses standard API pricing and excludes cloud features (GitHub review, Slack, etc.). As of 2026-07-17.Two paths to sign in, and the wrong one costs you money. Use codex login for the browser flow with your ChatGPT account — it draws straight on your subscription quota, the path for anyone already paying for ChatGPT. The other is an API key (pipe the key to codex login --with-api-key): billed per token at standard API pricing, and excluding cloud features (GitHub review, Slack, and such) — it's for automation environments like CI (chapter 8), not daily work.2注 2Note 2OpenAI Codex 文档 · Authentication —— 两种登录:用 ChatGPT 账号登录走订阅额度,用 API key 登录走按量计费。本地三面(桌面应用 / CLI / IDE)两种都支持;Codex cloud 必须用 ChatGPT 登录。CLI 用 codex login(浏览器流程),或把 key 通过 stdin 管道给 codex login --with-api-key;凭据缓存在 ~/.codex/auth.json(当密码看待)或系统钥匙串。API key 走标准 API 价、不含云端功能(GitHub 审查、Slack 等)。截至 2026-07-17。OpenAI Codex Docs · Authentication — two sign-ins: sign in with ChatGPT for subscription quota, or with an API key for usage-based billing. The three local fronts (desktop app / CLI / IDE) support both; Codex cloud requires ChatGPT sign-in. The CLI uses codex login (browser flow), or pipe the key through stdin to codex login --with-api-key; credentials cache in ~/.codex/auth.json (treat like a password) or the OS keyring. An API key uses standard API pricing and excludes cloud features (GitHub review, Slack, etc.). As of 2026-07-17. One hard boundary to keep: Codex cloud only accepts ChatGPT sign-in — for chapter 7's cloud, an API key won't do.2注 2Note 2OpenAI Codex 文档 · Authentication —— 两种登录:用 ChatGPT 账号登录走订阅额度,用 API key 登录走按量计费。本地三面(桌面应用 / CLI / IDE)两种都支持;Codex cloud 必须用 ChatGPT 登录。CLI 用 codex login(浏览器流程),或把 key 通过 stdin 管道给 codex login --with-api-key;凭据缓存在 ~/.codex/auth.json(当密码看待)或系统钥匙串。API key 走标准 API 价、不含云端功能(GitHub 审查、Slack 等)。截至 2026-07-17。OpenAI Codex Docs · Authentication — two sign-ins: sign in with ChatGPT for subscription quota, or with an API key for usage-based billing. The three local fronts (desktop app / CLI / IDE) support both; Codex cloud requires ChatGPT sign-in. The CLI uses codex login (browser flow), or pipe the key through stdin to codex login --with-api-key; credentials cache in ~/.codex/auth.json (treat like a password) or the OS keyring. An API key uses standard API pricing and excludes cloud features (GitHub review, Slack, etc.). As of 2026-07-17.
— II
第一个循环:说清目标,先看计划,再放它动手The First Loop: State the Goal, See the Plan, Then Let It Act.
Codex 就算你 prompt 不完美也能干活,但在大仓库、高风险的活上,给它对的上下文和清晰的结构,结果稳得多。官方推荐的 prompt 有四件套:目标(要改 / 建什么)、上下文(哪些文件、文档、报错相关 —— 可以 @ 提及)、约束(要守的规范、架构、安全线)、完成标准(什么条件下算完:测试通过、行为变了、bug 不再复现)。3注 3Note 3OpenAI Codex 文档 · Best practices —— 大 / 复杂仓库里,最大的提升是给 Codex 对的上下文加清晰的结构。推荐 prompt 含四件:Goal(要改 / 建什么)、Context(哪些文件 / 文档 / 报错相关,可 @ 提及)、Constraints(要遵守的规范、架构、安全要求)、Done when(什么条件下算完 —— 测试通过、行为变了、bug 不再复现)。复杂或模糊的任务,先让它计划:/plan 或 Shift+Tab。截至 2026-07-17。OpenAI Codex Docs · Best practices — in large / complex repos, the biggest unlock is giving Codex the right context with a clear structure. A recommended prompt includes four things: Goal (what to change / build), Context (which files / docs / errors matter, @-mentionable), Constraints (standards, architecture, safety requirements to follow), Done when (what must be true — tests pass, behavior changed, bug no longer reproducing). For complex or ambiguous tasks, plan first: /plan or Shift+Tab. As of 2026-07-17.这四件里,完成标准最容易漏,却最要紧 —— 它是第 9 章验收的基线。Codex is useful even when your prompt isn't perfect, but on large repos and higher-stakes work, giving it the right context and a clear structure makes results far steadier. The officially recommended prompt has four parts: goal (what to change / build), context (which files, docs, errors matter — @-mentionable), constraints (standards, architecture, safety lines to follow), and done-when (what must be true: tests pass, behavior changed, bug no longer reproducing).3注 3Note 3OpenAI Codex 文档 · Best practices —— 大 / 复杂仓库里,最大的提升是给 Codex 对的上下文加清晰的结构。推荐 prompt 含四件:Goal(要改 / 建什么)、Context(哪些文件 / 文档 / 报错相关,可 @ 提及)、Constraints(要遵守的规范、架构、安全要求)、Done when(什么条件下算完 —— 测试通过、行为变了、bug 不再复现)。复杂或模糊的任务,先让它计划:/plan 或 Shift+Tab。截至 2026-07-17。OpenAI Codex Docs · Best practices — in large / complex repos, the biggest unlock is giving Codex the right context with a clear structure. A recommended prompt includes four things: Goal (what to change / build), Context (which files / docs / errors matter, @-mentionable), Constraints (standards, architecture, safety requirements to follow), Done when (what must be true — tests pass, behavior changed, bug no longer reproducing). For complex or ambiguous tasks, plan first: /plan or Shift+Tab. As of 2026-07-17. Of the four, done-when is the easiest to drop and the most important — it's the baseline chapter 9 reviews against.
提示词Prompt第一件活的四件套模板Four-part template for a first job
目标:<一句话说清要改什么、达到什么效果>。
上下文:相关的是 <目录 / 文件 / 报错>,可以 @ 提及。
约束:沿用现有风格;不要碰 <不许动的目录 / 依赖>;不要顺手重构无关代码。
完成标准:<一条能检验的标准 —— 做完要能通过>。
拿不准的地方列成问题问我,别自己猜。Goal: <one sentence on what to change and the outcome>.
Context: relevant are <dirs / files / error>, @-mentionable.
Constraints: follow existing style; don't touch <off-limits dirs / deps>; don't refactor unrelated code.
Done when: <one checkable standard the result must pass>.
List open questions instead of guessing.
目标:给用户导出加一个 CSV 选项,默认仍是 JSON。
上下文:相关的是 src/export/、src/api/export.ts,可以 @ 提及。
约束:沿用现有风格;不要碰鉴权和数据库层;不要顺手重构无关代码。
完成标准:CSV 导出的用例全部通过,已有 JSON 导出测试仍然全绿。
拿不准的地方列成问题问我,别自己猜。Goal: add a CSV option to the user export; default stays JSON.
Context: relevant are src/export/, src/api/export.ts, @-mentionable.
Constraints: follow existing style; don't touch the auth or database layer; don't refactor unrelated code.
Done when: all CSV-export cases pass, and existing JSON-export tests stay green.
List open questions instead of guessing.
复杂或模糊的活,先让它计划:/plan 或 Shift+Tab 进入计划模式,它会先收集上下文、把打算怎么做讲给你听,你批了它才动手。3注 3Note 3OpenAI Codex 文档 · Best practices —— 大 / 复杂仓库里,最大的提升是给 Codex 对的上下文加清晰的结构。推荐 prompt 含四件:Goal(要改 / 建什么)、Context(哪些文件 / 文档 / 报错相关,可 @ 提及)、Constraints(要遵守的规范、架构、安全要求)、Done when(什么条件下算完 —— 测试通过、行为变了、bug 不再复现)。复杂或模糊的任务,先让它计划:/plan 或 Shift+Tab。截至 2026-07-17。OpenAI Codex Docs · Best practices — in large / complex repos, the biggest unlock is giving Codex the right context with a clear structure. A recommended prompt includes four things: Goal (what to change / build), Context (which files / docs / errors matter, @-mentionable), Constraints (standards, architecture, safety requirements to follow), Done when (what must be true — tests pass, behavior changed, bug no longer reproducing). For complex or ambiguous tasks, plan first: /plan or Shift+Tab. As of 2026-07-17.动手之后,/diff 随时看它改了什么(连未跟踪的新文件也在)。5注 5Note 5OpenAI Codex 文档 · Slash commands(CLI)—— 键盘优先的会话控制:/plan 切计划模式、/diff 看含未跟踪文件的 Git diff、/status 看会话配置与 token 用量、/compact 压缩可见对话腾出 token、/fork 从当前对话岔出一条新对话、/side(/btw)开一个不弄脏正题的临时侧聊、/model 换模型、/review 审工作区、/init 生成 AGENTS.md、/usage 看用量。截至 2026-07-17。OpenAI Codex Docs · Slash commands (CLI) — keyboard-first session control: /plan switches to plan mode, /diff shows the Git diff including untracked files, /status shows session configuration and token usage, /compact summarizes the visible chat to free tokens, /fork branches a new chat from the current one, /side (/btw) opens an ephemeral side chat that doesn't dirty the main thread, /model switches models, /review reviews the working tree, /init scaffolds AGENTS.md, /usage shows usage. As of 2026-07-17.这就是最小的循环:说清目标 → 看计划 → 放它动手 → 看 diff。它是第 1 章那个「派活 + 验收」的单机版,后面所有章节都在放大它。For complex or ambiguous work, plan first: /plan or Shift+Tab enters plan mode, where it gathers context and tells you how it intends to proceed, and only acts once you approve.3注 3Note 3OpenAI Codex 文档 · Best practices —— 大 / 复杂仓库里,最大的提升是给 Codex 对的上下文加清晰的结构。推荐 prompt 含四件:Goal(要改 / 建什么)、Context(哪些文件 / 文档 / 报错相关,可 @ 提及)、Constraints(要遵守的规范、架构、安全要求)、Done when(什么条件下算完 —— 测试通过、行为变了、bug 不再复现)。复杂或模糊的任务,先让它计划:/plan 或 Shift+Tab。截至 2026-07-17。OpenAI Codex Docs · Best practices — in large / complex repos, the biggest unlock is giving Codex the right context with a clear structure. A recommended prompt includes four things: Goal (what to change / build), Context (which files / docs / errors matter, @-mentionable), Constraints (standards, architecture, safety requirements to follow), Done when (what must be true — tests pass, behavior changed, bug no longer reproducing). For complex or ambiguous tasks, plan first: /plan or Shift+Tab. As of 2026-07-17. After it acts, /diff shows what it changed anytime (untracked new files included).5注 5Note 5OpenAI Codex 文档 · Slash commands(CLI)—— 键盘优先的会话控制:/plan 切计划模式、/diff 看含未跟踪文件的 Git diff、/status 看会话配置与 token 用量、/compact 压缩可见对话腾出 token、/fork 从当前对话岔出一条新对话、/side(/btw)开一个不弄脏正题的临时侧聊、/model 换模型、/review 审工作区、/init 生成 AGENTS.md、/usage 看用量。截至 2026-07-17。OpenAI Codex Docs · Slash commands (CLI) — keyboard-first session control: /plan switches to plan mode, /diff shows the Git diff including untracked files, /status shows session configuration and token usage, /compact summarizes the visible chat to free tokens, /fork branches a new chat from the current one, /side (/btw) opens an ephemeral side chat that doesn't dirty the main thread, /model switches models, /review reviews the working tree, /init scaffolds AGENTS.md, /usage shows usage. As of 2026-07-17. That's the minimal loop: state the goal, see the plan, let it act, read the diff. It's the single-machine version of chapter 1's send-out-and-verify, and every later chapter scales it up.
— III
中途改主意,和会话的日常手感Changing Your Mind, and the Daily Feel of a Session.
它正在跑,你想到一句要补的,不用等它停:回车是 steer —— 把你这句并进当前这轮,改方向、补细节都行;Tab 是 queue —— 把这句留到下一轮再处理。4注 4Note 4OpenAI Codex 文档 · Prompting —— Codex 正在干活时,你不用等它跑完就能再发一条:Steer 把消息并进当前这轮(改方向、补细节),Queue 把消息留给下一轮。Codex CLI 里,干活时按回车 steer 当前轮,按 Tab 把消息 queue 到下一轮。截至 2026-07-17。OpenAI Codex Docs · Prompting — while Codex is working you can send another message without waiting: Steer folds it into the current run (change direction, add a detail), Queue saves it for the next run. In the Codex CLI, press Enter while it works to steer the current turn, or press Tab to queue the message for the next turn. As of 2026-07-17.这两个键区分了「现在就拐」和「这轮先跑完」,是长任务里最省心的两个动作:方向对就 queue、方向偏就 steer,不用打断重来。It's running and you think of something to add — don't wait for it to stop: Enter is steer, folding your line into the current run to change direction or add a detail; Tab is queue, saving the line for the next run.4注 4Note 4OpenAI Codex 文档 · Prompting —— Codex 正在干活时,你不用等它跑完就能再发一条:Steer 把消息并进当前这轮(改方向、补细节),Queue 把消息留给下一轮。Codex CLI 里,干活时按回车 steer 当前轮,按 Tab 把消息 queue 到下一轮。截至 2026-07-17。OpenAI Codex Docs · Prompting — while Codex is working you can send another message without waiting: Steer folds it into the current run (change direction, add a detail), Queue saves it for the next run. In the Codex CLI, press Enter while it works to steer the current turn, or press Tab to queue the message for the next turn. As of 2026-07-17. These two keys separate 'turn now' from 'let this run finish' — the two most reassuring moves in a long task: queue when the direction is right, steer when it drifts, no interrupt-and-restart.会话的其余日常靠斜杠命令,先认几个每天都用的:/status 看当前配置和 token 用量(撞额度前先看这个);/compact 把跑长了的对话压成摘要、腾出上下文,留给任务之间的自然断点用;/fork 从当前对话岔出一条新的、去试另一种方案而不丢原路;/side(或 /btw)开一个临时侧聊,问句闲话、查个小事,不弄脏正题的记录。5注 5Note 5OpenAI Codex 文档 · Slash commands(CLI)—— 键盘优先的会话控制:/plan 切计划模式、/diff 看含未跟踪文件的 Git diff、/status 看会话配置与 token 用量、/compact 压缩可见对话腾出 token、/fork 从当前对话岔出一条新对话、/side(/btw)开一个不弄脏正题的临时侧聊、/model 换模型、/review 审工作区、/init 生成 AGENTS.md、/usage 看用量。截至 2026-07-17。OpenAI Codex Docs · Slash commands (CLI) — keyboard-first session control: /plan switches to plan mode, /diff shows the Git diff including untracked files, /status shows session configuration and token usage, /compact summarizes the visible chat to free tokens, /fork branches a new chat from the current one, /side (/btw) opens an ephemeral side chat that doesn't dirty the main thread, /model switches models, /review reviews the working tree, /init scaffolds AGENTS.md, /usage shows usage. As of 2026-07-17.这些不是花活,是让一场长会话不至于越跑越乱的卫生习惯。The rest of a session runs on slash commands; meet the ones you'll use daily: /status shows current configuration and token usage (check it before you hit a limit); /compact summarizes a long chat to free context, saved for natural breaks between tasks; /fork branches a new chat from the current one to try another approach without losing the original path; /side (or /btw) opens an ephemeral side chat for a quick aside that doesn't dirty the main thread's record.5注 5Note 5OpenAI Codex 文档 · Slash commands(CLI)—— 键盘优先的会话控制:/plan 切计划模式、/diff 看含未跟踪文件的 Git diff、/status 看会话配置与 token 用量、/compact 压缩可见对话腾出 token、/fork 从当前对话岔出一条新对话、/side(/btw)开一个不弄脏正题的临时侧聊、/model 换模型、/review 审工作区、/init 生成 AGENTS.md、/usage 看用量。截至 2026-07-17。OpenAI Codex Docs · Slash commands (CLI) — keyboard-first session control: /plan switches to plan mode, /diff shows the Git diff including untracked files, /status shows session configuration and token usage, /compact summarizes the visible chat to free tokens, /fork branches a new chat from the current one, /side (/btw) opens an ephemeral side chat that doesn't dirty the main thread, /model switches models, /review reviews the working tree, /init scaffolds AGENTS.md, /usage shows usage. As of 2026-07-17. These aren't tricks; they're the hygiene that keeps a long session from turning to mud.动手 · 半小时跑通第一个循环:Hands-on · close the first loop in half an hour:
01
装上,用 ChatGPT 账号登录Install, sign in with your ChatGPT account
三条安装命令挑一条,codex login 走浏览器流程。用 /status 确认登的是订阅、不是 API key —— 别让第一件活就走了按量计费。Pick one of the three install commands, codex login through the browser flow. Use /status to confirm you're on the subscription, not an API key — don't let your first job run on usage-based billing.
02
用四件套派一件真活,先 /planDispatch a real job with the four parts, /plan first
挑一件跨两三个文件的中等改动,照上面的模板写。先 /plan 看它的方案,至少改一处再放它动手 —— 感受「审方案」和「审代码」的差别。Pick a medium change spanning two or three files and write it with the template above. /plan first to see its approach, change at least one thing before letting it act — feel the difference between reviewing an approach and reviewing code.
03
故意 steer 一次,再 /diff 收尾Deliberately steer once, then /diff to close
跑到一半回车补一句「顺便把这个也改了」,看它当场并进去;完事 /diff 通读它改的每一处。这套手感,就是你之后在四个面上派活的底子。Halfway through, press Enter to add 'also handle this,' and watch it fold in on the spot; when done, /diff through everything it changed. This feel is the foundation for dispatching work on all four fronts later.
装一条命令,登一次录,
跑通一个循环,你就懂了它.
One install, one login,
one closed loop — and you get it.
Aklman Library
— 讨论Discussion
讨论Discussion.
评论区初始化中…Initializing comments…
01 / 01
没有匹配结果No matches.
换个关键词,或按 Esc 回到页面Try another keyword, or press Esc to return