What Codex CLI Is (and Isn’t)
Codex CLI is OpenAI’s coding agent for the terminal. You run codex inside a project folder, describe a change in plain English, and it reads the code, edits files, and runs commands on your machine. It is a coding agent, not an autocomplete: you hand it a task and it plans the work, carries it out, and shows you the diff as it goes.
Codex now comes in several shapes, and mixing them up is the first mistake most people make. The CLI is the open-source terminal program (Apache-2.0, on GitHub as openai/codex, with over 135,000 stars as of November 2026). The IDE extension puts the same agent into VS Code, Cursor, and Windsurf. The desktop app gives you panels and diffs instead of a text interface. And Codex Cloud runs tasks on OpenAI-managed machines that keep working while yours sleeps. This guide is about the CLI only — the one that runs on a server over SSH and the one you can drive from a script.
One thing the CLI does not do: it never commits anything you have not approved unless you explicitly configure it to. That is the whole point of the sandbox and approval system below, so learn it before you hand Codex a real repository.
What Codex CLI Costs in 2026
The CLI itself is free to download. You pay for the model time behind it, and there are two ways in:
- Sign in with ChatGPT. Codex is included in ChatGPT plans. CLI access starts at Plus ($20/month); Pro costs $100, $200, or $500 a month with a larger allowance. Free ($0) and Go ($8/month) get Codex in the desktop app, not the CLI.
- Use an API key. Billed per token at standard API rates. OpenAI recommends this path for automation rather than day-to-day interactive use, and API-key sign-in skips cloud features such as GitHub review.
On a ChatGPT plan, Codex does not charge per prompt. Each message draws on an allowance, and how much it costs depends on the model, the size of the task, and how much context it carries. OpenAI publishes estimates, not fixed limits: on Plus, roughly 15–160 GPT-6.1 Sol messages per five-hour window, 35–285 on GPT-6.1 Terra, 5–45 on GPT-6 Astra, and 350–3,000 on the lighter GPT-6 Luna. Weekly limits may also apply. Pro plans currently have no five-hour limit. Treat the ranges as planning numbers, not a guaranteed message cap — and check /status and /usage inside the CLI to see your real remaining allowance. OpenAI has also said that from December 2026, Plus, Pro, and Business will share one token-based allowance between ChatGPT and Codex, so expect the metering to change; the pricing page is the source of truth on the day you read it.
Step 1: Install Codex CLI
On macOS or Linux, OpenAI’s installer is the quickest way:
curl -fsSL https://chatgpt.com/codex/install.sh | sh
On Windows, run this in PowerShell:
powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"
Package managers work too:
npm install -g @openai/codex
brew install --cask codex
The standalone installers download from releases.openai.com/codex by default and fall back to GitHub Releases. If you would rather fetch a binary by hand, every GitHub release ships platform archives such as codex-x86_64-unknown-linux-musl.tar.gz — extract it and rename the binary to codex. After installing, run codex doctor for a diagnostic report if anything looks wrong, and codex update to check for a newer version.
Step 2: Sign In
Run codex for the first time and it asks how you want to sign in. Choose Sign in with ChatGPT and finish the browser flow — this is the usual choice, and the only one that also unlocks Codex Cloud. To use an API key instead, pipe it in without typing it:
printenv OPENAI_API_KEY | codex login --with-api-key
Check how you are signed in at any time with codex login status. If you signed in with ChatGPT and later want the API-key path, codex login lets you switch.
Step 3: Your First Session
Codex works on the folder you start it from, so start from the repository root — that is the workspace the sandbox protects, and where AGENTS.md is read from. Before you let it change anything, make a Git checkpoint:
cd my-project
git add -A && git commit -m "checkpoint before codex"
codex
Then ask it a question before you ask for code. Something like “give me an overview of this codebase” is the right first move: Codex explores with its own tools, and you learn how much it actually understands before you trust it with edits. If you must point it at a file, @-mention it rather than pasting the contents in.
When you are ready for a real change, keep the first task small — a single function, a single bug, a single test. Watch what it does, read the diff, and only then give it something bigger. The habit that separates people who love Codex from people who rage-quit it is exactly this: start read-only, verify, then widen the permissions.
Step 4: Sandbox and Approval Modes — the Part That Keeps You Safe
Codex combines two settings, and understanding the difference between them is the most important thing in this guide. The sandbox is the technical boundary: which files commands can modify and whether they can reach the network. The approval policy decides when Codex must stop and ask you before crossing a boundary. The sandbox applies to spawned commands too, not just Codex’s own file operations.
There are three sandbox modes:
| Mode | What Codex can do | When to use it |
|---|---|---|
| read-only | Inspect files, but cannot edit files or run commands without approval | Exploring an unfamiliar repo, or any folder that is not version-controlled |
| workspace-write | Read files, edit inside the workspace, and run routine local commands within that boundary | Everyday local work — this is the default low-friction mode |
| danger-full-access | Runs without sandbox restrictions; removes the filesystem and network boundaries | Only when you deliberately want the agent to act with full access |
Two approval policies matter in practice: on-request (Codex works inside the sandbox and asks when it needs to go beyond it) and never (Codex does not stop for approval prompts). Full access is the combination of danger-full-access with never — the lower-risk automation preset is workspace-write with on-request:
codex --sandbox workspace-write --ask-for-approval on-request
A few details worth knowing before you change anything:
- By default, Codex runs with network access turned off. In
workspace-writeyou can enable it inconfig.tomlwith[sandbox_workspace_write] network_access = true. - On launch, OpenAI recommends Auto (workspace-write + on-request) for version-controlled folders and read-only for folders that are not in Git.
- In
workspace-write,.git,.agents,.codex, and.awsunder the writable root are protected as read-only, so Codex cannot quietly rewrite your Git history or your AWS credentials. - The sandbox is enforced by the operating system: Seatbelt on macOS, bwrap plus seccomp on Linux, and MXC on Windows.
- There is also a
--dangerously-bypass-approvals-and-sandboxflag (alias--yolo) that runs every command without approvals or sandboxing. It exists for disposable containers. Do not use it on a machine you care about. - The old
approval_policy = "untrusted"value is retired and no longer supported — if it is in your config, remove it, because it can prevent the client from starting.
To change modes mid-session, type /permissions. It shows what Codex can do without asking first and lets you relax or tighten the requirements — for example, switching between Auto and Read Only. The built-in profiles are :read-only, :workspace, and :danger-full-access, and you can define custom ones in config.toml.
Step 5: Write an AGENTS.md
AGENTS.md is a plain Markdown file of standing instructions that Codex reads before doing any work. Type /init inside Codex to generate a starter from your codebase, then edit it — the generated version describes what is there, but only you know how you want it changed.
Codex loads these files hierarchically. It reads a global one from your Codex home directory (~/.codex/AGENTS.md, or AGENTS.override.md if that exists), then walks from the project root down to your current working directory, reading one file per directory. Files closer to where you are working appear later in the chain and override earlier guidance. The combined instructions cap out at 32 KiB by default. A useful AGENTS.md is short and opinionated:
# Project instructions
- Stack: Laravel 13, PHP 8.4, Vue 3, Pest for tests.
- Run `composer test` and `npm run build` before calling a task done.
- Never edit files under `database/migrations/` without asking first.
- Prefer small, focused commits; never force-push.
If your repo already has a CLAUDE.md, note that Codex does not read it — AGENTS.md is its file. (The reverse is not true: Claude Code reads AGENTS.md too, so one file can serve both tools.)
Step 6: Pick a Model
Type /model to open the model picker. It lists the models your plan can use and, after you pick one, asks for a reasoning effort. OpenAI’s recommendation is GPT-6.1 Sol for complex coding work, GPT-6.1 Terra for fast everyday and visual work, and GPT-6 Luna for focused, repeatable tasks; GPT-6 Astra sits above Sol for the hardest debugging. To start a session with a specific model, pass -m:
codex -m gpt-6-luna
Model names move fast — OpenAI’s own docs examples already reference GPT-6.2 Sol and GPT-6.2 Astra — so treat the picker as the source of truth for what your plan can use today, and pin a model in config.toml or a script only when you have a reason to. Newer models sometimes need a newer CLI: GPT-6.1 Terra, for example, needs Codex CLI 0.159.0 or later.
The Slash Commands That Actually Matter
There are around forty built-in slash commands. These are the ones that earn their keystrokes:
/init— generate anAGENTS.mdscaffold in the current directory./model— choose the active model and reasoning effort./permissions— see and change what Codex can do without asking./status— show the session configuration and token usage: active model, approval policy, writable roots, and remaining context capacity./usage— view account token usage;/usage daily,/usage weekly, and/usage cumulativebreak it down./review— ask Codex to review your working tree. It summarises the issues it finds, focusing on behaviour changes and missing tests, without modifying anything./resume— resume a saved chat from your session list instead of starting over. From the shell,codex resumeshows a picker andcodex resume --lastreopens the most recent session.
Two more behaviours are worth learning early. Press Enter while Codex is working to send new instructions into the current turn — you can steer it mid-task instead of waiting for it to finish and starting over. And /review is the cheapest code review you will ever get: run it before every commit, not just when you remember.
Step 7: Automate It with codex exec
codex exec (alias codex e) is where the CLI does things the desktop app cannot: it runs Codex non-interactively, for scripted or CI-style runs that should finish without human interaction. It streams progress to stderr and prints only the final agent message to stdout, so it composes with pipes and other Unix tools. The prompt is a single argument, or - to read it from stdin:
codex exec "run tests and fix any failures"
codex exec --sandbox workspace-write "update the changelog for this diff"
cat prompt.txt | codex exec -
By default, codex exec runs in a read-only sandbox — pass --sandbox workspace-write when the task needs to edit files, and reserve danger-full-access for controlled environments. Useful flags include --json for newline-delimited JSONL events (thread.started, turn.completed, item.*, error), --output-last-message -o result.md to save the final message to a file, -m to pick a model, --cd to run in a different directory, and --ephemeral when you do not want session rollout files persisted. A Git repository is required by default; override that with --skip-git-repo-check when you know what you are doing. To continue a scripted session later, use codex exec resume --last "follow-up prompt", or codex exec fork <SESSION_ID> to copy a session into a new one.
For CI, OpenAI recommends the Codex GitHub Action rather than exposing OPENAI_API_KEY or CODEX_API_KEY as a job-level variable. If you must pass a key, set it inline for a single invocation so it does not leak into the job environment.
Configure It Once in config.toml
Your user config lives at ~/.codex/config.toml; project overrides go in .codex/config.toml and apply only to trusted projects. Precedence runs from CLI flags and --config overrides at the top, through project config files, named profiles, and your user config, down to built-in defaults. A sensible starting config:
# ~/.codex/config.toml
model = "gpt-6.1-sol"
approval_policy = "on-request"
sandbox_mode = "workspace-write"
[sandbox_workspace_write]
network_access = false
Keep the defaults strict and loosen them per command with flags instead. A config that grants danger-full-access globally is a config that will eventually do something you regret on a Friday afternoon.
A Workflow That Actually Works
Here is the loop that holds up on real repositories:
- Checkpoint. Commit or stash before every task. Codex is good; it is not infallible, and
git checkout .is the fastest undo in the world. - Explore read-only first. Start the session with
--sandbox read-onlyand ask Codex to explain the area you are about to change. - Write the AGENTS.md. Run
/initonce per repo and keep the file honest as the project changes. - One task, one session. Small, well-described tasks beat sprawling ones. “Add rate limiting to the login route, 5 attempts per minute per email, with tests” will outperform “make auth better” every time.
- Review before you commit. Run
/review, read the findings, fix what matters, then commit yourself. - Resume, don’t restart.
codex resume --lastkeeps the context of a task you interrupted; a fresh session starts from zero. - Automate the boring parts. Put the repeatable jobs — changelog updates, test-fix loops, dependency bumps — behind
codex execin a script, with the sandbox set to exactly what the job needs and nothing more.
Codex CLI vs Claude Code: Which One?
Both are agentic coding tools that live in your terminal, read your repository, edit files, run commands and tests, and iterate until the task is done. The honest answer for most developers is that both are excellent at the actual coding, and the differences are about control and habit:
| Codex CLI | Claude Code | |
|---|---|---|
| Models | GPT-6 family (Sol, Terra, Luna, Astra) | Claude Opus and Sonnet |
| Licence | Apache-2.0, fully open source | Proprietary harness |
| Sandbox | OS-level (Seatbelt / bwrap / MXC); strictest default | Tiered per-tool permission rules |
| Project instructions | AGENTS.md |
CLAUDE.md (also reads AGENTS.md) |
| Billing | ChatGPT plan from Plus, or API key | Claude plan or Console API |
| Scripting | codex exec with JSONL output |
claude -p non-interactive mode |
Pick Codex CLI when you want the strictest default sandbox and an open-source agent you can inspect; pick Claude Code when you want the deepest harness for long, multi-file work. Many teams end up running both side by side in a shared workspace, with one AGENTS.md serving both. If you already use Claude Code, our Claude Code vs Cursor comparison and our AGENTS.md guide cover the other side of the same workflow.
7 Mistakes to Avoid
- Starting in danger-full-access. The first run should be read-only. You do not yet know what the agent will try.
- No Git checkpoint. If the change is not committed, it is not recoverable. Commit before, commit after.
- Giant, vague prompts. “Refactor the app” produces a mess. One task, one clear outcome, one session.
- Leaving
approval_policy = "untrusted"in your config. It is retired and can stop the client from starting. Remove it. - API keys as job-level CI variables. Use the Codex GitHub Action, or set the key inline for a single invocation.
- Trusting without reviewing.
/reviewexists for a reason. Codex writes confident-looking code that is occasionally wrong. - Assuming Free or Go includes the CLI. Those plans get Codex in the desktop app. The CLI starts at Plus.
Further Reading & References
- Codex CLI — official documentation (ChatGPT Learn)
- openai/codex on GitHub — source, README, and releases
- Codex pricing — OpenAI’s official pricing page
- How to use the Codex CLI — Flavio Copes (updated November 2026)
- How to use Codex CLI: a step-by-step guide — AgentVera
- Codex CLI Cheatsheet: config, commands, AGENTS.md, and best practices — Shipyard
- Codex vs Claude Code (2026) — OpenAgents
- Codex Usage Limits: 5-Hour Window, Weekly Cap, What Resets (2026) — Justin McKelvey
All facts verified 19 November 2026 against OpenAI’s official Codex documentation, the openai/codex GitHub repository, and OpenAI’s Codex pricing page.



