OpenAI Codex CLI Best Practices for Large Codebases: Context Management, Multi-File Edits & Sandbox Safety
OpenAI Codex CLI Best Practices for Large Codebases
OpenAI Codex CLI is a powerful terminal-native AI coding agent that can read, modify, and execute code across your entire project. However, working with large codebases introduces unique challenges around context limits, coordinated multi-file edits, and the very real risk of destructive commands. This guide covers battle-tested best practices to help you work safely and efficiently at scale.
Installation and Initial Setup
Before diving into best practices, ensure your environment is properly configured:
- Install Codex CLI globally:
npm install -g @openai/codex- Set your API key:
- Verify installation:export OPENAI_API_KEY=“YOUR_API_KEY”
- Initialize in your project root:codex —versioncd /path/to/your/large-project codexFor persistent configuration, create a
~/.codex/config.yamlfile:# ~/.codex/config.yaml model: o4-mini approval_mode: suggest notify: true history: true
Context Management Strategies
Large codebases easily exceed the context window. These strategies keep Codex focused on what matters.
1. Use a AGENTS.md File for Project Context
Place an AGENTS.md file in your project root to give Codex persistent instructions without consuming prompt space on every interaction:
# AGENTS.md
Project Overview
This is a microservices monorepo with 47 services.
Backend: Go 1.22, Frontend: React 19, Infra: Terraform.
Conventions
- All API handlers live in /services/
/handlers/
- Database migrations in /migrations/
/
- Never modify files in /generated/ — they are auto-generated.
Safety Rules
- Do NOT run
rm -rf,DROP TABLE, orgit push --force. Always create a branch before making changes.
2. Scope Prompts to Specific Directories
Instead of asking Codex to reason about the entire codebase, narrow the scope:
# Bad: too broad
codex "Refactor the authentication system"
Good: scoped and specific
codex “In services/auth/handlers/login.go, refactor the login handler to use the new JWT middleware from pkg/middleware/jwt.go”
3. Use .codexignore to Exclude Noise
Create a .codexignore file (follows .gitignore syntax) to prevent Codex from indexing irrelevant files:
# .codexignore
node_modules/
vendor/
dist/
build/
*.min.js
*.lock
generated/
__pycache__/
*.pyc
coverage/
### 4. Feed Context Incrementally
For complex tasks, break them into context-building steps:
# Step 1: Let Codex understand the interface
codex "Read services/auth/types.go and summarize the User struct and its methods"
Step 2: Now make the change with established context
codex “Add a LastLoginAt field to the User struct and update all methods that serialize it”
Multi-File Edit Workflows
Coordinated changes across multiple files are where Codex shines — but also where mistakes compound.
1. Always Work on a Branch
# Create a safe working branch before multi-file edits
git checkout -b codex/refactor-auth-middleware
Run your Codex commands
codex “Update all API route handlers in services/ to use the new auth middleware”
Review changes before merging
git diff —stat
2. Use suggest Mode for Multi-File Changes
The suggest approval mode shows you every proposed change before it is applied:
# Launch in suggest mode for maximum safety
codex --approval-mode suggest "Rename the UserService interface to AccountService across all files"
| Approval Mode | File Edits | Command Execution | Best For |
|---|---|---|---|
suggest | Requires approval | Requires approval | Large refactors, unfamiliar code |
auto-edit | Auto-applied | Requires approval | Trusted file edits, risky commands |
full-auto | Auto-applied | Auto-executed | Sandboxed CI environments only |
After Codex completes multi-file edits, run validation:
# Check that the project still compiles
codex "Run the build and report any errors"
Run affected tests
codex “Run tests in services/auth/ and services/user/ and summarize results”
Verify no unintended changes
git diff —name-only
Sandbox Safety Tips
Codex CLI can execute shell commands. In full-auto mode, a single hallucinated command can be catastrophic. Follow these rules religiously.
1. Use Network-Disabled Sandboxing
Codex applies sandboxing by default on supported platforms. Verify it is active:
# On macOS, Codex uses Apple Seatbelt sandboxing
On Linux, configure the sandbox wrapper
codex —sandbox=true “Install dependencies and run tests”
2. Define Writable Directories Explicitly
Restrict where Codex can write files to prevent accidental modifications outside your project:
# In config.yaml or AGENTS.md, specify boundaries
# Only allow writes within the project directory
codex --approval-mode auto-edit "Generate migration files for the new schema"
### 3. Blacklist Dangerous Commands in AGENTS.md
# AGENTS.md — Safety section
## Forbidden Commands
Never execute the following:
- rm -rf /
- git push --force to main or production branches
- DROP DATABASE or DROP TABLE without explicit user confirmation
- chmod 777 on any directory
- curl | bash (piping remote scripts to shell)
- Any command that modifies /etc, /usr, or system directories
### 4. Prefer Dry-Run Flags
When Codex needs to run destructive operations, instruct it to use dry-run modes first:
codex "Run the database migration with --dry-run first, show me the output, then I will confirm the real run"
codex “Use ‘git clean -fdn’ (dry run) to show what would be removed before actually cleaning”
Pro Tips for Power Users
- Chain prompts with context: Use Codex in interactive (REPL) mode for multi-step tasks. Each follow-up prompt inherits the conversation context, reducing the need to re-explain.- Pipe output into Codex: Feed real data into your prompt for precise edits:
- Use model selection strategically: Usegit diff HEAD~3 | codex “Review this diff for security issues and suggest fixes”o4-minifor fast, simple edits ando3for complex multi-file reasoning:
- Commit often with descriptive messages: Ask Codex to commit after each logical change so you can roll back granularly:codex —model o3 “Refactor the payment processing pipeline to support idempotency keys”
- Use full-auto only in disposable environments: CI containers and dev sandboxes are the only safe places forcodex “Stage and commit the auth middleware changes with a descriptive commit message”full-automode. Never use it on a production machine.
Troubleshooting Common Issues
| Problem | Cause | Solution |
|---|---|---|
| Codex ignores files or gives incomplete answers | Context window exceeded; too many files loaded | Use .codexignore, scope prompts to specific directories, and break large tasks into steps |
Error: OPENAI_API_KEY not set | API key not in environment | Run export OPENAI_API_KEY="YOUR_API_KEY" or add to ~/.bashrc |
| Codex tries to run destructive commands | Running in full-auto without guardrails | Switch to suggest mode and add safety rules to AGENTS.md |
| Multi-file edit produces inconsistent changes | Codex lost context between files | Break the edit into smaller, per-file tasks or use interactive mode for continuity |
| Sandbox errors on Linux | Missing sandbox dependencies | Ensure you are running Node.js 22+ and your OS supports the required isolation features |
How do I prevent Codex CLI from modifying auto-generated files in my project?
Add the generated file paths to a .codexignore file in your project root (same syntax as .gitignore). Additionally, explicitly state in your AGENTS.md file that these directories are off-limits. For example, add generated/ to .codexignore and include a rule like "Never modify files in /generated/" in your AGENTS.md safety section. This two-layer approach ensures Codex neither reads nor writes to those paths.
What is the safest approval mode for running Codex on a production-adjacent codebase?
Use suggest mode (codex —approval-mode suggest). This requires your explicit approval for every file edit and every shell command before execution. The auto-edit mode is acceptable for trusted codebases where you only want to gate command execution. Never use full-auto outside of isolated, disposable environments like CI containers or ephemeral VMs.
Can Codex CLI handle monorepos with dozens of services effectively?
Yes, but you must manage context carefully. Scope each prompt to a specific service or directory rather than asking Codex to reason about the entire monorepo at once. Use AGENTS.md to document the project structure so Codex understands the layout without needing to scan every file. For cross-service refactors, break the work into per-service steps and validate each one before moving to the next. Combining .codexignore with targeted prompts keeps Codex fast and accurate even in repositories with hundreds of thousands of files.