OpenAI Codex CLI
OpenAI Codex CLI is a lightweight coding agent that runs locally on your computer, providing AI-powered assistance directly in your terminal. It's part of OpenAI's suite of developer tools and integrates with your ChatGPT plan for seamless usage.
What is Codex CLI?
Codex CLI is a command-line interface that brings AI-powered coding assistance to your terminal. Unlike cloud-based solutions, Codex CLI runs locally on your machine, providing fast and private coding assistance without sending your code to external servers.
Key Features
- Local Execution: Runs entirely on your local machine
- Terminal Integration: Works directly in your command line
- ChatGPT Integration: Uses your existing ChatGPT plan
- Model Context Protocol (MCP): Supports MCP servers for extended functionality
- Zero Data Retention (ZDR): Option for enhanced privacy
- Multi-Platform: Available for macOS, Linux, and Windows
- Sandbox & Approvals: Safe execution environment with user approval
Installation
Using Package Managers
npm (Node.js):
npm install -g @openai/codex
Homebrew (macOS):
brew install codex
Manual Installation
Download the appropriate binary from the GitHub Releases:
macOS:
- Apple Silicon (arm64):
codex-aarch64-apple-darwin.tar.gz
- Intel (x86_64):
codex-x86_64-apple-darwin.tar.gz
Linux:
- x86_64:
codex-x86_64-unknown-linux-musl.tar.gz
- arm64:
codex-aarch64-unknown-linux-musl.tar.gz
Windows:
- x86_64:
codex-x86_64-pc-windows-msvc.zip
After downloading, extract and rename the binary to codex
:
# Extract the archive
tar -xzf codex-aarch64-apple-darwin.tar.gz
# Rename to codex
mv codex-aarch64-apple-darwin codex
# Make executable
chmod +x codex
# Move to PATH (optional)
sudo mv codex /usr/local/bin/
Getting Started
1. Initial Setup
Run Codex CLI for the first time:
codex
2. Authentication
Sign in with ChatGPT (Recommended):
- Select "Sign in with ChatGPT" when prompted
- Use your existing ChatGPT Plus, Pro, Team, Edu, or Enterprise plan
- No additional API key setup required
API Key Authentication:
- Use your OpenAI API key for usage-based billing
- Configure in
~/.codex/config.toml
3. Basic Usage
# Start interactive session
codex
# Run with a specific prompt
codex "Create a Python script to parse CSV files"
# Run with file input
codex --file script.py "Optimize this code for performance"
Configuration
Codex CLI stores configuration in ~/.codex/config.toml
:
# Basic configuration
[general]
model = "gpt-4"
temperature = 0.1
max_tokens = 4000
# Authentication
[auth]
provider = "chatgpt" # or "openai"
# MCP Servers
[mcp_servers]
# Add your MCP server configurations here
# Sandbox settings
[sandbox]
enabled = true
auto_approve = false
Configuration Options
Option | Description | Default |
---|---|---|
model | AI model to use | gpt-4 |
temperature | Response randomness (0-1) | 0.1 |
max_tokens | Maximum response length | 4000 |
provider | Authentication provider | chatgpt |
sandbox.enabled | Enable sandbox mode | true |
sandbox.auto_approve | Auto-approve safe operations | false |
Core Features
1. Interactive Coding
# Start interactive session
codex
# Example conversation:
# You: Create a REST API in Python using FastAPI
# Codex: I'll create a FastAPI REST API for you...
# [Codex generates code and files]
2. File Operations
# Analyze existing code
codex --file app.py "Review this code and suggest improvements"
# Generate new files
codex "Create a Dockerfile for a Node.js application"
# Refactor code
codex --file utils.py "Refactor this code to use async/await"
3. Project Management
# Initialize new project
codex "Set up a new React project with TypeScript and Tailwind CSS"
# Add features to existing project
codex "Add authentication to this Express.js application"
# Debug issues
codex --file error.log "Help me debug this application error"
4. Documentation Generation
# Generate README
codex "Create a comprehensive README for this project"
# Add code comments
codex --file main.py "Add detailed docstrings to all functions"
# Create API documentation
codex "Generate OpenAPI documentation for this FastAPI app"
Model Context Protocol (MCP)
Codex CLI supports MCP servers for extended functionality:
# ~/.codex/config.toml
[mcp_servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/directory"]
[mcp_servers.git]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-git", "--repository", "/path/to/repo"]
Available MCP Servers
- Filesystem: File and directory operations
- Git: Git repository management
- Database: Database querying and management
- Web: Web scraping and API interactions
- Custom: Your own MCP servers
Sandbox & Security
Sandbox Mode
Codex CLI includes a sandbox environment for safe code execution:
# Enable sandbox mode
codex --sandbox
# Auto-approve safe operations
codex --auto-approve
Approval System
- Safe Operations: Automatically approved (file reading, code analysis)
- Moderate Risk: Requires approval (file creation, package installation)
- High Risk: Always requires explicit approval (system commands, network access)
Zero Data Retention (ZDR)
For enhanced privacy, enable ZDR mode:
codex --zdr
This ensures your data is not retained by OpenAI.
Advanced Usage
1. Non-Interactive Mode
# CI/CD integration
codex --non-interactive "Generate unit tests for this function"
# Batch processing
echo "Create a Python class for user management" | codex
2. Verbose Logging
# Enable detailed logging
codex --verbose "Debug this application"
# Trace mode for development
codex --trace "Analyze this codebase"
3. Custom Prompts
# Use custom system prompt
codex --system-prompt "You are a senior Python developer" "Review this code"
# Load prompt from file
codex --prompt-file prompts/code-review.txt --file app.py
Integration Examples
1. VS Code Integration
// .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Codex: Code Review",
"type": "shell",
"command": "codex",
"args": [
"--file",
"${file}",
"Review this code and suggest improvements"
],
"group": "build"
}
]
}
2. Git Hooks
#!/bin/sh
# .git/hooks/pre-commit
codex --non-interactive "Review the staged changes and ensure code quality"
3. CI/CD Pipeline
# .github/workflows/codex-review.yml
name: Codex Code Review
on: [pull_request]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Codex CLI
run: npm install -g @openai/codex
- name: Code Review
run: codex --non-interactive "Review the changes in this PR"
Best Practices
1. Security
- Use sandbox mode for untrusted code
- Review generated code before execution
- Keep API keys secure
- Use ZDR mode for sensitive projects
2. Performance
- Use specific prompts for better results
- Break large tasks into smaller chunks
- Cache frequently used configurations
- Monitor token usage
3. Collaboration
- Document your Codex workflows
- Share useful prompts with your team
- Use consistent configuration across projects
- Integrate with your existing development tools
Troubleshooting
Common Issues
Authentication Problems:
# Clear authentication cache
rm -rf ~/.codex/auth
# Re-authenticate
codex --login
Permission Errors:
# Fix binary permissions
chmod +x codex
# Check PATH
which codex
Network Issues:
# Test connectivity
codex --test-connection
# Use proxy
codex --proxy http://proxy.company.com:8080
Debug Mode
# Enable debug logging
codex --debug "Your prompt here"
# Check configuration
codex --config-check
# Validate installation
codex --validate
Comparison with Other Tools
Feature | Codex CLI | GitHub Copilot | ChatGPT Web | Cursor |
---|---|---|---|---|
Environment | Terminal | IDE | Browser | IDE |
Privacy | Local + ZDR | Cloud | Cloud | Local + Cloud |
Cost | ChatGPT Plan | Subscription | Subscription | Subscription |
Customization | High | Medium | Low | High |
Integration | CLI Tools | IDE Only | Browser Only | IDE Only |
Offline | Partial | No | No | Partial |
When to Use Codex CLI
Choose Codex CLI when:
- You prefer terminal-based workflows
- You need local execution for privacy
- You want to integrate with CLI tools and scripts
- You're working with multiple programming languages
- You need custom automation and workflows
- You want to use your existing ChatGPT plan
Consider alternatives when:
- You primarily work in IDEs
- You need real-time code completion
- You prefer GUI-based interactions
- You need offline-only functionality
Resources
- GitHub Repository: github.com/openai/codex
- Documentation: docs.openai.com/codex
- ChatGPT Integration: chatgpt.com/codex
- MCP Documentation: modelcontextprotocol.io
- Community: GitHub Discussions and Issues
Contributing
Codex CLI is open source and welcomes contributions:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
See the Contributing Guide for detailed instructions.