Codex CLI Cheatsheet - OpenAI Terminal Coding Agent Full Reference
Codex CLI is OpenAI official open-source terminal coding agent (Rust) for developers driving GPT-family models to edit code, run tests, and manage approvals directly in a repo — and for CI engineers adopting AI codegen/review. Unlike cloud chat it adds fine-grained sandbox (read-only/workspace-write/full-access) and approval policies, plus headless codex exec --json for pipelines. This sheet covers install/login, non-interactive execution, and the sandbox trade-offs.
Typical Use Case
Codex CLI shines at headless automation: codex exec --json turns one-shot jobs (code review, batch comments, config extraction) into parseable JSONL for CI, no TUI babysitting. Interactively it works like a coachable assistant — it shows step state (Thinking/Looking up/Editing/Finished) and uses -a approvals to gate file edits and commands. It is strong at reading large repos in a local sandbox, supports local open-source models via --oss, and AGENTS.md encodes team conventions. For orgs, -p profiles plus config.toml pin a shared default config across a team.
Install & Auth 7
npm install -g @openai/codexbrew install --cask codexcodex --versioncodex logincodex login --with-api-keycodex login --device-authcodex logoutInteractive Mode 5
codexcodex "Explain this codebase"codex --search "Fix the login bug"codex -i ./screenshot.png "Implement this UI"codex --ossNon-interactive (codex exec) 5
codex exec "Fix the failing tests"echo "Refactor utils" | codex exec --osscodex exec --json "List all API endpoints"codex exec -o result.txt "Summarize the architecture"codex exec --output-schema '{...}' "Extract config"Session Management 5
codex resumecodex forkcodex applycodex --restorecodex --historyModel & Config 5
codex -m gpt-5.4 "Complex task"codex -p openai "Generate a component"codex -C /path/to/repocodex -ccodex completion bashSandbox & Approval 8
codex -s read-onlycodex -s workspace-writecodex -s danger-full-accesscodex -a untrustedcodex -a nevercodex --full-auto "Complete the implementation"codex --yolo "Emergency fix"codex --dangerously-bypass-approvals-and-sandboxMCP & Codex Cloud 7
codex mcp listcodex mcp add -- <args...>codex mcp add --url https://example.com/mcpcodex mcp get <name>codex mcp remove <name>codex cloud exec --env "task"codex cloud exec --env --attempts 3 "task"Interactive Slash Commands 12
/clear/compact/new/fork/diff/status/init/review/plan/model/permissions/exitCommand Examples
Run headless and emit JSON
codex exec --json "列出项目里所有 API 端点"
codex exec -o endpoints.txt "总结架构"codex exec(别名 codex e)是非交互事实标准,--json 输出 JSONL,-o 把最终回答存为文件。
Output
{"findings":{"endpoint_count":23,"categories":[...]}}
(最终回答同时写入 endpoints.txt,JSONL 便于 CI 解析)Fully auto-complete an implementation
codex --full-auto "补齐 payment 模块的单测,改动前先看现有测试风格"默认 --full-auto 等价于 workspace-write 沙箱 + on-failure 批准,安全且省心,是日常首选。
Output
(模型在禁用网络的沙箱内自行读写工作目录,仅在需要联网装依赖时才退出沙箱询问)
Explore a large repo read-only
codex -a never -s read-only "这个项目的构建流程和依赖拓扑是怎样的"适合审计、调研、理解历史演进;如需写文件再切回 workspace-write 并加批准。
Output
(只读模式可读取全系统文件,不落任何修改、不弹确认)
Add a stdio MCP server
codex mcp add -- npx -y @modelcontextprotocol/server-github
codex mcp list__ 之后的所有参数原样传给服务器,可让模型在会话里直接调用仓库/工具接口。
Output
Added MCP server "server-github" (stdio)
Common Pitfalls
- --dangerously-bypass-approvals-and-sandbox and --yolo skip all sandbox and approvals so the model can modify the system and run any command — only in isolated/throwaway environments.
- Only -a never + -s read-only is truly read-only; -a never alone can still let the model edit files in the working directory.
- Permission rules written into AGENTS.md make Codex follow team conventions by default but also lock in behavior — confirm impact before editing memory files.
- codex exec runs unattended with no human approvals, so side-effecting commands rely on the sandbox to bound scope (workspace-write), not on -a prompts.
Tips
- For non-interactive automation always use `codex exec` + `--json` with CI pipelines for codegen/review — Codex's signature use.
- The default `--full-auto` auto-approves inside a network-disabled sandbox: safe and hands-off; it only exits the sandbox to ask when it needs network (e.g. npm install).
- Put project conventions in AGENTS.md (repo root or ~/.codex/); Codex merges and loads them automatically — more efficient than repeating them in prompts.
- For read-only exploration use `-a never -s read-only`: no prompts yet full file read access, ideal for audits/research.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Aug 5, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us