Codex CLI: OpenAI's terminal agent, with a sandbox and a local-model escape hatch
Install Codex CLI, sign in with your ChatGPT plan, learn the sandbox and approval flags that keep it safe, wire it to LM Studio or Ollama with --oss, and run it headless with codex exec.
--oss.What Codex CLI is
Codex CLI is OpenAI’s coding agent for the terminal: it reads your repository, edits files, runs commands, and iterates until the task is done. Same shape as Claude Code, different opinions โ and two features that make it interesting even if you don’t use OpenAI models:
- Explicit sandbox modes. You choose, per run, how much of your machine the agent can touch.
--oss. It can drive a local open-source model through LM Studio or Ollama instead of OpenAI’s API.
That second point is the reason this page cross-links to the local track: Codex CLI is currently the easiest way to get a polished terminal agent on top of models running on your own GPU.
Step 1 โ Install
Any OS (npm) โ Node.js 16+:
npm install -g @openai/codex
macOS (Homebrew):
brew install --cask codex
Windows: the npm command above is the simplest path. There is also a PowerShell installer documented by OpenAI:
irm https://chatgpt.com/codex/install.ps1 | iex
Check:
codex --version
# codex-cli 0.167.0
codex --help is more trustworthy than any tutorial, including this one.Step 2 โ Sign in
cd ~/projects/my-app
codex
First launch offers Sign in with ChatGPT (browser OAuth), device-code auth, or an API key. The subscription path is the cheap one: Codex is included in every ChatGPT plan, from Free upward.
| ChatGPT plan | Price | What you get in Codex |
|---|---|---|
| Free | $0 | quick coding tasks, limited model |
| Go | $8/month | lightweight tasks |
| Plus | $20/month | a few focused sessions per week |
| Pro | from $100/month | heavy daily use |
| Business / Edu / Enterprise | per seat | team controls, shared billing |
Codex and ChatGPT Work share the same usage pool โ hammering the agent eats your chat quota too. OpenAI’s own docs say not to estimate task cost from API token prices; subscriptions and API billing are separate worlds.
Step 3 โ The sandbox model (the part that matters)
Codex gives you two independent dials: what the filesystem/network allows (sandbox) and when it must ask you (approval).
codex --sandbox read-only # can read, cannot write or reach the network
codex --sandbox workspace-write # can edit the current workspace (default for most work)
codex --sandbox danger-full-access # no filesystem sandbox at all
codex -a on-request # asks before commands that need escalation (default)
codex -a never # never asks โ sandbox is the only guardrail
And the one to treat like a loaded gun:
codex --dangerously-bypass-approvals-and-sandbox # alias: --yolo
--yolo means no sandbox, no approvals. It exists for disposable containers. On your laptop it’s how you get a wiped directory, a leaked .env, or a force-push to main. Why sandboxing is the real safety layer โ
workspace-write + on-request. Widen only for a repo you can restore from git, and never run --yolo in a folder containing credentials, SSH keys, or a production .env.Step 4 โ Your first task
Start in a git repo with a clean tree, so every change is visible and revertible:
cd ~/projects/my-app
git status # should be clean
codex
Then:
> find why `npm test` fails on the auth module, fix it, and show me the diff
You’ll see commands proposed before they run, diffs as they’re applied, and a running transcript you can steer mid-turn. Useful session commands:
| Command | What it does |
|---|---|
/model | switch model (and reasoning effort) |
/permissions | change sandbox/approval for this session |
/approve | approve a pending action |
/diff | review changes |
/compact | compress the conversation |
/status | session and token state |
/agents, /subagents | inspect delegated work |
Step 5 โ Teach it the repo
Codex reads AGENTS.md at the repo root โ the same convention other agent tools now share:
# AGENTS.md
- Node 22, pnpm, Vitest
- Run `pnpm test` and `pnpm lint` before declaring success
- Do not edit `prisma/migrations/`
- Commit messages: conventional commits
Global defaults live in ~/.codex/config.toml (model, approval policy, sandbox, providers). Per-run overrides: codex -c key=value.
Step 6 โ Headless and local models
Non-interactive, for scripts and CI:
codex exec "list every endpoint in src/api and write docs/api.md"
codex exec --json "summarize the diff since main" | jq .
codex resume # continue the last session
And the local escape hatch โ start LM Studio’s server (port 1234) or Ollama (port 11434), then:
codex --oss --local-provider lmstudio
codex --oss --local-provider ollama
Now the agent loop runs on your machine’s model. Quality drops compared to frontier models, but cost drops to electricity, and nothing leaves the laptop. Serve a model locally first โ
Codex vs. Claude Code, honestly
| Codex CLI | Claude Code | |
|---|---|---|
| Free access | yes, via ChatGPT Free/Go | no |
| Sandbox control | explicit, per run | permission prompts + settings |
| Local models | yes (--oss) | no |
| Instruction file | AGENTS.md | CLAUDE.md |
| Maturity | pre-1.0, fast-moving | 2.x, steadier |
If you already pay for Claude, Claude Code is the more polished experience. If you want one agent that works with both a frontier model and your own GGUF files, Codex CLI is the only one of the two that does it.