All guides

5 Codex CLI commands that do the most work for you

Five Codex CLI commands for persistent project guidance, automation, review, session continuity and external documentation.

By Roman · · 4 min read

OpenAI

Five commands fix most of that: /init, codex exec, /review, codex resume and codex mcp add.

TL;DR

  • /init writes an AGENTS.md scaffold; refine it and commit it.
  • codex exec runs Codex from scripts; give each run an explicit read-only or workspace-write sandbox.
  • /review checks your changes before you open a pull request.
  • codex resume --last picks up the latest interactive chat in the current directory.
  • codex mcp add connects a documentation server, such as OpenAI’s read-only docs.

What working without them costs you

A fresh chat without project instructions may have to rediscover test commands and repository conventions. Automation also becomes brittle when permissions and output formats are implicit. Store stable guidance, make scripted runs explicit and resume only when earlier context still applies.

1. /init: write the repo rules down once

Run /init in the directory where you want the scaffold. Review the generated AGENTS.md, keep it focused and commit repository guidance that teammates should share.

# AGENTS.md

## Build and test
- Install: `pnpm install`
- Test the API package: `pnpm --filter api test`
- Run `pnpm lint` before finishing

## Conventions
- Keep TypeScript in strict mode; do not add `any`
- Put API handlers in `src/routes/`
- Do not edit `generated/`; run `pnpm codegen`

Codex loads global guidance first, then project instructions from the project root toward the current directory. Instructions closer to the current directory take precedence.

Do not put secrets, general reference material or rules unrelated to agent work in AGENTS.md.

2. codex exec: run Codex without a chat

Use codex exec when a task has a defined input, permission level and output. Reporting runs should be read-only; grant workspace writes only when the task must edit files.

codex exec --sandbox read-only \
  "Summarize the last 10 commits and flag risky database migrations"

codex exec --sandbox workspace-write \
  "Fix the failing tests in packages/api and rerun them"

codex exec --sandbox read-only -o notes.md \
  "Write release notes for changes since tag v2.3.0"

codex exec --sandbox read-only --json \
  "List TODO comments in src/" > events.jsonl

When a script needs stable fields, supply a JSON Schema and save the validated final response:

cat > risk.schema.json <<'EOF'
{
  "type": "object",
  "properties": {
    "risk": { "type": "string", "enum": ["low", "medium", "high"] },
    "reason": { "type": "string" }
  },
  "required": ["risk", "reason"],
  "additionalProperties": false
}
EOF

codex exec --sandbox read-only --output-schema risk.schema.json \
  -o risk.json "Review git diff main...HEAD and rate deployment risk"
jq -r .risk risk.json

Codex requires a Git repository unless you pass --skip-git-repo-check. Reserve --sandbox danger-full-access for an isolated runner or container. Use an interactive session when the task needs clarification or steering while it runs.

3. /review: get a second pass before a pull request

Type /review in an interactive session to review uncommitted changes or compare the work with a base branch.

/review

Review mode reports findings instead of implementing fixes. If a diff combines unrelated work, review smaller, focused changes separately.

Do not use it as a substitute for required human security or architecture review.

codex resume opens the session picker. --last resumes the latest interactive chat for the current working directory, while a session ID or name selects a specific chat.

codex resume
codex resume --last

codex --sandbox workspace-write exec resume --last \
  "Add tests for the edge cases from the previous run"

The final command continues the most recent session in the current directory, interactive or exec. Pass a session ID to target a specific run. Start a fresh chat with /new when the objective has changed or old context would mislead the run. Use /compact to compact the current chat’s context when the work is still related.

5. codex mcp add: add documentation when you need it

The hosted OpenAI documentation server gives Codex read-only search and page access to official OpenAI developer documentation.

codex mcp add openaiDeveloperDocs --url https://developers.openai.com/mcp
codex mcp list

The equivalent configuration is:

[mcp_servers.openaiDeveloperDocs]
url = "https://developers.openai.com/mcp"

User configuration defaults to ~/.codex/config.toml. Trusted projects may use .codex/config.toml. Configure only servers the repository needs, and trust both remote endpoints and commands used to start local servers.

Do not add a server when repository files or existing documentation already contain the required information.

Checklist

  • AGENTS.md is focused, reviewed and committed
  • Automated runs select an explicit sandbox
  • Scripts that parse results use structured output
  • /review runs before a pull request
  • Resumed chats are related to the current objective
  • MCP servers are necessary and trusted

Further reading