Codex CLI — AI Coding Tool Guide
OpenAI's open-source CLI coding agent. Reasoning strategies, task decomposition, sandboxed execution, and configuration for multi-model workflows.
Codex CLI
Codex CLI is OpenAI's open-source command-line coding agent. It runs in the terminal, uses OpenAI models, and supports sandboxed execution with configurable reasoning strategies for different task types.
What Makes Codex CLI Different
- Open-source — Apache 2.0 license, inspect and modify the source
- Reasoning strategies — Configurable approaches for planning, coding, debugging, and reviewing
- Sandboxed execution — Run untrusted code safely in Docker or VM sandboxes
- OpenAI ecosystem — First-class support for GPT-5 and o-series reasoning models
- Task decomposition — Breaks complex tasks into subtasks with dependency tracking
Getting Started
npm install -g @openai/codex-cli
export OPENAI_API_KEY="sk-..."
cd my-project
codex
Prompt Patterns
Basic Prompts
codex "Add TypeScript types to src/services/api.ts and handle error responses"
Reasoning Strategy
For complex problems requiring step-by-step analysis:
codex --strategy reasoning \
"Design a caching layer for the API. Consider:
- Cache invalidation strategy (TTL vs event-driven)
- What to cache (queries, computed results, rate-limited endpoints)
- Where to cache (in-memory, Redis, edge)
- How to handle cache misses under load"
Planning Strategy
For tasks that need architecture before implementation:
codex --strategy plan \
"Add OAuth2 login with GitHub and Google. Plan the implementation:
- Which library to use (NextAuth vs Clerk)
- Database schema changes needed
- Middleware updates for protected routes
- Session management strategy
Present the plan, then implement step by step."
Code Review Strategy
codex --strategy review \
"Review src/services/payment.ts for:
- Security: input validation, injection risks
- Reliability: error handling, retry logic
- Performance: N+1 queries, unnecessary recomputation
- Compliance: PCI-DSS requirements for payment data"
Debugging Strategy
codex --strategy debug \
"The image upload endpoint returns 500 on PNG files > 10MB.
Trace the pipeline: multer → sharp → S3 upload.
Find where the failure occurs and why only PNGs fail."
Configuration
Codex CLI reads ~/.codex/config.json:
{
"model": "gpt-5",
"reasoning": {
"default_strategy": "auto",
"max_steps": 20,
"checkpoint_frequency": 5
},
"sandbox": {
"enabled": true,
"type": "docker",
"image": "codex-sandbox:latest"
},
"permissions": {
"network": "allowed",
"filesystem_write": "allowed",
"command_execution": "ask"
},
"git": {
"auto_commit": false,
"branch_prefix": "codex/"
}
}
Task Dependencies
Codex CLI tracks task completion and dependencies:
codex \
"Task 1: Create the User model in prisma/schema.prisma
Task 2: Create the API endpoint (depends on Task 1)
Task 3: Write integration tests (depends on Task 2)
Task 4: Generate API docs (depends on Task 2)"
Tasks execute in dependency order. If Task 1 fails, Tasks 2-4 are skipped.
Strategy Selection
| Strategy | Best For | Flags |
|---|---|---|
auto | General tasks | (default) |
reasoning | Complex analysis, architecture | Step-by-step thinking |
plan | Multi-step features | Plan first, implement after approval |
review | Code review, security audit | Analysis only, no changes |
debug | Bug investigation | Trace paths, identify root cause |
Related Pages
- Tool Comparison — Codex CLI vs other AI coding tools
- Prompt Chaining (Prompts) — Multi-step task patterns
- Agentic Prompting (Prompts) — Autonomous agent fundamentals
Related Articles
AI Coding Tools — Prompt Engineering & Configuration
Complete guides for Claude Code, OpenCode, Gemini CLI, Cursor, GitHub Copilot, and more. Tool-specific prompt patterns, configuration files, and setup guides for every major AI coding assistant.
Claude Driving Gemini CLI with Ralph Loop Verification
Claude designs the approach, Gemini CLI executes with iterative self-verification via the Ralph loop. Split reasoning from execution across tools.
OpenCode — Configuration Reference
Complete OpenCode configuration reference. Covers opencode.json settings, MCP server integration, skills system, .opencode/ directory conventions, and environment variables.