Skip to main content

Syntax

Description

The exec 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

string
Prompt to send to Codex. Use - to read from stdin.

Output

boolean
Output events as JSON Lines (JSONL). Each line is a valid JSON object representing an event.
path
Write the final message to a file. Useful for capturing Codex’s response.
path
Path to a JSON schema file. Codex will format its final output to match this schema.

Model & Provider

string
AI model to use (e.g., gpt-4.1, o4-mini).
boolean
Use open-source/local model provider.
string
OSS provider: ollama, lmstudio.

Approval & Sandbox

boolean
Run in full-auto mode. No approvals required.
string
Sandbox mode: workspace-write, workspace-read-network-write, read-only, danger-full-access.
boolean
Skip all approvals and sandbox restrictions. Use with extreme caution.

Input

string
Comma-separated list of image paths.
path
Additional writable directories (repeatable).

Progress Indicators

string
Control colored output: auto, always, never.
boolean
Show progress indicator. Auto-detected by default.

Session

path
Working directory.
boolean
Don’t save session to history.
boolean
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:
Common event types:
  • text - Text messages from Codex
  • exec_approval_request - Requesting approval to run a command
  • apply_patch_approval_request - Requesting approval to modify files
  • turn_complete - Task completed
  • error - Error occurred

Exit Codes

  • 0 - Success
  • 1 - Error occurred (failed commands, API errors, etc.)