Codex CLI Cheat Sheet

Quick reference for codex, OpenAI's AI coding agent for the terminal. Install: npm install -g @openai/codex ยท Run: codex to start.

โญ Most Used Commands
Command Explanation Typical Usage
codex Start an interactive session in the current directory Beginning a coding session. Codex reads your files and waits for instructions
codex "prompt" Run a one-shot prompt and exit, no interactive session Quick tasks from the shell: codex "add error handling to server.py"
codex -a auto-edit Auto-apply file edits without asking; still prompts before running shell commands Faster editing sessions where you trust the changes but still want shell control
codex -a full-auto Fully autonomous. Applies edits and runs commands without prompting CI pipelines, sandboxed environments, or tasks you've already reviewed the approach for
โญ Most Used Slash Commands
Command Explanation Typical Usage
/resume Reload a previously saved session. Picks up context, files, and conversation history Continuing work from a previous day or after closing the CLI mid-task
/compact Summarises conversation history to free up context while keeping the working thread Session is getting long but the task isn't done. Preserve context without starting over
/plan Enter planning mode. Codex proposes an execution strategy and waits for approval before acting Before a risky or multi-step task; when you want to review the approach before anything is touched
/status Display current session config: model, approval mode, and token usage Quick check on what model you're using or how much context has been consumed
/new Start a fresh task in the same CLI instance. Clears conversation without restarting the process Moving to a completely unrelated task without closing and reopening the terminal
๐Ÿ’ฌ Other Slash Commands
/diff Review Git changes in the current repo before committing
/fast Toggle performance optimisation: faster responses on supported models
/fork Branch the current conversation to explore an alternative approach without losing the original thread
/model Switch AI model mid-session without restarting
/permissions Adjust approval requirements on the fly, e.g. switch between auto and read-only mid-session
/personality Change communication style: friendly, pragmatic, or neutral
๐Ÿ›ก๏ธ Approval Modes
suggest
Default. Codex proposes changes and explains what it would do. You review and apply manually. Nothing is written or run without your action. Safest mode for getting started.
auto-edit
Middle ground. File edits are applied automatically; shell commands still require approval. Good for iterative coding sessions where you want speed but keep control over what runs.
full-auto
Fully autonomous. All edits and commands are applied without asking. Use inside a sandbox or CI environment. On supported platforms Codex runs network-isolated by default.
๐Ÿšฉ CLI Flags
-a / --approval-mode Set approval mode: suggest, auto-edit, full-auto
-m / --model Model to use, e.g. codex-1, o4-mini, o3
-q / --quiet Suppress interactive UI. Pipe-friendly for scripting
--project-doc <path> Load a specific context file instead of AGENTS.md
--no-project-doc Skip loading AGENTS.md entirely
--dangerously-auto-approve-everything Skip all prompts. For trusted CI environments only
--version Print installed version and exit
--help / -h Show available flags and usage
๐Ÿ“„ AGENTS.md
Place an AGENTS.md at your project root to give Codex persistent context: stack, conventions, commands to know, things to avoid.
Codex reads AGENTS.md at the start of every session. It's the Codex equivalent of Claude Code's CLAUDE.md.
What to put in it: tech stack, test commands, code style rules, file layout, anything you'd tell a new engineer on day one.
Use --project-doc <path> to load a different file, or --no-project-doc to skip it entirely.
โš ๏ธ Keep AGENTS.md concise. Focus on non-obvious rules and constraints. Codex can already read your code.
โŒจ๏ธ Keyboard Shortcuts
Enter Submit message
Ctrl+C Interrupt / cancel the current operation
Ctrl+D Exit Codex
โ†‘ / โ†“ Navigate prompt history
h Show inline help during a session
q Quit the session
๐Ÿง  Models at a Glance
codex-1 Default. OpenAI's code-optimised agentic model. Best balance of speed, cost, and coding ability
o4-mini Fast, cost-effective reasoning. Good for straightforward tasks and scripting
o3 Most powerful reasoning. Use for complex architecture or hard debugging; slower and pricier
gpt-4.1 Latest GPT-4 generation. Strong general-purpose coding and instruction following
Switch with codex -m o3 "prompt" or set a default in ~/.codex/config.yaml.
โš™๏ธ Setup & Config
npm install -g @openai/codex Install Codex CLI globally
npm update -g @openai/codex Update to the latest version
API key: set OPENAI_API_KEY as an environment variable, or add it to ~/.codex/config.yaml.
Config file: ~/.codex/config.yaml. Set your default model, approval mode, and other preferences so you don't have to pass flags every time.
Example config.yaml:
model: codex-1
approvalMode: auto-edit
๐Ÿค– Scripting & Headless Use
Run one-shot tasks from the shell. Useful for CI, makefiles, and automation pipelines.
codex -q "prompt" Quiet mode: suppresses UI, outputs result only. Pipe-friendly.
Pipe input: cat file.py | codex -q "add docstrings to every function"
Redirect output: codex -q "generate tests for auth.py" > tests/test_auth.py
In CI, combine flags: codex -q -a full-auto --dangerously-auto-approve-everything "fix lint errors"
๐Ÿ’ก Tips & Patterns
Start with suggest mode while learning the tool. Switch to auto-edit once you're comfortable, and reserve full-auto for sandboxed CI or pre-reviewed tasks.
AGENTS.md is your first lever. Put project conventions, preferred libraries, and "never do X" rules in it. Codex reads it every session and it shapes every response.
Use --project-doc to switch contexts, e.g. one AGENTS file for backend work, another for frontend. Pass it per-invocation without touching the default file.
Sandbox full-auto. On macOS, full-auto mode runs in a network-isolated sandbox by default. On Linux, use Docker or a VM to get equivalent isolation.
Provide a clear task scope. "Refactor the auth module to use JWTs" works better than "improve the code". The more specific your prompt, the fewer back-and-forth rounds.
Interrupt safely. Ctrl+C cancels the current operation mid-stream. Codex will not apply partial changes. You can redirect or refine your prompt and try again.
Check your version. Codex is actively developed. Run codex --version and npm update -g @openai/codex regularly to pick up new models and fixes.