Syntax
Description
Theexec command runs Codex non-interactively, making it ideal for automation, CI/CD pipelines, and scripting. It reads a prompt, executes the task, and exits when complete.
Use
codex exec when you need:- Headless operation (no interactive TUI)
- Structured JSON output
- Automation in scripts or CI/CD
- Output piped to other commands
Usage
Basic Execution
From stdin
Read the prompt from stdin:JSON Output
Get structured JSON events:Options
Prompt to send to Codex. Use
- to read from stdin.Output
Output events as JSON Lines (JSONL). Each line is a valid JSON object representing an event.
Write the final message to a file. Useful for capturing Codex’s response.
Path to a JSON schema file. Codex will format its final output to match this schema.
Model & Provider
AI model to use (e.g.,
gpt-4.1, o4-mini).Use open-source/local model provider.
OSS provider:
ollama, lmstudio.Approval & Sandbox
Run in full-auto mode. No approvals required.
Sandbox mode:
workspace-write, workspace-read-network-write, read-only, danger-full-access.Skip all approvals and sandbox restrictions. Use with extreme caution.
Input
Comma-separated list of image paths.
Additional writable directories (repeatable).
Progress Indicators
Control colored output:
auto, always, never.Show progress indicator. Auto-detected by default.
Session
Working directory.
Don’t save session to history.
Skip Git repository check.
Examples
CI/CD Integration
JSON Output for Processing
Structured Output Schema
Automation Scripts
Reading from stdin
Different Models
JSON Output Format
When using--json, each line is a JSON object with this structure:
text- Text messages from Codexexec_approval_request- Requesting approval to run a commandapply_patch_approval_request- Requesting approval to modify filesturn_complete- Task completederror- Error occurred
Exit Codes
0- Success1- Error occurred (failed commands, API errors, etc.)
Related Commands
codex- Interactive modecodex review- Code review mode