You invoke OpenAI Codex CLI in headless (non-interactive) mode from within Claude Code using codex exec.
Before each invocation, verify the latest model available from the active client or provider and use only that model for code implementation, review, architecture analysis, security checks, and other tasks.
Prerequisites
codexCLI is installed and authenticated (codex loginorOPENAI_API_KEYset)- The working directory should be a git repository (or use
--skip-git-repo-check)
Modes
Review mode (read-only)
For code review, analysis, second opinions. Codex cannot modify files.
codex exec --sandbox read-only --ephemeral [OPTIONS] "PROMPT"
Implementation mode (workspace-write)
For code generation and implementation. Codex can create and modify files in the workspace.
codex exec --full-auto --ephemeral [OPTIONS] "PROMPT"
--full-auto is equivalent to --sandbox workspace-write with auto-approval of file writes.
Common flags
| Flag | Purpose |
|---|---|
--ephemeral |
Do not persist session to disk (always include) |
-m MODEL |
Use the latest model verified at invocation time |
--skip-git-repo-check |
Allow running outside a git repo |
-C DIR |
Set working directory |
-o FILE |
Write final message to a file |
--json |
Output JSON Lines event stream |
--output-schema FILE |
Force structured output with JSON Schema |
-i IMAGE |
Attach image file to the prompt |
-c key=value |
Override config (e.g. -c model_reasoning_effort="xhigh") |
Workflow
For implementation
- Identify the files/features to implement
- Write a detailed prompt with requirements, file paths, and design constraints
- Run
codex exec --full-auto --ephemeral -C PROJECT_DIR "PROMPT"via Bash (timeout: 300000ms) - Verify the generated files with Read tool
- Run tests/linting to validate quality
- Report results to the user
For review
- Parse the user's request to determine the task type
- Run
codex exec --sandbox read-only --ephemeral "PROMPT"via Bash (timeout: 120000ms) - Present the result to the user
- If the user wants to act on suggestions, implement them
Built-in review command
codex exec review
Reviews the current diff against the base branch without needing a custom prompt.
Implementation prompt tips
- Include the full file path where code should be written
- Reference existing files that Codex should read for context
- Specify the language, framework, and coding style
- Include type signatures, interfaces, or examples from the codebase
- Be specific about error handling and edge cases
- One task per invocation for best results
Important notes
- Set timeout to 120000-300000ms for
codex execcalls (implementation takes longer) - Progress logs go to stderr, final answer goes to stdout
- Token usage is shown at the end of the output
- If the user has ChatGPT Plus/Pro, Codex usage is included in the subscription
- If using API key, each call consumes OpenAI API credits
--dangerously-bypass-approvals-and-sandboxはサンドボックスを無効化し、ファイルシステム全体への書き込みを許可するため使わない- Codex requires a git repo by default; add
--skip-git-repo-checkfor non-repo directories