Codex: Codex Advanced Tips
Last updated: 2026-08-31
With basic operations mastered, this lesson covers advanced tips to maximize Codex's efficiency.
📋 Prerequisites: Proficient with Codex basic operations (see Lessons 3-8)
1. What You Will Learn
- Multi-task parallelism strategies
- Context optimization techniques
- Error recovery methods
- Efficiency-boosting secrets
2. Multi-Task Parallelism
(1) When to Parallelize
| Scenario | Strategy |
|---|---|
| Modifying different files | Can parallelize |
| Modifying the same file | Must be sequential |
| Tasks with dependencies | Must be sequential |
| Independent feature modules | Can parallelize |
(2) Parallel Operation Methods
App Mode: Create multiple tasks in the left panel, each runs independently.
Web Mode: Submit multiple tasks simultaneously to cloud sandboxes.
CLI Mode: Open multiple terminal windows, each running a Codex instance.
▶ Example 1: Alice's Parallel Strategy
TEXT
📖 Display only
# Alice handles three independent modules simultaneously
Task 1: Refactor src/auth/ module
Task 2: Add Swagger docs to src/api/
Task 3: Optimize src/utils/ performance
# Three tasks modify different directories — can run in parallel
# Estimated time savings: 60%
3. Context Optimization
(1) Context Window Management
Codex's context window is limited — proper management improves results:
| Tip | Description |
|---|---|
| Precise Addition | Only add relevant files to context |
| Timely Removal | Remove completed files from context |
| Regular Compaction | Use /compact to compress history |
| New Sessions When Needed | Split large tasks into multiple sessions |
(2) Context Addition Strategy
TEXT
📖 Display only
# Good practice
/add src/auth/login.ts # Only add relevant files
/add src/types/auth.ts # Add type definitions
# Bad practice
/add src/**/*.ts # Too many files, wastes context
(3) AGENTS.md as Persistent Context
AGENTS.md is automatically included as context without consuming the window:
MARKDOWN
# AGENTS.md
## Project Structure
- src/auth/ - Authentication module
- src/api/ - API routes
- src/utils/ - Utility functions
## Conventions
- Use TypeScript strict mode
- All APIs need error handling
▶ Example 2: Bob's Context Optimization
TEXT
📖 Display only
# Bob working on a large project
/status # View: context 75% used
# Optimize
/remove src/legacy/ # Remove irrelevant files
/compact # Compress history
# Still not enough
/new # Start new session
/add src/payments/ # Only add current task-related files
4. Error Recovery
(1) When Codex Makes Mistakes
TEXT
📖 Display only
# Scenario: Codex runs tests and they fail
> Codex: Tests failed, 3 cases did not pass
> You: View details of failed cases, fix them one by one
# Scenario: Codex modified the wrong file
> Codex: Modified config.ts (should have modified constants.ts)
> You: Revert config.ts changes, modify constants.ts
(2) Git Rollback
BASH
# View changes
git diff
# Revert specific file
git checkout -- src/wrong-file.ts
# Revert all changes
git checkout -- .
# Go back to previous commit
git reset --hard HEAD~1
(3) Iterative Correction
TEXT
📖 Display only
# First attempt
> "Migrate Express to Fastify"
Result: Some routes not correctly converted
# Append correction
> "Check the /api/users route, the conversion syntax is wrong, refer to Fastify official docs"
Result: Route fix completed
# Continue correcting
> "Middleware conversion also has issues, error handling needs Fastify's setNotFoundHandler"
Result: Middleware fix completed
5. Efficiency-Boosting Secrets
(1) Prompt Optimization
| Tip | Description | Example |
|---|---|---|
| Provide Examples | Give expected output format | "Output in the following format: ..." |
| Reference Files | Specify reference files | "Follow the style of auth.py" |
| Step-by-step | Break large tasks into steps | "Step 1: ... Step 2: ..." |
| Include Verification | Specify how to verify | "Ensure npm test passes" |
| Set Constraints | Limit scope | "Only modify the src/auth/ directory" |
(2) Template-Based Prompts
TEXT
📖 Display only
# Fix Bug Template
"Fix <Bug-description> in <file>.
Error message: <error-log>
Expected behavior: <correct-behavior>
Ensure <verify-command> passes."
# Add Feature Template
"Add <feature> to <module>.
Follow the code style of <existing-file>.
Include input validation and error handling.
Write unit tests."
# Refactor Template
"Refactor <old-impl> to <new-impl>.
Maintain the external interface.
Ensure all existing tests pass.
Add new tests covering the new implementation."
(3) Automated Pipelines
BASH
# One command: code fix → test → commit
codex --full-auto "Fix all lint errors, run tests, if passing then git commit -m 'fix: lint errors'"
6. Common Pitfalls & How to Avoid Them
| Pitfall | Avoidance |
|---|---|
| Context overflow | Regularly /compact, use /new when needed |
| Blindly accepting changes | Review every diff |
| Task too large | Split into small steps, execute one by one |
| Forgetting Git protection | Always commit before operations |
| Parallel modifications to same file | Strictly separate task scopes |
❓ FAQ
Q How do I know if context is sufficient?
A Use
/status to view usage. When exceeding 80%, consider compacting or starting a new session.Q What if Codex keeps misunderstanding my intent?
A Try a different description, provide specific examples, or reference existing code. English descriptions usually work better.
Q Will parallel tasks interfere with each other?
A Cloud sandbox mode won't — each task runs independently. Local multi-instance mode may cause file conflicts; ensure you're modifying different files.
Q How do I make Codex follow project code style?
A Define style rules in AGENTS.md — Codex will automatically follow them. You can also reference ESLint/Prettier configs.
Q How large a project can Codex handle?
A Depends on the context window. 128K tokens can handle roughly a few thousand lines of code. Large projects require precise context file addition, not full loading.
📖 Summary
- Multi-task parallelism: different files parallel, same file sequential
- Context optimization: precise addition, timely removal, regular compaction
- Error recovery: append corrections / Git rollback / iterative development
- Efficiency boost: template-based Prompts + include verification + automated pipelines
- Avoid pitfalls: context overflow, blind acceptance, oversized tasks
📝 Exercises
- Basic (⭐): Use iterative development to complete a feature — write a skeleton first, then gradually improve, recording each appended instruction.
- Intermediate (⭐⭐): Run two independent tasks in parallel, record the time savings ratio.
- Advanced (⭐⭐⭐): Create your own Codex Prompt template library covering five scenarios: Fix Bug / Add Feature / Refactor / Test / Review.