Getting Started
Install
npm install -g @openai/codex
Authentication
export OPENAI_API_KEY="sk-..."

Set in shell profile or .env file

First Run
codex "explain this project" codex "add input validation to app.py"
Commands
Interactive (Default)
codex # start interactive session codex "fix the login bug" # with initial prompt
codex exec
codex exec "write unit tests for utils.py" codex exec "refactor db.py to use async"

Non-interactive — runs task to completion

codex review
codex review # review staged changes codex review --diff HEAD~3 # review last 3 commits
Modes
Approval Modes
suggestShow proposed changes, require approval for every file edit and command
auto-editApply file edits automatically, require approval for shell commands
full-autoApply edits and run commands without approval
Setting the Mode
codex --approval-mode suggest "add tests" codex --approval-mode auto-edit "refactor" codex --approval-mode full-auto "fix lint"
Configuration
Config File
# ~/.codex/config.yaml model: o4-mini approval_mode: suggest providers: - name: openai api_key_env: OPENAI_API_KEY

Per-project overrides: .codex/config.yaml in project root

Project Instructions
# AGENTS.md (in project root) - Run tests with: uv run pytest - Use ruff for linting - Never modify migration files
Useful Config Options
modelModel to use (e.g., o4-mini, o3)
approval_modeDefault approval mode
providersAPI provider config (key, base URL)
historySave conversation history: true / false
Sandboxing
How It Works
Codex runs commands in a sandboxed environment to prevent unintended side effects. Network access is disabled by default. File writes are restricted to the project directory.
Sandbox Options
macOSApple Seatbelt (default, built-in)
LinuxDocker-based sandbox
--full-autoRequires sandbox to be enabled
--dangerously-auto-approveSkip sandbox (not recommended)
Tips
Effective Prompts
Be specific"add retry logic to fetch_data()" > "improve code"
Reference files"fix the bug in src/auth.py" narrows scope
State constraints"don't change the public API" sets boundaries
IterateFollow up with refinements in the same session
Workflow Patterns
# Explore → plan → execute codex "explain the auth module" codex "plan how to add OAuth support" codex --approval-mode auto-edit "add OAuth" # Review before commit git add -A codex review
Common Flags
--model, -mOverride model for this session
--approval-modeSet approval mode
--quiet, -qMinimal output
--no-project-docIgnore AGENTS.md