Tutorial
Your first session with the Codex CLI
Install it, choose a sandbox and approval mode, give it a well-scoped task in a repository and review its changes before you accept.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
Codex is OpenAI’s coding agent. The same agent runs in a terminal (the Codex CLI), as an IDE extension, in a desktop app and as a cloud service that works on tasks in the background. This tutorial covers the CLI: install it, point it at a repository, give it a well-scoped task and review the result before anything is committed.
The CLI is open source and runs locally. What sets it apart in daily use is the sandbox: commands the model runs are confined by an operating-system sandbox, and you choose how much it may do without asking.
1. Install and sign in
On macOS or Linux, OpenAI’s installer is:
curl -fsSL https://chatgpt.com/codex/install.sh | sh
If you prefer npm:
npm install -g @openai/codex
Check the install and sign in:
codex --version
codex login
codex login opens a browser to sign in with your ChatGPT account. To use an API key instead, run codex login --with-api-key and paste the key when asked. codex login status shows which method is active.
2. Start in the repository
cd ~/code/your-project
codex
Codex opens an interactive session in the terminal. Before asking for changes, ask questions:
Explain how this project is structured and how I run the tests.
Then check:
/status
/status shows the current session configuration: model, reasoning effort, the working directory, and the approval and sandbox settings. Get used to glancing at it; it tells you what Codex is allowed to do right now.
3. Choose what Codex may do
Two settings work together:
- Sandbox mode limits what commands can touch.
read-onlylets Codex read files and run commands that do not write.workspace-writelets it edit files and run commands inside the working directory (and any extra directories you add).danger-full-accessremoves the sandbox. - Approval policy decides when Codex stops to ask you. With
on-request, the model asks when it wants to do something the sandbox does not allow, such as using the network. Withnever, it never asks and failures go straight back to the model.
Inside a session, /permissions lets you pick a preset. From the command line you can set both explicitly:
# Start cautious: read-only, asks before anything more
codex --sandbox read-only --ask-for-approval on-request
A sensible progression for a first week: start read-only while you get a feel for it, move to workspace-write with on-request approvals for normal work, and keep danger-full-access for disposable containers.
4. Create AGENTS.md with /init
Codex reads AGENTS.md files for project instructions: from your home Codex folder, then from the repository root down to the directory you are working in. Generate a starting point:
/init
Open the generated AGENTS.md and trim it to the commands and conventions a new colleague would need. The CLAUDE.md and AGENTS.md tutorial covers what to keep, and how to share the file with Claude Code and Cursor.
5. Give it a scoped task
Pick something small and checkable. Good first tasks: add a missing test for a function you know, fix a small bug with a clear reproduction, or tighten input validation in one place. State the boundary and the definition of done:
The parseDuration function in src/utils/time.ts accepts "10x" and returns NaN.
Make it throw a clear error for unknown units, add tests for valid and invalid
input in the existing test file, and run the tests. Do not change other functions.
Codex will read the relevant files, propose edits and run commands. Depending on your settings it will ask before some of them. Read what it wants to run before approving. When it says it is finished, review the changes with Git yourself:
git status
git diff
Check, in this order:
- Did it change only what you asked?
- Do the new tests fail if you revert the fix? (Quick way:
git stashthe source change only, run the tests, then restore it.) - Do all tests pass when you run them yourself?
If something is off, tell it plainly in the same session. If it went in the wrong direction, discard the changes with Git and start again with a clearer prompt.
6. Use /review before you commit
Codex has a built-in reviewer. Inside a session:
/review
It reviews your current changes and reports issues. Treat its findings as a second opinion, not a verdict: it can miss things and occasionally flags non-issues. The same reviewer is available non-interactively as codex review, which is useful in scripts.
Add a skill for work you repeat
Once you notice yourself typing the same instructions for a kind of task (writing a migration, preparing release notes, following your team’s review checklist), turn them into an Agent Skill. A skill is a folder with a SKILL.md file: a short frontmatter block with a name and a description, then the instructions. Codex reads skills from .agents/skills/ in the repository (searching from your working directory up to the repository root) and from ~/.agents/skills/ for personal ones.
---
name: release-notes
description: Draft release notes from merged pull requests since the last tag. Use when asked to prepare a release or write a changelog.
---
1. Find the last tag with `git describe --tags --abbrev=0`.
2. List the commits since that tag with `git log --oneline <tag>..HEAD`.
3. Group user-facing changes under Added, Changed and Fixed, one line each.
4. Leave out internal refactors and dependency bumps unless they change behaviour.
Save it as .agents/skills/release-notes/SKILL.md. Codex sees the name and description of every installed skill and loads the full instructions when a task matches; you can also invoke one directly by typing $release-notes in the prompt. Because .agents/skills/ is a shared convention, the same folder also works in Cursor and several other agents. The Agent Skills directory has ready-made skills with install commands for each agent.
Running Codex from scripts
codex exec runs a task non-interactively and prints the result, which makes Codex usable in CI or a Makefile:
codex exec "Summarise the changes on this branch compared with main in five bullet points"
Useful options include --json for machine-readable events, -o file.md to write the final message to a file, and --output-schema schema.json to require the final answer to match a JSON Schema. In CI, combine codex exec with a read-only sandbox unless the job really needs to write.
Everyday commands
| Command | What it does |
|---|---|
codex |
Start an interactive session in the current directory |
codex "task" |
Start a session with a first prompt |
codex exec "task" |
Run non-interactively and print the result |
codex review |
Review current changes non-interactively |
codex resume --last |
Continue the most recent session |
/init |
Create an AGENTS.md for the project |
/status |
Show model, directory, sandbox and approval settings |
/permissions |
Choose what Codex may do without asking |
/model |
Choose the model and reasoning effort |
/review |
Review the current changes |
Pitfalls
Starting with a big task. “Refactor the data layer” produces a large diff that is hard to review. Do three small tasks first.
Loosening the sandbox to stop the prompts. If you keep approving the same safe command, configure it properly instead of switching to full access.
Skipping your own test run. “Tests pass” in the agent’s summary is a claim. Run them.
Long sessions across unrelated tasks. Start a new session for a new task so earlier context does not leak into it.
Assuming instructions are enforced. AGENTS.md guides behaviour; the sandbox enforces it. Anything that must not happen belongs in sandbox and approval settings.
Next steps
- Compare the experience with Claude Code and Cursor on the same task.
- Learn what to look for in agent-written changes in Reviewing AI-written code.
- Try a real job: the unit tests for legacy code workflow.
- Tighten what Codex may do on its own with the Codex sandbox and approval settings.
- See how it compares with the alternatives in Claude Code vs Codex vs Cursor.
Covers
- Skills
- Tools
Check your understanding
Codex CLI first session quiz
Sandbox modes, approval policies, AGENTS.md, scoped tasks, /review, skills and codex exec.
7 questions · pass mark 70%
Review
Related
- Your first session with Claude Code
Install it, run it in a real repository, approve its first changes, commit a reviewed edit and learn the commands you will use daily.
- CLAUDE.md and AGENTS.md: instruction files for coding agents
CLAUDE.md vs AGENTS.md: what to put in each, where Claude Code, Codex, Cursor and Copilot look for them, and how to keep one source of truth without drift.
- Coding agents side by side: Claude Code, Codex and Cursor
The same task in Claude Code, Codex CLI and Cursor, in one table per topic: start, commands, instruction files, skill folders, MCP setup and permissions.
- Reviewing AI-written code: what to look for
The ways agent-written diffs fail, a practical review checklist, and how to set up the work so review stays manageable.
- Write unit tests for legacy code with a coding agent
An agent writes characterisation tests, you check for weak assertions and prove them with mutation testing.