Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

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.

By Beginner30 min hands-on
Bookmark

Bookmarks are kept in your free account, so you can find this page again on any device.

Sign in to bookmark
Quiz at the end
On 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-only lets Codex read files and run commands that do not write. workspace-write lets it edit files and run commands inside the working directory (and any extra directories you add). danger-full-access removes 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. With never, 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:

  1. Did it change only what you asked?
  2. Do the new tests fail if you revert the fix? (Quick way: git stash the source change only, run the tests, then restore it.)
  3. 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

Covers

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%

Question 1 of 70 answered
  1. What does the workspace-write sandbox mode allow?
Back to top