How to Use Codex CLI: Commands, Modes and What One Run Costs

How to use Codex CLI day to day, checked on codex-cli 0.174.0: sign in, start a session, sandbox and approvals, model and effort, codex exec, resume and fork, AGENTS.md, MCP servers, a cheat sheet, and one small run measured at 42,099 input tokens.

Installing Codex CLI takes one command. Using it well takes a few more: which mode to start in, how much it may touch, how to run it from a script, and how to get back to yesterday's conversation. This guide covers the commands you use every day, checked one by one on codex-cli 0.174.0, and one small run we measured so you know what a task costs before you start.

We run Codex every day next to Claude Code, as separate agents in AgentsRoom. Everything below was checked on December 4, 2026, on macOS, against codex --help, the help of each subcommand, and the version installed on our machine. Codex changes often: when a flag here does not exist on your version, codex <command> --help has the last word.

Install it and sign in

npm install -g @openai/codex
codex --version
codex login

codex login opens your browser and signs you in with your ChatGPT account. Three variants are worth knowing:

  • printenv OPENAI_API_KEY | codex login --with-api-key signs in with an API key instead of a ChatGPT plan.
  • codex login --device-auth gives you a code to type on another device, useful on a server with no browser.
  • codex login status tells you which login is active.

On Windows, the steps and the usual errors are in our Windows install guide. To update later: codex update. If something looks broken: codex doctor checks the installation, the configuration and the login.

Start a session in a project

Open a terminal at the root of your project and type codex. You can also give the first instruction right away:

codex "explain what this repository does"

This is the interactive screen: you describe the task, Codex reads files, runs commands and edits code, and you answer when it asks. The welcome screen of 0.174.0 suggests five slash commands, and they are the ones to learn first:

CommandWhat it does
/initWrites an AGENTS.md file with instructions for Codex about this project
/statusShows the session's configuration and its token usage
/permissionsChooses what Codex is allowed to do
/modelChooses the model and the reasoning effort
/reviewReviews your current changes and lists issues

A few more help in a long session: /compact summarizes the conversation before it fills the context window, /diff shows the git diff including untracked files, /new starts a fresh chat, /resume and /fork reopen or copy a past one, and /mcp lists the MCP tools Codex can see.

Codex works in the folder you started it from. -C <dir> starts it in another folder, --add-dir <dir> makes one more folder writable, and --worktree starts the session in a new Git worktree, so its edits do not land in your current branch.

Sandbox and approvals: decide how much it may touch

Two separate settings, and mixing them up is the most common surprise.

The sandbox (-s) limits what the commands Codex runs can reach:

  • read-only: Codex can read and run commands, but not write.
  • workspace-write: it can write inside the project folder.
  • danger-full-access: no limit.

The approval policy (-a) decides when Codex stops to ask you. On 0.174.0 there are two values: on-request, where the model asks when it thinks it should, and never, where it never asks and a failed command goes straight back to the model.

codex -s workspace-write -a on-request

There is also --dangerously-bypass-approvals-and-sandbox. The help calls it "extremely dangerous" and meant for machines that are already sandboxed from the outside. On your own laptop, workspace-write with on-request covers almost every task.

Choose the model and the effort

codex -m <model> -c model_reasoning_effort="high"

-m picks the model, and -c overrides any line of the configuration for this run. In the interactive screen, /model does both from a list. Your defaults go in ~/.codex/config.toml, and -p <name> layers a second file, <name>.config.toml, on top of it: handy to keep a "fast" and a "careful" setup side by side.

Run it without the interactive screen: codex exec

codex exec runs one task and exits. It is what you use in a script, a Git hook or CI.

codex exec -s read-only "find the bug that makes test_average fail"

Three options make it useful in scripts:

  • --json prints every event as one JSON line: the session starting, each command, each message, and the token usage at the end.
  • -o answer.txt writes the agent's last message to a file.
  • --ephemeral keeps no session file on disk.

Two things that surprised us. First, codex exec never waits for you: it refuses any approval request at once, so pick the sandbox before, not after. Second, when its input is not a terminal it also reads stdin, and prints "Reading additional input from stdin...". In a script that has nothing to pipe in, add < /dev/null.

What one small task costs, measured

We made a throwaway repository with two files and 21 lines of Python, calc.py with a wrong average and test_calc.py with its test, and asked Codex to find the bug without editing anything:

codex exec --ignore-user-config -s read-only --json -o last.txt \
  "Find the bug in calc.py that makes test_average fail. Explain the fix in two sentences. Do not edit any file."

--ignore-user-config keeps our own settings and MCP servers out of the measure. Codex used its default model, gpt-6-luna.

What we measuredResult
Time, start to exit22 seconds
Commands Codex ran2 (rg --files, then cat on both files)
Input tokens42,099
...of which from the cache38,144 (91%)
Output tokens159
AnswerCorrect: divide by len(values), not len(values) - 1

The code itself is 21 lines. Nearly all of the 42,099 input tokens are what Codex sends with your task: its instructions, the definitions of its tools and the conversation so far, sent again at each step. Most of it came from the cache, which is why such a run stays cheap, but it is a floor: a one-line question in an empty repository does not cost a few hundred tokens.

Then we continued the same conversation from the script:

codex exec resume --last "Now give me only the corrected line of code, nothing else."

Same session id, 3 seconds, 4,325 tokens reported by the CLI, and the answer return total / len(values). One task, one run each: a data point, not a benchmark.

Resume and fork a conversation

