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

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

Output

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

Model & Provider

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

Approval & Sandbox

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

Input

-i, --images
string
Comma-separated list of image paths.
--add-dir
path
Additional writable directories (repeatable).

Progress Indicators

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

Session

-C, --cwd
path
Working directory.
--ephemeral
boolean
Don’t save session to history.
--skip-git-repo-check
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.)