Skip to main content

Overview

Exec mode allows you to run Codex non-interactively for automation, CI/CD pipelines, and scripted workflows. Instead of launching the interactive TUI, Codex executes your prompt, completes the task, and exits automatically.
Exec mode is ideal for automated workflows where you need Codex to complete a task without human intervention.

Basic Usage

The codex exec command runs Codex in non-interactive mode:
You can also pass the prompt via stdin:
Or from a file:

Command-Line Options

--ephemeral
boolean
default:"false"
Run without persisting session rollout files to disk. Useful for temporary automation tasks.
--json
boolean
default:"false"
Print events to stdout as JSONL for programmatic parsing.
--output-last-message
string
Write the last message from the agent to the specified file.
--output-schema
string
Path to a JSON Schema file describing the model’s final response shape.
--color
string
default:"auto"
Control color output: always, never, or auto.
--progress-cursor
boolean
default:"false"
Force cursor-based progress updates in exec mode.

CI/CD Integration

GitHub Actions Example

Environment Variables

Always store API keys and sensitive credentials as secrets in your CI/CD platform.

Approval Modes for Automation

1

Full Auto Mode

Use --full-auto for complete automation with sandboxing:
This is equivalent to:
2

Danger Mode (Use with Caution)

Only for externally sandboxed environments:
This disables all safety checks. Only use inside Docker or other isolated environments.

Output Formats

Human-Readable Output

By default, exec mode prints human-readable progress to stderr:
Output:

JSON Lines (JSONL) Output

For programmatic parsing, use --json:
Each line is a JSON event:

Structured Output with Schema

Provide a JSON Schema to enforce structured output:
schema.json

Working with Images

Attach images to your exec prompt:

Session Management

Ephemeral Sessions

Use --ephemeral to avoid writing session data to disk:
Benefits:
  • Faster startup (no session loading)
  • No disk I/O overhead
  • Clean environment for one-off tasks

Resume Previous Sessions

You can resume a previous exec session:

Error Handling

Exec mode exits with status codes for automation:
Exit Code 0
success
Task completed successfully
Exit Code 1
error
Task failed or error occurred
Example with error handling:

Best Practices

Use Ephemeral Mode

For CI/CD tasks that don’t need session persistence

Capture Output

Use --output-last-message to save results

Enable JSON Mode

Parse events programmatically with --json

Set Appropriate Sandbox

Use --full-auto for safe automation

Examples

Troubleshooting

Ensure you’re using an appropriate approval mode:
  • Use --full-auto for automated execution
  • Check that your prompt is clear and has defined completion criteria
  • Enable verbose logging to see what’s happening: RUST_LOG=debug codex exec ...
  • Set explicit timeouts in your CI configuration
  • Use --ephemeral to reduce startup overhead
  • Consider breaking large tasks into smaller steps
  • Verify OPENAI_API_KEY is set correctly
  • Check that the API key has sufficient permissions
  • Ensure your organization allows API access

See Also

Execution Policies

Control which commands can be executed

Tracing & Logging

Debug with verbose logging

Custom Instructions

Add project-specific guidance

Sandbox Configuration

Configure sandboxing behavior