OpenAI Codex CLI is an open-source command-line AI coding assistant that provides code generation and project operation capabilities based on OpenAI’s models. This article will cover its installation, configuration, common usage, and pitfalls to watch out for.
What is Codex CLI
Codex CLI is a command-line tool from OpenAI that allows developers to interact directly with AI in the terminal to accomplish tasks like writing code, file operations, and debugging. It’s an open-source project built on Node.js with sandbox mode for code execution.
Similar to Claude Code, Codex CLI also adopts an “agent” mode—AI can autonomously read files, execute commands, and modify code, rather than just passively answering questions.
Installation
Install Codex CLI globally via npm:
npm install -g @openai/codex
Verify the installation:
codex --version
Configuration
Set your OpenAI API Key:
export OPENAI_API_KEY=sk-xxxxx
You can also write the Key to a .env file or shell configuration file to avoid manually setting it each time.
Basic Usage
Interactive Mode
Run codex directly to enter interactive mode and chat with AI in the terminal:
codex
# Enter the interactive conversation interface
Single Execution
Use the -q flag to execute a single command, suitable for scripting scenarios:
codex -q "Add a .gitignore file to the current project"
Specify Working Directory
Use the --cd flag to specify the working directory:
codex --cd /path/to/project
Common Pitfalls
Permission Issues
Codex CLI runs in sandbox mode by default, and certain operations may be denied. If you need more permissive access, you can use --full-auto mode, but be aware of security risks:
codex --full-auto
Use --full-auto only in trusted environments, and always track changes with git.
Context Loss
Similar to Claude Code, Codex CLI also loses context during long conversations. Recommendations:
- Break large tasks into smaller steps
- Use a
CODEX.mdfile to persist important context - Start new conversations periodically
Unstable Output
Due to the non-deterministic nature of LLMs, the same prompt may produce different results. For scenarios requiring precise output:
- Use more explicit prompts
- Specify output format (e.g., “only output JSON”)
- Set
temperature=0to reduce randomness
Best Practices
- Always Use Git — Ensure your project has Git version control before using Codex CLI, making it easy to rollback unwanted changes
- Specify Clear Scope — Explicitly tell Codex CLI which files to modify in your prompts to avoid unnecessary changes
- Use Sandbox Mode — Unless necessary, avoid
--full-autoand let Codex CLI operate in a restricted environment - Verify Output — Test and review generated code, don’t blindly trust AI output
← Back to ArticlesYou can manage both Claude Code and Codex CLI simultaneously with Wishmere, and it will automatically select the most suitable tool based on tasks.