Codex CLI: OpenAI’s Autonomous AI Coding Terminal Tool

📘 Tutorials 2026-07-19 · Updated 2026-11-22 3 min read

Codex CLI reads, edits and runs commands itself. What matters is 3 sandbox levels plus approvals, exec mode, AGENTS.md, and how it splits work with Aider.

📜 Table of Contents

📖 Keep reading

Codex CLI tutorial: exec mode, the three sandbox levels, and AGENTS.md

Codex CLI is OpenAI's terminal coding agent. It reads files, edits them, runs commands and reacts to failures. What decides whether it is safe on your machine is not model quality but two gates: sandboxing and approvals.

Install and sign in

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

Node 18+. On Windows use WSL2 — the sandbox relies on Landlock/seccomp on Linux or Seatbelt on macOS, which native Windows lacks.

Sign in with codex login (ChatGPT plan) or export OPENAI_API_KEY=... (API billing). Local or third-party endpoints go in ~/.codex/config.toml; check the repo README for exact fields.

Two ways to run

Mode Command Use
Interactive TUI codex Conversational work, step by step
Non-interactive codex exec "task" Scripts, CI, batches

Piping failures in is the most practical trick:

pytest -q 2>&1 | tail -40 > err.txt
codex exec "read err.txt, fix the three failing tests, do not change assertions"

Three sandbox levels

Level Allows When
read-only Reads only, all writes denied Reviews and architecture questions
workspace-write Writes inside the working dir and temp The sensible default
danger-full-access Everything, including network and the whole disk Disposable containers only

Approvals are a second axis: always ask, ask on failure, or never. Workspace-write plus ask-on-failure rarely interrupts normal work but stops when a task wants to install packages or touch system paths.

Flag names have changed across releases (older docs use --full-auto and --yolo; newer builds split sandbox and approval), so run codex --help before copying commands from a blog post.

AGENTS.md

Codex reads AGENTS.md on every task: at the repo root for shared rules, in subdirectories for that subtree, and in ~/.codex/AGENTS.md for personal preferences.

# Conventions
- Python 3.12 + FastAPI, dependencies via uv
- Every public endpoint needs type hints and error codes
- Schema changes require an Alembic migration
## Forbidden
- Do not edit already-merged files under migrations/
- Do not add new HTTP client libraries

The prohibitions matter more than the style rules: the most common agent failure is not bad code but an unapproved new dependency.

Codex versus Aider

Aspect Codex CLI Aider
Role Autonomous agent running commands Human-led pair editing
Models Mostly OpenAI Any provider, local included
Isolation Built-in sandbox + approvals Git only
Best for End-to-end tasks, CI Precise edits to a few files

Three habits that pay off

Start read-only for the plan, then allow writes. Write tasks as acceptance criteria ("return 400 on empty password, with tests"). Read git diff every round — the agent writes fast, you own the merge.

FAQ

Needs internet? Yes by default; offline use requires a self-hosted compatible endpoint.

Windows? Use WSL2; native sandbox support is still limited.

Does my code leave the machine? Yes, context goes to the model provider. Check data terms for private repos.

Why are test runs slow? The agent actually executes commands. Scope tests with -k to save time.

Flag names and defaults change between releases; check the official repo and codex --help.

Related

Related Articles
2026-07-26
DevOps AI Playbook for Infrastructure Teams
2026-08-03
AI YouTube Automation 2026: yt-dlp (182k Stars) + Whisper - Build a Free Content Pipeline
2026-07-27
ElevenLabs Voice Cloning Tutorial 2026: Complete Guide to AI Voice Generation
2026-08-05
AI Meeting Notes App in 2026: faster-whisper (25k Stars) + WhisperX Local Pipeline - Free Transcription with Speaker Labels
2026-07-26
AI Developer PC Requirements 2026
2026-07-23
Prompt Engineering Guide Chinese 2026: Complete Tutorial in Chinese

Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.

💬 Comments (0)

No comments yet. Be the first!

Login to comment