Sending Tasks and Questions to Codex CLI
Delegate coding tasks or ask questions to OpenAI's Codex CLI (codex exec) from within Claude Code. Codex runs non-interactively and returns its output.
Choosing the Right Mode
| Intent | Approach | Key Flags |
|--------|----------|-----------|
| Coding task (fix, build, refactor) | codex exec with task prompt | --full-auto, -C <dir> |
| Question / analysis | codex exec with question prompt | --ephemeral, -o <file> |
| Code review | codex review | --uncommitted, --base <branch> |
Constructing the Command
Coding Tasks
For tasks where Codex should modify files:
codex exec --full-auto -C <working-dir> "<task description>"
--full-autoenables sandboxed automatic execution (no approval prompts)-C <dir>sets the working directory (use the relevant repo/worktree path)- The task description should be specific and actionable
Example:
codex exec --full-auto -C /Users/thopper/c/my-project "Fix the broken import in src/utils.py that causes a NameError when calling parse_config()"
Questions and Analysis
For questions where you want Codex's analysis without file changes:
codex exec --ephemeral -o /tmp/codex-response.md -C <working-dir> "<question>"
--ephemeralavoids persisting the session to disk-o <file>writes Codex's final response to a file for easy reading- After the command completes, read the output file to relay the answer
Example:
codex exec --ephemeral -o /tmp/codex-response.md -C /Users/thopper/c/my-project "Explain how the converter DAG in diffusify_core/converters works and identify any potential circular dependencies"
Code Review
For reviewing changes:
# Review uncommitted changes
codex review --uncommitted -C <working-dir>
# Review changes against a base branch
codex review --base main -C <working-dir>
# Review with custom instructions
codex review --base main -C <working-dir> "Focus on security vulnerabilities and error handling"
Optional Flags
| Flag | Purpose |
|------|---------|
| -m <model> | Override the model (e.g., -m o3, -m gpt-5.3-codex) |
| -s read-only | Sandbox to read-only (good for questions, prevents file changes) |
| --add-dir <dir> | Add extra writable directories |
| -i <file> | Attach an image to the prompt |
Workflow
-
Understand the user's intent: Is this a coding task (Codex should change files) or a question (Codex should analyze and respond)?
-
Formulate the prompt: Write a clear, specific prompt for Codex. Include:
- What to do or what to answer
- Relevant file paths or context
- Any constraints (e.g., "don't modify tests", "use the existing pattern in X")
-
Pick the working directory: Use the repo root or the specific worktree where changes should land. Default to the current working directory if appropriate.
-
Run the command: Execute via Bash tool. For long-running tasks, consider running in the background.
-
Report results:
- For coding tasks: summarize what Codex did, check
git diffin the target directory - For questions: read the output file (
-o) and relay the answer - For reviews: present the review findings
- For coding tasks: summarize what Codex did, check
Tips
-
Long prompts: For complex tasks, pipe the prompt from stdin:
codex exec --full-auto -C <dir> <<'EOF' Your detailed multi-line prompt here. Include specific files, constraints, and expected outcomes. EOF -
Background execution: For tasks that may run long, use Bash with
run_in_background: trueand check back later. -
Combining outputs: After a coding task, inspect the changes with
git diffin the target directory before reporting back. -
Read-only for safety: When asking questions, add
-s read-onlyif you want to guarantee Codex won't modify any files.