OpenAI Codex CLI Skill
Use OpenAI Codex CLI (codex) for coding tasks including code review, refactoring, bug fixes, CI repairs, and feature implementation. Codex CLI runs locally on your machine with full filesystem access.
When to Use
- - User asks for code changes, refactoring, or implementation
- CI/build failures need fixing
- Code review before commit/push
- Large codebase exploration or explanation
- Tasks requiring file editing + command execution
- When GPT-5-Codex model strengths are needed (code generation, tool use)
Installation & Auth
Codex CLI requires ChatGPT Plus/Pro/Business/Enterprise subscription.
CODEBLOCK0
Core Commands
Interactive Mode (TUI)
CODEBLOCK1
Non-Interactive (Scripting)
CODEBLOCK2
Session Management
CODEBLOCK3
Slash Commands (In TUI)
| Command | Purpose |
|---|
| INLINECODE1 | Switch model (gpt-5-codex, gpt-5) |
| INLINECODE2 |
Set approval mode (Auto, Read Only, Full Access) |
|
/review | Code review against branch, uncommitted changes, or specific commit |
|
/diff | Show Git diff including untracked files |
|
/compact | Summarize conversation to free context |
|
/init | Generate AGENTS.md scaffold |
|
/status | Show session config and token usage |
|
/undo | Revert most recent turn |
|
/new | Start fresh conversation |
|
/mcp | List configured MCP tools |
|
/mention <path> | Attach file to conversation |
Approval Modes
| Mode | Behavior |
|---|
| Auto (default) | Read/edit/run commands in workspace; asks for outside access |
| Read Only |
Browse files only; requires approval for changes |
|
Full Access | Full machine access including network (use sparingly) |
Key Flags
| Flag | Purpose |
|---|
| INLINECODE12 | Override model (gpt-5-codex, gpt-5) |
| INLINECODE13 |
Set working directory |
|
--add-dir <path> | Add additional writable roots |
|
--image, -i <path> | Attach image(s) to prompt |
|
--full-auto | Workspace write + approve on failure |
|
--sandbox <mode> | read-only, workspace-write, danger-full-access |
|
--json | Output newline-delimited JSON |
|
--search | Enable web search tool |
Clawdbot Integration Patterns
Pattern 1: Direct exec Tool
Call Codex from Clawdbot's exec tool for coding tasks:
CODEBLOCK4
Pattern 2: Subagent Delegation
Spawn a coding subagent that uses Codex:
CODEBLOCK5
Pattern 3: CLI Backend Fallback
Configure Codex as a text-only fallback:
CODEBLOCK6
Pattern 4: MCP Server Mode
Run Codex as an MCP server for other agents:
CODEBLOCK7
Clawdbot Config: OpenAI Codex Provider
Use your ChatGPT Pro subscription via the openai-codex provider:
CODEBLOCK8
Auth syncs automatically from ~/.codex/auth.json to Clawdbot's auth profiles.
Code Review Workflow
CODEBLOCK9
Multi-Directory Projects
CODEBLOCK10
Custom Slash Commands
Create reusable prompts in ~/.codex/prompts/:
CODEBLOCK11
Invoke: INLINECODE23
MCP Integration
Add MCP servers to extend Codex:
CODEBLOCK12
Web Search
Enable in ~/.codex/config.toml:
CODEBLOCK13
Then Codex can search for current docs, APIs, etc.
Best Practices
- 1. Start with
/init to create AGENTS.md with repo-specific instructions - Use
/review before commits for AI code review - Set
/approvals appropriately — Auto for trusted repos, Read Only for exploration - Use
--add-dir for monorepos instead of INLINECODE29 - Resume sessions to maintain context across coding sessions
- Attach images for UI work, design specs, error screenshots
Example Workflows
Fix CI Failure
CODEBLOCK14
Refactor Component
CODEBLOCK15
Implement Feature from Spec
CODEBLOCK16
Code Review PR
CODEBLOCK17
Troubleshooting
| Issue | Solution |
|---|
| Auth fails | Run codex logout then INLINECODE31 |
| Commands blocked |
Check
/approvals, may need
--full-auto |
| Out of context | Use
/compact to summarize |
| Wrong directory | Use
--cd flag or check
/status |
| Model unavailable | Verify subscription tier supports model |
References
OpenAI Codex CLI 技能
使用 OpenAI Codex CLI(codex)处理编码任务,包括代码审查、重构、错误修复、CI 修复和功能实现。Codex CLI 在本地机器上运行,具有完整的文件系统访问权限。
何时使用
- - 用户要求进行代码更改、重构或实现
- CI/构建失败需要修复
- 提交/推送前的代码审查
- 大型代码库探索或解释
- 需要文件编辑 + 命令执行的任务
- 需要 GPT-5-Codex 模型优势时(代码生成、工具使用)
安装与认证
Codex CLI 需要 ChatGPT Plus/Pro/Business/Enterprise 订阅。
bash
安装
npm i -g @openai/codex
认证(打开浏览器进行 OAuth)
codex login
或使用 API 密钥
printenv OPENAI
APIKEY | codex login --with-api-key
验证认证状态
codex login status
核心命令
交互模式(TUI)
bash
codex # 启动交互式终端界面
codex 解释这个代码库 # 使用提示词启动
codex --cd ~/projects/myapp # 设置工作目录
非交互模式(脚本化)
bash
codex exec 修复 CI 失败 # 运行并退出
codex exec --full-auto 添加输入验证 # 自动批准工作区写入
codex exec --json 列出所有 API 端点 # JSON 输出便于解析
codex exec -i screenshot.png 匹配此设计 # 带图片输入
会话管理
bash
codex resume # 从最近的会话中选择
codex resume --last # 继续最近的会话
codex resume
# 恢复特定会话
斜杠命令(TUI 中)
| 命令 | 用途 |
|---|
| /model | 切换模型(gpt-5-codex, gpt-5) |
| /approvals |
设置审批模式(自动、只读、完全访问) |
| /review | 针对分支、未提交更改或特定提交进行代码审查 |
| /diff | 显示 Git 差异,包括未跟踪文件 |
| /compact | 总结对话以释放上下文 |
| /init | 生成 AGENTS.md 脚手架 |
| /status | 显示会话配置和令牌使用情况 |
| /undo | 撤销最近的操作轮次 |
| /new | 开始新的对话 |
| /mcp | 列出已配置的 MCP 工具 |
| /mention | 将文件附加到对话中 |
审批模式
| 模式 | 行为 |
|---|
| 自动(默认) | 在工作区中读取/编辑/运行命令;外部访问需询问 |
| 只读 |
仅浏览文件;更改需要审批 |
| 完全访问 | 完全机器访问,包括网络(谨慎使用) |
关键标志
| 标志 | 用途 |
|---|
| --model, -m <model> | 覆盖模型(gpt-5-codex, gpt-5) |
| --cd, -C <path> |
设置工作目录 |
| --add-dir | 添加额外的可写根目录 |
| --image, -i | 将图片附加到提示词 |
| --full-auto | 工作区写入 + 失败时自动批准 |
| --sandbox | 只读、工作区写入、危险完全访问 |
| --json | 输出换行符分隔的 JSON |
| --search | 启用网络搜索工具 |
Clawdbot 集成模式
模式 1:直接 exec 工具
从 Clawdbot 的 exec 工具调用 Codex 处理编码任务:
bash
在 Clawdbot 会话中
exec codex exec --full-auto --cd ~/projects/medreport 修复 src/components 中的 TypeScript 错误
模式 2:子代理委派
生成使用 Codex 的编码子代理:
json5
// 在 agents.defaults 或按代理配置中
{
agents: {
list: [
{
id: coder,
workspace: ~/clawd-coder,
model: openai-codex/gpt-5.2, // 使用 Codex 认证
tools: {
allow: [exec, read, write, edit, apply_patch, process]
}
}
]
}
}
模式 3:CLI 后端回退
将 Codex 配置为纯文本回退:
json5
{
agents: {
defaults: {
cliBackends: {
codex-cli: {
command: codex,
args: [exec, --full-auto],
output: text,
sessionArg: null // Codex 管理自己的会话
}
}
}
}
}
模式 4:MCP 服务器模式
将 Codex 作为 MCP 服务器运行,供其他代理使用:
bash
codex mcp-server # 通过 stdio MCP 暴露 Codex 工具
Clawdbot 配置:OpenAI Codex 提供商
通过 openai-codex 提供商使用您的 ChatGPT Pro 订阅:
json5
{
agents: {
defaults: {
model: { primary: openai-codex/gpt-5.2 },
models: {
openai-codex/gpt-5.2: { alias: Codex },
anthropic/claude-opus-4-5: { alias: Opus }
}
}
}
}
认证自动从 ~/.codex/auth.json 同步到 Clawdbot 的认证配置文件。
代码审查工作流
bash
交互式审查
codex
/review # 选择:分支、未提交或特定提交
非交互式
codex exec 审查此 PR 相对于 main 分支的更改
多目录项目
bash
跨 monorepo 包工作
codex --cd apps/frontend --add-dir ../backend --add-dir ../shared
或在 TUI 中
codex --cd ~/projects/myapp --add-dir ~/projects/shared-lib
自定义斜杠命令
在 ~/.codex/prompts/ 中创建可重用的提示词:
markdown
description: 准备并打开草稿 PR
argument-hint: [BRANCH=] [TITLE=]
如果指定了 BRANCH,则创建分支 dev/$BRANCH。
使用清晰的消息暂存并提交更改。
使用标题 $TITLE 打开草稿 PR,或自动生成一个。
调用:/prompts:pr BRANCH=feature-auth TITLE=添加 OAuth 流程
MCP 集成
添加 MCP 服务器以扩展 Codex:
bash
添加 stdio 服务器
codex mcp add github -- npx @anthropic/mcp-server-github
添加 HTTP 服务器
codex mcp add docs --url https://mcp.deepwiki.com/mcp
列出已配置的
codex mcp list
网络搜索
在 ~/.codex/config.toml 中启用:
toml
[features]
websearchrequest = true
[sandboxworkspacewrite]
network_access = true
然后 Codex 可以搜索当前文档、API 等。
最佳实践
- 1. 从 /init 开始,创建包含仓库特定说明的 AGENTS.md
- 在提交前使用 /review 进行 AI 代码审查
- 适当设置 /approvals — 受信任仓库使用自动,探索时使用只读
- 对 monorepo 使用 --add-dir,而不是 danger-full-access
- 恢复会话 以在编码会话之间保持上下文
- 附加图片 用于 UI 工作、设计规范、错误截图
示例工作流
修复 CI 失败
bash
codex exec --full-auto CI 在 lint 步骤失败。修复所有 ESLint 错误。
重构组件
bash
codex exec --cd src/components 重构 UserProfile.tsx,使用 React Query 替代 useEffect 进行数据获取
根据规范实现功能
bash
codex exec -i spec.png --cd ~/projects/app 根据设计规范实现此功能
代码审查 PR
bash
codex exec 审查 main