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
irm https://chatgpt.com/codex/install.ps1 | iex        # Windows

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 agentsBrowse and switch sessions
codex archive <chat>Hide a chat from the picker
codex unarchive <chat>Restore an archived chat
codex delete <chat>Delete a chat and its transcript
codex queueQueue a message for a session

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 features listList feature flags
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

See: Developer commands

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>on-request or never; no untrusted
-C, --cd <dir>Set the working directory
--add-dir <dir>Grant another writable folder
--worktreeRun in a managed git worktree
--searchEnable live web search
-i, --image <file>Attach images to the prompt
-c key=valueOverride any config value

See: Developer commands

Other flags

FlagDescription
--ossUse a local open-source model
--local-provider <provider>Local backend for --oss: lmstudio or ollama
--approve-for-meAuto-review approvals; replaces --full-auto
--no-alt-screenKeep output in terminal scrollback
--enable <feature>Enable optional features
--disable <feature>Turn an optional feature off
--no-daemonSkip the shared background server
--strict-configReject unknown config keys
--dangerously-bypass-approvals-and-sandboxSkip approvals and sandboxing

See: Developer commands

Sandbox and approvals

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

Auto is the default preset: edits in the workspace, approval to leave it, network 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 --ephemeral "triage issues"  # write no session files
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
/sideStart an ephemeral side 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
/exportExport the chat as Markdown

See: Slash commands

Terminals and workspace

CommandDescription
/psShow background terminals
/stopStop all background terminals
/cd <path>Move the session to a directory
/worktreeContinue in a managed git worktree
/recapSummarize the chat, keep context
/rawToggle raw scrollback

See: Slash commands

Setup

Config

CommandDescription
/initGenerate an AGENTS.md scaffold
/skillsBrowse and apply skills
/pluginsBrowse installed plugins
/memoriesConfigure memory use
/mcpList configured MCP tools
/hooksManage lifecycle hooks
/experimentalToggle experimental features

Interface

CommandDescription
/themeChoose a syntax theme
/tuiChoose the terminal mode
/statuslineConfigure footer items
/titleConfigure window title 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)

See: Slash commands

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

See: Interactive shortcuts

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"
web_search = "cached"

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_SQLITE_HOMEWhere SQLite-backed state lives
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

See: Environment variables

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 — concatenated from the repo root down, up to 32 KiB by default (project_doc_max_bytes). 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"

See: Non-interactive mode

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.

See: Code review

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.

See: MCP

Authentication

codex mcp login my-server             # OAuth, opens a browser
codex mcp login my-server --scopes read,write
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!