Codex is OpenAI's coding agent for the terminal. This reference covers sessions, commands, flags, and configuration.

Getting started

Introduction

Codex is OpenAI's coding agent for the terminal. This reference covers the most commonly used commands, flags, and settings.

Install

curl -fsSL https://chatgpt.com/codex/install.sh | sh   # macOS/Linux
npm install -g @openai/codex                           # npm
brew install --cask codex                              # macOS

On Windows, run irm https://chatgpt.com/codex/install.ps1 | iex in PowerShell. See: Getting started

Sign in

codex                 # first run opens a browser login
codex login           # sign in again
codex login status    # show the current account

Codex works with a ChatGPT plan or an API key. For scripts, pipe a key in: printenv OPENAI_API_KEY | codex login --with-api-key. See: Authentication

Sessions

codex                      # start the interactive TUI
codex "explain this repo"  # start with a prompt
codex resume --last        # reopen the latest chat
codex fork --last          # branch the latest chat

codex resume scopes --last to the current directory; add --all to search every session. See: CLI reference

CLI reference

Commands

Sessions

CommandDescription
codexStart the interactive TUI
codex resumeReopen a saved chat
codex forkBranch a saved chat into a new one
codex archive <chat>Hide a chat from the picker
codex delete <chat>Delete a chat and its transcript

Setup

CommandDescription
codex loginSign in with ChatGPT
codex logoutRemove stored credentials
codex updateUpdate to the latest version
codex doctorDiagnose install and config
codex completion zshGenerate shell completions
codex mcpManage MCP servers
codex pluginInstall and remove plugins

Automation

CommandDescription
codex exec "task"Run non-interactively
codex reviewReview changes non-interactively
codex cloudBrowse and run cloud chats
codex apply <task>Apply a cloud diff locally

Flags

FlagDescription
-m, --model <model>Choose the model
-p, --profile <name>Layer a profile over config
-s, --sandbox <mode>Set the sandbox policy
-a, --ask-for-approval <policy>Control approval prompts
-C, --cd <dir>Set the working directory
--add-dir <dir>Grant another writable folder
--searchEnable live web search
-i, --image <file>Attach images to the prompt
-c key=valueOverride any config value

Other flags

FlagDescription
--ossUse a local open-source model
--no-alt-screenKeep output in terminal scrollback
--enable <feature>Enable optional features
--dangerously-bypass-approvals-and-sandboxSkip approvals and sandboxing

Sandbox and approvals

ModeEffect
read-onlyRead files; no edits or network
workspace-writeEdit the workspace; network off
danger-full-accessNo sandbox at all
codex --sandbox workspace-write --ask-for-approval on-request  # Auto
codex --sandbox read-only --ask-for-approval never  # read-only

Auto (the Git-repo default): edits in the workspace, asks before leaving it. Network stays off until sandbox_workspace_write.network_access = true. See: Approvals & security

Non-interactive mode

codex exec "summarize this repo"        # final message to stdout
git diff | codex exec "review this"     # pipe context in
codex exec --json "list failing tests"  # JSONL events
codex exec resume --last "fix them"     # continue the last run

codex exec runs read-only by default; add --sandbox workspace-write to allow edits. -o file writes the final message to a file. See: Non-interactive mode

Code review

codex review --uncommitted   # staged, unstaged, and untracked
codex review --base main     # changes against a base branch
codex review --commit <sha>  # a single commit

Pass exactly one target, or give custom instructions as a prompt. --title only works with --commit. See: Code review

Slash commands

Session

Chat

CommandDescription
/newStart a fresh chat
/clearClear the terminal and start fresh
/resumeReopen a saved chat
/forkBranch the current chat
/compactSummarize to free up context
/rename <name>Rename the current chat

Control

CommandDescription
/planSwitch to plan mode
/goal <objective>Set a persistent task goal
/permissionsChange what Codex may do
/approveRetry an auto-denied action
/modelChoose the active model
/fastToggle the Fast service tier