Codex keeps every interactive session. On our Mac, each one is a JSONL file under ~/.codex/sessions, sorted by date, with the session id in its name.

codex resume            # pick from the sessions of this folder
codex resume --last     # the most recent one
codex resume --all      # sessions of every folder
codex fork --last       # a copy under a new id, the original stays untouched

codex resume accepts the usual options, so codex resume <id> -m <model> continues a conversation on another model. codex archive, codex unarchive and codex delete tidy the list.

AGENTS.md, config.toml and MCP servers

AGENTS.md is the file Codex reads for the rules of a project: how to run the tests, what not to touch, the conventions. /init writes a first version; you then keep it short and true. What goes in it, and how it compares with CLAUDE.md, is in our AGENTS.md guide.

~/.codex/config.toml holds your defaults. Any key can be overridden for one run with -c key=value, and the CODEX_HOME variable moves the whole folder (settings, login, sessions) elsewhere.

MCP servers give Codex more tools:

codex mcp add docs -- npx -y some-mcp-server
codex mcp add remote --url https://example.com/mcp
codex mcp list

The first form starts a local server with a command, the second connects to a streamable HTTP server. In the interactive screen, /mcp lists the tools Codex actually sees.

Review your changes with codex review

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

It runs without the interactive screen and prints its findings. You can add your own instructions as a last argument, for example "focus on error handling".

Codex CLI cheat sheet

You want toCommand
Start a sessioncodex or codex "first instruction"
Run one task from a scriptcodex exec "task"
Get events as JSONcodex exec --json "task"
Save the final answercodex exec -o answer.txt "task"
Continue the last sessioncodex resume --last
Continue it from a scriptcodex exec resume --last "next step"
Copy a sessioncodex fork --last
Limit what it can touch-s read-only, -s workspace-write
Never be asked-a never
Pick the model-m <model>
Set the effort-c model_reasoning_effort="high"
Work in another folder-C <dir>
Work in a new Git worktree--worktree
Allow web search--search
Review your changescodex review --uncommitted
Add an MCP servercodex mcp add <name> -- <command>
Sign in with an API keyprintenv OPENAI_API_KEY | codex login --with-api-key
Check the installationcodex doctor

What we practise: Codex as one agent among several

We make AgentsRoom, a desktop app that runs coding agents side by side, and Codex is one of the CLIs it starts. Each Codex agent is the real codex in its own terminal, so everything above still works there. What we added sits around it:

  • One account per project. AgentsRoom gives each Codex account its own CODEX_HOME and runs codex login in it, so a personal account and a work account never mix. See multiple Codex accounts.
  • Change the model without losing the conversation. When you switch a Codex agent's model, AgentsRoom restarts it with codex resume <id> -m <model>, the command shown above.
  • Fork an agent. Fork runs codex fork so you can try a second approach from the same point.
  • See what it runs. In the conversation view, a Codex agent shows each command it runs and each file it edits, and its approval requests come with Allow, Deny and Always allow buttons.

If you also use Claude Code, our comparison of the two CLIs says where each one does better.

Frequently asked questions

How do I use Codex CLI?

Install it (npm install -g @openai/codex), sign in once with codex login, then open a terminal in your project and type codex. You get an interactive session where you describe the task in plain words. Type /init to have Codex write an AGENTS.md file for the project, /model to pick the model and effort, /permissions to choose what it may do, and /review to have it review your changes.

What are the main Codex CLI commands?

On codex-cli 0.174.0: codex (interactive session), codex exec (one task without the interactive screen), codex resume and codex fork (continue or copy a past conversation), codex review (review uncommitted changes, a branch or a commit), codex login and codex logout, codex mcp (add or list MCP servers), codex doctor (check the installation) and codex update. codex --help lists the rest.

What is the difference between codex and codex exec?

codex opens the interactive terminal interface: you talk to the agent turn by turn and approve what it asks. codex exec runs one task and exits, which suits scripts and CI. It never waits for you: an approval request is refused at once, so give it a sandbox that fits the task. Add --json for a stream of events and -o to write the final answer to a file.

How do I resume a Codex CLI conversation?

codex resume opens a list of your past sessions in the current folder, codex resume --last reopens the most recent one, and codex resume <session id> a precise one. For a script, codex exec resume --last "next instruction" continues without the interactive screen. codex fork does the same but starts a copy under a new id, so the original stays as it was.

What do the Codex CLI sandbox and approval modes do?

The sandbox (-s) limits what the commands Codex runs can touch: read-only, workspace-write (the project folder) or danger-full-access. The approval policy (-a) decides when Codex stops to ask you: on-request lets the model ask when it needs to, never never asks and returns failures to the model. In the interactive screen, /permissions changes both.

Can I use Codex CLI with an API key instead of ChatGPT?

Yes. codex login signs in with your ChatGPT account by default. To use an OpenAI API key instead, pipe it in: printenv OPENAI_API_KEY | codex login --with-api-key. codex login status shows which login is active.

Where does Codex CLI save its sessions?

On our Mac, each conversation is a JSONL file under ~/.codex/sessions, sorted by year, month and day, with the session id in the file name. Settings live in ~/.codex/config.toml. Both move with the CODEX_HOME variable, which is how you keep two Codex accounts apart.

Download AgentsRoom

Run all your AI agents, on all your projects, from a single window.

Download AgentsRoomFree

Companion app: monitor your agents on the go

Bring your own: Claude, Codex, Antigravity CLI, or other AI provider.

Push bugs and requests straight to your public backlog.

Keep reading