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-keysigns in with an API key instead of a ChatGPT plan.codex login --device-authgives you a code to type on another device, useful on a server with no browser.codex login statustells 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:
| Command | What it does |
|---|---|
/init | Writes an AGENTS.md file with instructions for Codex about this project |
/status | Shows the session's configuration and its token usage |
/permissions | Chooses what Codex is allowed to do |
/model | Chooses the model and the reasoning effort |
/review | Reviews 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:
--jsonprints every event as one JSON line: the session starting, each command, each message, and the token usage at the end.-o answer.txtwrites the agent's last message to a file.--ephemeralkeeps 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 measured | Result |
|---|---|
| Time, start to exit | 22 seconds |
| Commands Codex ran | 2 (rg --files, then cat on both files) |
| Input tokens | 42,099 |
| ...of which from the cache | 38,144 (91%) |
| Output tokens | 159 |
| Answer | Correct: 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 to | Command |
|---|---|
| Start a session | codex or codex "first instruction" |
| Run one task from a script | codex exec "task" |
| Get events as JSON | codex exec --json "task" |
| Save the final answer | codex exec -o answer.txt "task" |
| Continue the last session | codex resume --last |
| Continue it from a script | codex exec resume --last "next step" |
| Copy a session | codex 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 changes | codex review --uncommitted |
| Add an MCP server | codex mcp add <name> -- <command> |
| Sign in with an API key | printenv OPENAI_API_KEY | codex login --with-api-key |
| Check the installation | codex 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_HOMEand runscodex loginin 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 forkso 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.
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
Voice Mode in Codex, OpenCode, Antigravity and Claude Code: Which CLI Has One?
We checked five coding agent CLIs and two apps on October 13, 2026. Codex CLI and oh-my-pi hold a spoken conversation, Claude Code and Antigravity CLI only dictate into the prompt, OpenCode has nothing built in. How to start each one, where the audio goes, and what voice costs in tokens.
Read the articleDoes Claude Code Remote Control Use More Tokens? And Does Codex Have One?
Two questions people type into Google since Anthropic shipped Remote Control: does driving a Claude Code session from your phone cost more tokens, and is there an equivalent for Codex. Short answers: no, a turn is a turn wherever you type it, and yes but only half of it exists. Here is what Remote Control actually is, how to measure the token question yourself in four commands, what codex remote-control does today, and how a mobile remote that never talks to a provider changes the math.
Read the articleClaude Code Game Studios: What It Is, How to Use It, What It Costs
Claude Code Game Studios is a free MIT project template that gives Claude Code 49 game development agents and 75 skills. What is in the repository (version 1.3.0), how to start, what it adds to a session measured on Claude Code 2.1.334 (13.9k against 22.4k tokens at start), its limits, and how to run its roles as separate agents.
Read the article