OpenAI Codex CLI – 入门指南

2025-08-15 208 0

Overview  概述

OpenAI Codex CLI is an open‑source command‑line tool that brings the power of our latest reasoning models directly to your terminal. It acts as a lightweight coding agent that can read, modify, and run code on your local machine to help you build features faster, squash bugs, and understand unfamiliar code. Because the CLI runs locally, your source code never leaves your environment unless you choose to share it.
OpenAI Codex CLI 是一款开源命令行工具,将我们最新的推理模型能力直接带到您的终端。它作为一个轻量级编程助手,可以在本地机器上读取、修改和运行代码,帮助您更快地构建功能、修复错误以及理解陌生代码。由于 CLI 在本地运行,您的源代码永远不会离开您的环境,除非您选择共享它。

For more details about Codex, please check out the Github repo directly - https://github.com/openai/codex.
有关 Codex 的更多详情,请直接查看 Github 代码库 - https://github.com/openai/codex。

Key Functionality  核心功能

  • Zero‑setup installation – a single npm install -g @openai/codex (or codex --upgrade) gets you started.
    零配置安装——只需一个 npm install -g @openai/codex (或 codex --upgrade )即可快速上手
  • Multimodal inputs – pass text, screenshots, or diagrams and let the agent generate or edit code accordingly.
    多模态输入——支持传递文本、截图或图表,让智能体据此生成或编辑代码
  • Rich approvals workflow – choose how hands‑on you want to be with three distinct modes (see Approval Modes).
    丰富的审批流程——通过三种独特模式自主掌控参与程度(详见审批模式)
  • Runs entirely in your terminal – perfect for quick iteration without switching contexts.
    完全在终端中运行——无需切换界面即可快速迭代的完美解决方案

Approval Modes  审批模式

Mode  模式What the Agent Can Do
代理功能
When to Use  使用场景
Suggest (default)  建议模式(默认)Read files. Proposes edits & shell commands, but requires your approval before making changes or executing commands.
读取文件。会建议编辑内容和 Shell 命令,但在实际修改文件或执行命令前需获得您的批准。
Safe exploration, code reviews, learning a codebase.
安全探索、代码审查、学习代码库。
Auto Edit  自动编辑Read and write files automatically. Still asks before running shell commands.
自动读写文件。执行 Shell 命令前仍会询问确认。
Refactoring or repetitive edits where you want to keep an eye on side‑effects.
需要关注副作用的代码重构或重复性编辑
Full Auto  全自动模式Read, write, and execute commands autonomously inside a sandboxed, network‑disabled environment scoped to the current directory.
在受限于当前目录的沙盒化、无网络环境中自主读取、写入和执行命令
Longer tasks like fixing a broken build or prototyping features while you grab a coffee.
耗时较长的任务,比如修复损坏的构建或原型功能开发,您可以趁此喝杯咖啡

Pro tip: Codex warns you before entering Auto Edit or Full Auto if the directory isn’t under version control.
专业提示:如果目录未纳入版本控制,Codex 会在进入自动编辑或全自动模式前发出警告。

Quick Start  快速入门

  1. Installnpm install -g @openai/codex  安装: npm install -g @openai/codex
  2. Authenticate: Export your OpenAI API key (export OPENAI_API_KEY="<OAI_KEY>").
    身份验证:导出您的 OpenAI API 密钥( export OPENAI_API_KEY="<OAI_KEY>" )。
  3. Run in Suggest mode: From your project root, type codex and ask for example: “Explain this repo to me.”
    在建议模式下运行:从项目根目录输入 codex 并提问,例如:"请向我解释这个代码仓库。"
  4. Switch modes: Add flags as needed:
    切换模式:根据需要添加标志:
    • codex --auto-edit
    • codex --full-auto
  5. Review outputs: Codex prints proposed patches and shell commands inline. Approve, reject, or tweak as desired.
    查看输出:Codex 会内联显示建议的补丁和 Shell 命令。根据需要批准、拒绝或调整。

FAQ  常见问题

Which models does Codex use?
Codex 使用哪些模型?

By default Codex targets o4‑mini for fast reasoning. You can specify any model available in the Responses API, e.g. codex -m o3. We’d also recommend verifying your developer account to start seeing chain of thought summaries in the API.
默认情况下,Codex 针对 o4-mini 进行快速推理。您可以指定 Responses API 中提供的任何模型,例如 codex -m o3 。我们还建议验证您的开发者账户,以便在 API 中查看思维链摘要。

Does Codex upload my code to OpenAI?
Codex 会将我的代码上传到 OpenAI 吗?

No. All file reads, writes, and command executions happen locally. Only your prompt, high‑level context, and optional diff summaries are sent to the model for generation.
不会。所有文件读取、写入和命令执行都在本地完成。只有您的提示词、高层级上下文及可选的差异摘要会被发送至模型用于生成内容。

How do I change approval modes?
如何更改审批模式?

Start Codex with the appropriate flag (--suggest--auto-edit--full-auto) or use the in‑session shortcut /mode to toggle.
启动 Codex 时添加相应参数( --suggest 、 --auto-edit 、 --full-auto )或使用会话内快捷键 /mode 进行切换。

What platforms are supported?
支持哪些平台?

Codex officially supports macOS and Linux. Windows support is experimental and may require WSL.
Codex 官方支持 macOS 和 Linux 系统。Windows 支持尚处于实验阶段,可能需要使用 WSL。

The agent is stuck or times out—what can I do?
代理程序卡住或超时了怎么办?

  • Ensure you have a stable internet connection for API calls.
    请确保网络连接稳定以进行 API 调用。
  • Try CTRL‑C to cancel the current step and ask Codex to continue.
    输入 CTRL‑C 可取消当前步骤并让 Codex 继续执行。
  • If using Full Auto mode, confirm that the sandbox has permission to access required directories.
    若使用全自动模式,请确保沙盒环境已获得访问必要目录的权限。

How do I update Codex?
如何更新 Codex?

Run codex --upgrade at any time to fetch the latest release.
随时运行 codex --upgrade 即可获取最新版本。

相关文章

Claude Code 的强大是因为sonet4模型强还是工具强?
OpenAI最强写代码AI智能体来了:周五ChatGPT上线Codex研究预览版
🚀 OpenAI 重磅发布 Codex:AI 编程助手新时代来临!

发布评论