Inspect

CommandDescription
/diffShow the Git diff
/reviewReview the working tree
/statusShow model, policy, and tokens
/usageShow account token usage
/copyCopy the latest response

Setup

Config

CommandDescription
/initGenerate an AGENTS.md scaffold
/skillsBrowse and apply skills
/pluginsBrowse installed plugins
/mcpList configured MCP tools
/hooksManage lifecycle hooks

Interface

CommandDescription
/themeChoose a syntax theme
/statuslineConfigure footer items
/vimToggle Vim composer mode
/keymapRemap TUI shortcuts

Account

CommandDescription
/importImport Claude Code or Cursor setup
/feedbackSend logs to the maintainers
/logoutSign out
/quitExit the CLI (also /exit)

Interactive mode

Keyboard shortcuts

Session

ShortcutDescription
Ctrl+CClose the session
Ctrl+RSearch prompt history
Ctrl+OCopy the latest response
Ctrl+LClear the terminal view
Ctrl+GEdit the prompt in $VISUAL/$EDITOR
Esc EscEdit the previous message and fork
Alt+RToggle raw scrollback
Up DownRestore draft history

While running

ShortcutDescription
EnterInject instructions into the turn
TabQueue a follow-up for the next turn

Input modes

InputEffect
/Slash commands
@Search files and add paths
!Run a shell command directly

Paste an image into the composer. See: Image inputs

Configuration

Config file

~/.codex/config.toml            # user settings
.codex/config.toml              # project settings (trusted projects)
$CODEX_HOME/<name>.config.toml  # profile, selected with --profile
/etc/codex/config.toml          # system defaults (Unix)
model = "gpt-6.1-sol"
approval_policy = "on-request"
sandbox_mode = "workspace-write"
model_reasoning_effort = "medium"

Project files merge from the repo root down to the current directory; CLI flags and -c key=value overrides win. See: Config basics

Environment variables

VariablePurpose
CODEX_HOMEState directory (default ~/.codex)
CODEX_API_KEYAPI key for non-interactive runs
CODEX_ACCESS_TOKENAccess token for trusted automation
CODEX_CA_CERTIFICATEPEM CA bundle for corporate TLS
RUST_LOGLog verbosity for debugging

AGENTS.md

AGENTS.md            # project instructions, committed
~/.codex/AGENTS.md   # global defaults for every repo

Each directory contributes one file — AGENTS.override.md if present, else AGENTS.md — and Codex concatenates them from the repo root down. See: AGENTS.md

Common workflows

Scripting and CI

# Machine-readable events for scripts
codex exec --json "list the failing tests" | jq

# Capture the final message in a file
codex exec -o summary.md "summarize this repo"

# Use an API key inline in CI
CODEX_API_KEY=$KEY codex exec "triage open bugs"

Reviewing code

codex review --uncommitted
codex review --base main
git diff | codex exec "review this diff for bugs"

Inside a session, /review reviews the working tree and /diff shows the changes.

Cloud tasks

codex cloud list                # recent cloud chats
codex cloud exec --env <id> "fix the flaky test"
codex apply <task-id>           # apply the diff locally

codex cloud exec needs --env; add --attempts 2 for best-of-N. See: Codex Cloud

MCP servers

Adding servers

codex mcp add my-server -- npx my-mcp-server        # stdio
codex mcp add remote --url https://example.com/mcp  # HTTP
codex mcp list

Add --env KEY=VALUE for stdio servers, or --bearer-token-env-var VAR for HTTP.

Authentication

codex mcp login my-server             # OAuth, opens a browser
codex mcp login my-server --scopes read:docs
codex mcp logout my-server

OAuth works with streamable HTTP servers that support it. Stored credentials live in the store set by mcp_oauth_credentials_store (auto, file, or keyring). See: MCP

Also see

0 Comments for this cheatsheet. Write yours!