# Codex CLI Guide: Install, Sandbox, MCP and CI (2026)

> Install Codex CLI, sign in with ChatGPT or an API key, and set up sandbox, approvals, config.toml, MCP and codex exec in CI. Version 0.172.0, with test results.
> Author: Roman Belov · Published: 2026-11-26 · Source: https://futurecraft.pro/blog/codex-cli-guide/

Codex CLI is OpenAI's coding agent that runs in the terminal inside your repository: it reads files, edits them and runs commands within a sandbox and an approval policy. It installs with one command, signs in with a ChatGPT plan or an API key, and runs as `codex exec` in scripts and CI.

To start now:

```bash
curl -fsSL https://chatgpt.com/codex/install.sh | sh   # macOS and Linux
codex login                                            # sign in via browser
cd my-repo && codex                                    # interactive mode
```

In a folder under git, Codex offers the Auto preset: edits inside the project without asking, network off, approval for anything beyond.

Versions, prices and limits are as of November 29, 2026, from OpenAI's documentation. "Author's test" means the command ran on codex-cli 0.172.0 (macOS, ChatGPT sign-in, `codex exec --ephemeral --ignore-user-config --json`, model `gpt-6.2-sol`), one run per scenario. The MCP test used a separate clean `CODEX_HOME` the same day. A from-scratch install, Windows and Linux weren't tested; those parts follow the documentation.

## When you need the CLI

