概述
Codex CLI 是 OpenAI 推出的官方命令行 AI 编码助手,基于强大的 GPT 模型家族构建。与普通的 ChatGPT 不同,Codex 针对代码生成和开发工作流进行了优化,具备智能代码生成与重构、跨文件理解、终端命令执行等能力。2026 年它一次性新增了 Computer Use(全系统控制)、Memory、ImageGen 以及 90+ 插件生态,从简单的代码补全进化为 Agent 化编码工具。
核心特性:OpenAI 官方产品、GPT 驱动、Agent 化编码、丰富的插件生态。
安装
Codex CLI 通过 npm 全局安装,需要 Node.js >= 22。如果默认源较慢,可以切换到镜像源加速下载。
# Install Codex CLI globally (requires Node.js >= 22)
npm install -g @openai/codex
# Speed up with a mirror (optional)
npm install -g @openai/codex --registry=https://registry.npmmirror.com
# Verify the installation
codex --version前置条件:必须安装 Node.js(>= 22)。用 node -v 检查。
基本用法
安装后需要配置 OpenAI API key。在终端运行 codex 启动交互式会话,或直接在 VS Code 集成终端中启动,让它在你的编辑器环境中处理跨文件重构等复杂任务。
# Configure the API key
export OPENAI_API_KEY="sk-your-key"
# Start an interactive session
codex
# Run a task directly
codex "Refactor this React component to use Hooks"
# Launch inside the VS Code integrated terminal to pair with the editor在 Windows 上,用 PowerShell 设置环境变量:$env:OPENAI_API_KEY="sk-..."。
技巧
2026 年 Codex 新增了 /goal 命令用于设定长期目标,以及 Memory 系统用于跨会话记忆。其插件生态可扩展能力(如浏览器控制、数据库操作)。对于复杂的跨文件重构,先让它阅读相关文件再下达指令;对于敏感项目,注意 API key 的权限范围和用量计费。
# Set a long-term goal
/goal Refactor the entire auth module to OAuth2
# List available plugins
/plugins
# Specify a model
codex --model gpt-4.1提示:OpenAI 按用量计费;复杂任务会消耗大量 token,请留意用量。
Configuration
Codex CLI is configured through the OPENAI_API_KEY environment variable, CLI flags, and an in-session /config command. Use --model to select a GPT model, and persist defaults in ~/.codex/config.toml. The /goal command sets long-term objectives and the Memory system carries context across sessions. Plugins (listed via /plugins) extend capabilities such as browser control and database access.
# Authenticate
export OPENAI_API_KEY="sk-..."
# Pick a model
codex --model gpt-4.1
# Persist defaults in ~/.codex/config.toml
model = "gpt-4.1"
auto_mem = true
# In-session configuration
/config # open the settings panel
/plugins # list and manage plugins
/goal Refactor the auth module to OAuth2
# Windows (PowerShell)
$env:OPENAI_API_KEY="sk-..."OpenAI bills by usage; complex agent tasks can consume many tokens, so monitor usage in the dashboard.
FAQ
Common questions cover API key setup, model selection, plugin availability, cost, and the difference between Codex CLI and ChatGPT. Codex CLI is optimized for terminal-based coding workflows with agentic file editing and command execution, while ChatGPT is a general-purpose chat interface. Plugins and Computer Use extend Codex beyond code editing.
Q: How is Codex CLI different from ChatGPT?
A: Codex CLI is a terminal agent focused on code: it edits files, runs
commands, and uses plugins/Memory for agentic workflows. ChatGPT is a
general chat product.
Q: Can I use my own API key?
A: Yes—set OPENAI_API_KEY and Codex bills against your OpenAI account.
Q: How do I reduce token costs?
A: Use a smaller model for simple tasks, scope context tightly, and avoid
long-running autonomous loops on expensive models.
Q: Does it work on Windows?
A: Yes; set the key in PowerShell with $env:OPENAI_API_KEY="sk-..." and
run codex in the terminal or VS Code integrated terminal.
Q: What are plugins for?
A: Plugins add capabilities like browser control, database operations, and
third-party API calls; list them with /plugins.Keep an eye on the usage dashboard—agentic tasks can rack up tokens quickly.
Ready to try OpenAI Codex?
Visit the official site for the latest version and full documentation.
Visit OpenAI Codex