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
Thecodex exec command runs Codex in non-interactive mode:
Command-Line Options
Run without persisting session rollout files to disk. Useful for temporary automation tasks.
Print events to stdout as JSONL for programmatic parsing.
Write the last message from the agent to the specified file.
Path to a JSON Schema file describing the model’s final response shape.
Control color output:
always, never, or auto.Force cursor-based progress updates in exec mode.
CI/CD Integration
GitHub Actions Example
Environment Variables
Approval Modes for Automation
Output Formats
Human-Readable Output
By default, exec mode prints human-readable progress to stderr:JSON Lines (JSONL) Output
For programmatic parsing, use--json:
Structured Output with Schema
Provide a JSON Schema to enforce structured output:Example JSON Schema
Example JSON Schema
schema.json
Working with Images
Attach images to your exec prompt:Session Management
Ephemeral Sessions
Use--ephemeral to avoid writing session data to disk:
- 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:Task completed successfully
Task failed or error occurred
Best Practices
Use Ephemeral Mode
For CI/CD tasks that don’t need session persistence
Capture Output
Use
--output-last-message to save resultsEnable JSON Mode
Parse events programmatically with
--jsonSet Appropriate Sandbox
Use
--full-auto for safe automationExamples
Generate Documentation
Generate Documentation
Automated Code Review
Automated Code Review
Batch Processing
Batch Processing
Scheduled Maintenance
Scheduled Maintenance
Troubleshooting
Task doesn't complete automatically
Task doesn't complete automatically
Ensure you’re using an appropriate approval mode:
- Use
--full-autofor 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 ...
CI job times out
CI job times out
- Set explicit timeouts in your CI configuration
- Use
--ephemeralto reduce startup overhead - Consider breaking large tasks into smaller steps
Authentication fails in CI
Authentication fails in CI
- Verify
OPENAI_API_KEYis 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