Codex comes as a CLI, a desktop app, an IDE extension and a cloud service. Pick the CLI if you live in the terminal, work on a remote machine or call the agent from scripts. The [Claude Code vs Codex comparison](https://futurecraft.pro/blog/claude-code-vs-codex/) covers models, cloud and prices. For an agent that isn't tied to one vendor, see [OpenCode](https://futurecraft.pro/blog/opencode-guide/).

## Installing and updating Codex CLI

The latest stable release is [0.172.0 from November 24, 2026](https://github.com/openai/codex/releases/tag/rust-v0.172.0), also `latest` on npm. The [official methods](https://learn.chatgpt.com/docs/codex/cli):

| System | Install | Update |
|---|---|---|
| macOS, Linux | `curl -fsSL https://chatgpt.com/codex/install.sh \| sh` | same command |
| Windows (PowerShell) | `powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 \| iex"` | same command |
| npm | `npm install -g @openai/codex` | same command |
| Homebrew | `brew install --cask codex` | `brew upgrade --cask codex` |

After installing, run `codex --version`. `codex update` updates the CLI if the build supports it, and `codex doctor` reports on installation, config, auth and git.

On Linux and WSL2 the documentation says to install `bubblewrap` first: the sandbox depends on it. WSL1 isn't supported since 0.115. For the neighboring tool, see [how to install Claude Code](https://futurecraft.pro/blog/install-claude-code/).

## Signing in: ChatGPT plan or API key

Your sign-in method decides cost and some features. A summary of the [Authentication](https://learn.chatgpt.com/docs/auth) and [Pricing](https://learn.chatgpt.com/docs/pricing) pages:

| | ChatGPT plan | API key |
|---|---|---|
| How to sign in | `codex login` | `printenv OPENAI_API_KEY \| codex login --with-api-key` |
| Billing | plan limit, then credits | API prices |
| Models | by plan | by key access |
| Codex cloud, GitHub PR review, Slack | yes, on paid plans | no |
| CI | trusted runners only, no public repositories | the default option |

`codex login status` shows the current method, `codex logout` resets it. Without a browser, use `codex login --device-auth`: it's in beta, and you first enable device code sign-in in ChatGPT security settings. The fallback is `ssh -L 1455:localhost:1455 user@remote` to forward the callback port, with `codex login` in that session.

Tokens live in `~/.codex/auth.json` as plain text or in the OS credential store. Don't commit or share that file. For Keychain or its equivalent, set `cli_auth_credentials_store = "keyring"` in the config.

## First run and useful commands

Run `codex` in the repository root and start with a question, no edits. The commands you'll use most:

- `/status` shows the model, approval policy, writable directories and remaining context.
- `/permissions` changes permissions on the fly, e.g. Auto to read-only.
- `/model` switches model and reasoning effort.
- `/init` creates a starter `AGENTS.md`.
- `/diff` shows changes, `/review` asks the agent to check them.
- `/compact` compresses history after a long session.

`codex resume` continues a closed session. Commit before a task so edits are easy to roll back.

## Models and reasoning effort

[GPT-6.2 Astra and Sol](https://openai.com/index/introducing-gpt-6-2-astra-sol/) came out on November 17, 2026. With no model set, Codex picks the recommended one; in the author's test that was `gpt-6.2-sol`.

| Model | For what (per the [documentation](https://learn.chatgpt.com/docs/models)) | Default context | Messages per 5 hours on Plus, estimate | Credits per 1M: input / cached / output |
|---|---|---|---|---|
| `gpt-6.2-astra` | the hardest tasks | 272,000 | 6-50 | 200 / 10 / 1,000 |
| `gpt-6.2-sol` | everyday code | 500,000 | 17-180 | 50 / 2.5 / 250 |
| `gpt-6.1-sol` | long repetitive tasks | 500,000 | 15-160 | 50 / 2.5 / 250 |
| `gpt-6.1-terra` | text, images, computer use; needs CLI 0.159.0+ | not stated | 35-285 | 25 / 1.25 / 125 |
| `gpt-6-luna` | simple tasks in bulk | not stated | 350-3,000 | 2.5 / 0.25 / 12.5 |

Both GPT-6.2 models support a window of up to 1M tokens, set with `model_context_window`. In Enterprise and Edu they're off until an administrator enables them.

Change the model with `-m gpt-6.2-sol`, `/model` or the `model` config key. `model_reasoning_effort` sets reasoning depth: `low`, `medium`, `high`, `xhigh`, `max`, `ultra`, depending on the model. `ultra` brings in subagents; Luna doesn't have it. The documentation suggests the client's choice for GPT-6.2 and `high` for Luna.

Old model names break scripts. With ChatGPT sign-in, `gpt-5.4` was retired on August 31 and `gpt-5.5` on October 14, 2026. API key sessions weren't affected.

## Codex sandbox: what the agent can physically do

The sandbox limits commands at the OS level (Seatbelt on macOS, `bwrap` and `seccomp` on Linux), and the approval policy decides when the agent must ask. Combinations from the [documentation](https://learn.chatgpt.com/docs/agent-approvals-security):

| Scenario | Flags | What happens |
|---|---|---|
| Auto | none, or `--sandbox workspace-write --ask-for-approval on-request` | edits the project itself, asks before leaving it or using the network |
| Review | `--sandbox read-only --ask-for-approval on-request` | reads and runs commands without writing |
| CI without edits | `--sandbox read-only --ask-for-approval never` | same, but never asks |
| Automatic review | `--approve-for-me` | `workspace-write`; a reviewer agent checks requests, model usage is higher |
| Full access | `--dangerously-bypass-approvals-and-sandbox` (`--yolo`) | no sandbox, no questions; isolated environments only |

Codex recommends Auto in a version-controlled folder and `read-only` elsewhere. The reference doesn't state the exact `approval_policy` default, so check `/status`.

### What the test showed

Test repository: `calc.py`, three tests with one failing, and `AGENTS.md`. Author's test, one run per scenario.

| Action | Result |
|---|---|
| "Fix the test", `-s read-only` | edit rejected: `writing is blocked by read-only sandbox`; the model checked the fix in memory and showed the code |
| "Fix the test", `-s workspace-write` | fixed, 3 tests passed, 21 s |
| `curl` in `workspace-write` | exit 6, `Could not resolve host` |
| same with `-c sandbox_workspace_write.network_access=true` | HTTP 200 |
| write to the home folder, outside the project | `Operation not permitted` |
| `git commit` in `workspace-write` | `.git/index.lock: Operation not permitted`, exit 128 |
| `codex sandbox -- sh -c …` | writing to `/tmp` and network denied by default |

The last row is `codex sandbox`, a separate command that runs anything under Codex's policy without a model (the reference shows `macos`, `linux` and `windows` subcommands; the `--` form worked in the test). In `workspace-write`, the documentation includes `/tmp` in the workspace.

### Why the agent couldn't commit

The `git commit` failure matches the documentation. Inside writable directories `.git`, `.agents`, `.codex` and `.aws` are recursively read-only, and [Advanced Config](https://learn.chatgpt.com/docs/config-file/config-advanced) says this is why `git commit` "may still require approval to run outside the sandbox". Git failed at its first step, `index.lock`.

Interactive mode with `on-request` may ask you. In the `codex exec` test the policy was `never`, so the command failed. The workable setup: the agent edits files, you or the next pipeline step commit. Per the documentation, a `prefix_rule` with `decision = "allow"` can permit `git commit` ([rules](https://learn.chatgpt.com/docs/agent-configuration/rules) are marked experimental), but then it runs outside the sandbox with the repository's git hooks. If the agent may edit those hooks, the boundary is gone. Not tested.

### Approvals: flag and config accept different values

| Where | Values accepted in 0.172.0 |
|---|---|
| `-a` / `--ask-for-approval` flag | `on-request`, `never` (author's test; same in the [tag's source](https://github.com/openai/codex/blob/rust-v0.172.0/codex-rs/utils/cli/src/approval_mode_cli_arg.rs)) |
| `approval_policy` key in `config.toml` | `on-request`, `never` and a `granular` object (per the [reference](https://learn.chatgpt.com/docs/config-file/config-reference), not tested) |

`granular` keeps prompts for the categories you choose and rejects the rest automatically:

```toml
approval_policy = { granular = { sandbox_approval = true, rules = true, mcp_elicitations = true, request_permissions = false, skill_approval = false } }
```

`on-failure` is deprecated; `untrusted` is no longer supported and may keep the client from starting. If you used `untrusted` to confirm every command, the documentation suggests dropping the explicit `approval_policy` and marking the project in `~/.codex/config.toml`:

```toml
[projects."/path/to/project"]
trust_level = "untrusted"
```

### Network and web search

`network_access = true` opens the whole network to commands. Domain rules work only with `[features.network_proxy]` enabled, and the proxy doesn't filter web search, MCP connections or the model's own requests. Web search defaults to `cached` (OpenAI's index), `--search` switches it to `live`, `web_search = "disabled"` turns it off.

## config.toml: layers, profiles, overrides

The [precedence order](https://learn.chatgpt.com/docs/config-file/config-basic), strongest first:

1. CLI flags and `-c key=value`.
2. Project `.codex/config.toml` files, nearest to the current directory wins; trusted projects only.
3. The `--profile <name>` file.
4. `~/.codex/config.toml`.
5. Cloud workspace settings, `/etc/codex/config.toml`, built-in values.

A profile is weaker than project config: sandbox and approval keys aren't on the list Codex ignores in a project `.codex/config.toml`, so a trusted repository can loosen a strict personal profile. Pass hard limits as flags.

Since 0.134.0 a profile is a separate `~/.codex/<name>.config.toml` file with top-level keys. Codex no longer reads the `[profiles.<name>]` table or the `profile = "…"` key. An untested example from the documentation:

```toml
# ~/.codex/careful.config.toml, run: codex --profile careful
sandbox_mode = "read-only"
approval_policy = "on-request"
web_search = "disabled"

[shell_environment_policy]
ignore_default_excludes = false
```

The last line drops variables with `KEY`, `SECRET` or `TOKEN` in the name from command environments. The default is `true`, which leaves that filter off.

`--strict-config` catches a mistyped key. In the author's test: ``unknown configuration field `approvl_policy` in -c/--config override``.

## AGENTS.md

Codex reads the global `~/.codex/AGENTS.md`, then one file per directory from the project root to the current one, and concatenates them, nearest last. The total limit is 32 KiB.

In the author's test (one run each) the root `AGENTS.md` worked. An `AGENTS.override.md` next to it replaced it, and a file in a subfolder beat the root one when launched from there. With `project_doc_max_bytes=10` the file was cut off, the instruction wasn't followed, and the last lines of output had no warning. If a rule stops working, check file sizes. For writing one for several agents, see the [AGENTS.md guide](https://futurecraft.pro/blog/agents-md-guide/).

## MCP, skills and subagents

`codex mcp add <name> -- <command>` adds a stdio [MCP server](https://learn.chatgpt.com/docs/extend/mcp?surface=cli), `--url <URL>` an HTTP one; both write an `[mcp_servers.<name>]` table. In the author's test `codex mcp list` showed `enabled` even with the HTTP server down, because it doesn't check the connection. And `codex exec` without extra settings rejected the tool call: `MCP tool call requires approval, but approval policy is never`. With this config the call went through:

```toml
[mcp_servers.rates]
command = "python"
args = ["server.py"]
default_tools_approval_mode = "approve"
```

Set `approve` only for servers you trust, and narrow tools with `enabled_tools`. Do you need MCP at all? See [MCP vs API](https://futurecraft.pro/blog/mcp-vs-api/).

[Skills](https://learn.chatgpt.com/docs/build-skills) are folders with `SKILL.md` in the repository's `.agents/skills` or in `$HOME/.agents/skills`; `/skills` lists them. [Subagents](https://learn.chatgpt.com/docs/agent-configuration/subagents) are on by default, start on request or by instruction, and use more tokens than a single agent.

## codex exec in CI

[`codex exec`](https://learn.chatgpt.com/docs/non-interactive-mode) runs a task without an interface: progress goes to stderr, the final answer to stdout. The default sandbox is `read-only`; edits need an explicit mode.

```bash
# check without edits, answer matching a JSON Schema
codex exec --ephemeral --output-schema ./schema.json -o result.json \
  "Find exception handling errors in the diff"

# edits inside the project, events as JSONL
codex exec --sandbox workspace-write --json "Fix the failing test" > events.jsonl

# continue the last session (it must not be --ephemeral)
codex exec resume --last "Add a test for the edge case"
```

Keep in mind:

- Outside git, `codex exec` refuses to run. In the author's test: `Not inside a trusted directory and --skip-git-repo-check was not specified`.
- In the author's test `--output-schema` returned JSON matching the schema, but an `AGENTS.md` instruction to add a word at the end landed inside a string field. Keep answer-format rules out of such repositories.
- Project config and MCP from the repository load only if the project is trusted; `--trust` saves that.
- Pass the key per call through `CODEX_API_KEY`. Don't set it job-wide: repository code can read the environment.

For GitHub Actions there's the official [`openai/codex-action@v1`](https://github.com/openai/codex-action). Based on the [documentation example](https://learn.chatgpt.com/docs/github-action):

```yaml
jobs:
  codex:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      final_message: ${{ steps.run_codex.outputs.final-message }}
    steps:
      - uses: actions/checkout@v5
        with:
          persist-credentials: false
      - id: run_codex
        uses: openai/codex-action@v1
        with:
          openai-api-key: ${{ secrets.OPENAI_API_KEY }}
          prompt-file: .github/codex/prompts/review.md
          sandbox: read-only
```

A second job with write permissions but no key posts the comment or pull request. If the agent edits code, OpenAI's example saves `git diff` as an artifact for another job to apply and commit. Treat prompts from outside PRs as untrusted input.

## Limits and cost

| Option | Price on November 29, 2026 | About the CLI |
|---|---|---|
| Free, Go | $0, $8 a month | the card lists only GPT-6 Luna in the desktop app |
| Plus | $20 a month | CLI listed; 5-hour limit |
| Pro | $100, $200 or $500 a month | currently no 5-hour limit |
| Business | $20 per seat billed annually, $25 monthly, 2 seats minimum | Standard message estimates same as Plus |
| Enterprise, Edu | by contract | credit usage or per-seat limits |
| API key | API prices | no cloud features |

Local and cloud tasks share one limit; weekly limits may apply. `/status` shows what's left.

What one turn cost in the author's test on `gpt-6.2-sol`, with credits calculated from the model table:

| Turn | Input tokens | Cached | Output | Credits, calculated |
|---|---|---|---|---|
| short, one command | ≈57,700 | 42,496 | not recorded | ≈0.9 without output |
| fixing a test, `workspace-write` | 146,560 | 130,176 | 370 | ≈1.2 |
| same in `read-only` | 170,493 | 154,624 | 744 | ≈1.4 |

Even a short turn is two model calls of about 29K base tokens each. The `read-only` attempt cost more than the successful edit. Three runs on a tiny project don't extrapolate to a large repository. To spend less, the documentation suggests a short `AGENTS.md`, fewer MCP servers, narrow tasks and a smaller model.

## Common Codex CLI errors

| Symptom | Cause | What to do | Status |
|---|---|---|---|
| `--full-auto`: `unexpected argument` | flag removed | `--sandbox workspace-write` or `--approve-for-me` | test |
| `codex exec` doesn't change files | `read-only` by default | `--sandbox workspace-write` | test and docs |
| `Could not resolve host` | no network in `workspace-write` | `network_access = true` | test |
| `index.lock: Operation not permitted` | `.git` is protected | commit outside the sandbox | test |
| MCP tool rejected in `exec` | `never` policy | `default_tools_approval_mode = "approve"` | test |
| client won't start after an update | `approval_policy = "untrusted"` | `on-request` | docs |
| `--profile x` has no effect | old `[profiles.x]` | a `~/.codex/x.config.toml` file | docs |
| script with `gpt-5.5` fails | model retired for ChatGPT sign-in | a model on your plan | docs |
| custom provider doesn't work | needs the Responses API | `wire_api = "chat"` isn't supported | docs |

## What this guide doesn't cover

Not tested: the exact `approval_policy` default, behavior on Windows and Linux, `granular`, permission profiles (beta), hooks and Codex cloud. Recent versions dropped `--full-auto`, `untrusted` and old profiles. Before moving commands to CI, pin the CLI version (the action has a `codex-version` parameter) and check `codex --help`.

## Checklist before team use

1. Pin the CLI version and the model in CI.
2. Use Auto for people, `read-only` for checks, `workspace-write` in a separate job for automated edits.
3. Decide who commits: in `workspace-write`, `.git` is write-protected.
4. Keep the network off, open it by domain through `network_proxy`.
5. Set `ignore_default_excludes = false` so secrets don't reach commands.
6. Check the total size of `AGENTS.md` and the list of MCP servers.
7. Search your scripts for `--full-auto`, `untrusted`, `[profiles.` and `gpt-5.`.

Start with 7: `grep -rnE -e '--full-auto|untrusted|\[profiles\.|gpt-5\.' .github scripts` shows what an update will break.
