AI & LLMs · Guide · AI & Prompt Tools
How to Use Codex CLI
Every Codex CLI command with examples. npm install, sandbox approval modes, /review, /init, config file gotchas — covers the OpenAI terminal agent in 5 min.
Codex CLI is OpenAI’s open-source terminal agent: a `codex` command that edits files, runs shell commands, and reasons about code inside a sandbox.
Advertisement
Codex CLI gives you an agentic loop at the command line, backed by OpenAI’s coding-tuned models. You launch `codex` in any directory, describe a task, and it reads files, proposes edits, runs commands, and iterates until the task is done or it needs your input. Unlike a chat window, it keeps execution inside a sandbox and surfaces diffs for approval before touching disk, which makes it safer to hand real work.
What it is
Codex CLI is an Apache-2.0 project from OpenAI on GitHub. It ships as an npm package and authenticates with either a ChatGPT login (tied to your plan’s usage) or an OpenAI API key. The agent is multimodal — it can read images pasted into the terminal — and uses the Responses API under the hood. It supports MCP servers for extending tools.
Install
# Node 20+ npm install -g @openai/codex # first run prompts for login or API key codex
First session
Run `codex` in a project directory. Pick an approval mode (suggest, auto-edit, or full-auto) and describe a task.
$ codex > Fix the failing test in tests/users.spec.ts → reads test → greps source → proposes patch → runs npm test
Everyday workflows
- Bug triage — paste a failing CI log and let Codex reproduce and patch locally.
- Small features — scope a single endpoint or component and let it run in auto-edit mode.
- Refactors — ask for a rename or extraction, review the diff, commit.
Gotchas and tips
Full-auto mode is tempting but will happily rewrite large swaths of code if the task is vague. Use suggest mode for anything near production, and keep dirty working trees out — Codex occasionally stages changes it then undoes. Commit or stash before starting.
The sandbox blocks network by default on some platforms; if your task needs npm install or a curl, enable network access explicitly for that session. A codex.md or AGENTS.md file at the repo root lets you pin conventions that apply every run.
Who it’s for
Developers already in the OpenAI ecosystem who want a terminal-native alternative to ChatGPT’s code interpreter, with real access to their local filesystem. Start with a test-fix task — narrow, verifiable, and a great sanity check of the tool’s behavior.
Use these while you read
Tools that pair with this guide
- System Prompt BuilderCompose a focused system prompt from a role, tone, constraints, and output format — copy-ready for any LLM.AI & Prompt Tools
- AI Prompt GeneratorTurn a vague idea into a structured prompt. Pick role, task, context, constraints, and output format. Works with ChatGPT, Claude, and Gemini.AI & Prompt Tools
- AI Token CounterEstimate tokens, characters, words, and approximate API cost for GPT-4o, GPT-4, Claude, and Gemini — before you hit send.AI & Prompt Tools
- AI Prompt LibraryBrowse a curated catalog of prompt templates for writing, coding, marketing, and research. One click to copy.AI & Prompt Tools
Advertisement
Continue reading
- AI & LLMsGitHub Copilot Pricing and ComparisonCompare free vs paid GitHub Copilot tiers and analyze it against ChatGPT, Cursor, and Tabnine. Find the best value plan instantly with this free online guide.
- AI & LLMsGitHub Copilot Features and CapabilitiesTest what Copilot really does — code accuracy, scope limits, debugging, web dev, legacy code, tests, docs, team customization. Free guide, no sign-up.
- AI & LLMsGitHub Copilot Security and Data HandlingAudit where your code goes, who sees it, training-data policy, network needs, and what happens when Copilot suggests broken code. Free, no sign-up.
- AI & LLMsAI Fluency SkillsThe 8 sub-skills of AI fluency: prompt structure, model selection, tool use, quality calibration, iteration, context management, cost awareness, privacy.
- AI & LLMsAnthropic Skills ExplainedSkills as Anthropic's answer to Custom GPTs — markdown-defined, version-controlled in git, work in terminal. Anatomy + Skills vs Custom GPTs.
- AI & LLMsKimi K2 vs DeepSeek V3Two open-weight Chinese flagships. Kimi K2 = 1M context, DeepSeek V3.2 = top-tier reasoning + coding. Pick by use case.