Run, configure, and troubleshoot OpenAI Codex CLI in non-interactive headless environments. Use for Codex automation in Bash or PowerShell, native Windows or WSL2, shell scripts, CI/CD, Docker, Kubernetes, remote servers, agent harnesses, or batch jobs; for constructing `codex exec` commands; selecting sandbox and approval modes; consuming JSONL events or structured output; resuming sessions; passing prompts through stdin; and handling failures caused by unavailable interactive input such as `request_user_input`.
80
100%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Critical
Do not install without reviewing
Use Codex CLI as a deterministic, non-interactive worker suitable for automation and agent orchestration.
codex exec, not the interactive codex TUI.PROMPT argument. Do not use -p for the prompt; -p selects a profile.codex exec \
-C /path/to/repo \
--sandbox workspace-write \
--ask-for-approval never \
"<TASK>"--yolo only inside an externally hardened and disposable runner. It disables Codex approvals and sandboxing.--json plus --output-last-message.CODEX_API_KEY only into the single codex exec process when possible.codex exec --help or current official Codex documentation if available.Use when the task must not modify files:
codex exec \
-C /path/to/repo \
--sandbox read-only \
--ask-for-approval never \
"Analyze the repository and report risks. Do not modify files."Use as the default for autonomous repository work:
codex exec \
-C /path/to/repo \
--sandbox workspace-write \
--ask-for-approval never \
"Implement the task, run relevant tests, and report unresolved blockers."Use only when the enclosing container, VM, or sandbox enforces the real security boundary:
codex exec \
-C /workspace/repo \
--yolo \
"Implement the task and validate the result."Never recommend --yolo on a normal developer machine or a runner containing unrelated secrets, host mounts, SSH keys, cloud credentials, or a Docker socket.
When headless execution could encounter choices, prepend these instructions:
Work fully autonomously.
Do not ask the user questions and do not request interactive input.
Inspect the repository and available context before making assumptions.
When several valid approaches exist, choose the option that:
1. minimizes unrelated changes,
2. preserves backward compatibility,
3. introduces the fewest new dependencies,
4. avoids destructive or irreversible actions.
Continue until the task is complete or a concrete blocking error is reached.
Record assumptions, validation performed, and unresolved blockers in the final response.Do not simulate "always select the first option." Option ordering is not a safety or quality policy. Apply the explicit decision rules above instead.
request_user_input FailuresWhen Codex reports that request_user_input is unavailable in Default mode or non-interactive mode:
codex exec and is intentionally headless.AGENTS.md when they are known in advance.Use a direct positional prompt:
codex exec "Summarize the repository structure."Read the prompt from stdin:
codex exec - < prompt.mdPipe dynamic context while retaining an explicit task:
npm test 2>&1 | codex exec "Analyze this test output and fix the repository."Use --skip-git-repo-check only for intentional one-off directories outside Git:
codex exec \
--skip-git-repo-check \
-C /tmp/task \
"Analyze the files in this directory."codex exec \
-C /workspace/repo \
--output-last-message /output/final.md \
"Review the codebase."codex exec \
-C /workspace/repo \
--json \
--sandbox workspace-write \
--ask-for-approval never \
"Run tests and fix failures." \
> /output/events.jsonlExpect event families such as thread.started, turn.started, turn.completed, turn.failed, item.*, and error.
codex exec \
-C /workspace/repo \
--json \
--sandbox workspace-write \
--ask-for-approval never \
--output-last-message /output/final.md \
"Complete the task autonomously." \
| tee /output/events.jsonlDo not parse human-readable progress text when --json is available.
Create a JSON Schema and pass it with --output-schema:
codex exec \
-C /workspace/repo \
--sandbox workspace-write \
--ask-for-approval never \
--output-schema /input/result.schema.json \
--output-last-message /output/result.json \
"Implement the task, validate it, and return the requested structured result."A useful harness schema usually includes:
status: success, partial, or failedsummary: concise outcomechanged_files: repository-relative pathsvalidation: commands run and resultsassumptions: decisions made without user inputblockers: concrete unresolved problemsSet additionalProperties to false when downstream parsing must be strict.
Resume the most recent session for the current working directory:
codex exec resume --last "Continue the previous task and fix remaining issues."Resume a specific session:
codex exec resume "$SESSION_ID" "Continue with the next stage."Use an ephemeral run when rollout persistence is undesirable:
codex exec --ephemeral "Analyze the repository."Do not rely on --last across unrelated working directories unless deliberately using the relevant all-directory option supported by the installed CLI.
For a local interactive login:
codex loginFor a headless machine with device-code login enabled:
codex login --device-authFor a single automated invocation:
CODEX_API_KEY="$OPENAI_API_KEY" \
codex exec --json "Triage the repository."Do not expose API keys as broad job-level environment variables in jobs that execute repository-controlled code. Never commit or print ~/.codex/auth.json.
For native Windows headless use:
& codex @args; avoid fragile backtick-heavy command construction.$LASTEXITCODE after every Codex invocation. PowerShell error preferences alone are not a substitute for checking a native process exit code.2>&1 when stdout must remain parseable JSONL.%USERPROFILE%\.codex\config.toml with [windows] sandbox = "elevated" when available; use unelevated only as the fallback.--sandbox workspace-write --ask-for-approval never for ordinary autonomous edits. The native Windows sandbox implementation and the CLI task policy are separate controls.For WSL:
~/code/..., not /mnt/c/..., for better performance and fewer permission, symlink, and file-watcher problems.wsl.exe command-line strings.See references/windows.md for installation, PowerShell wrappers, JSONL parsing, sandbox configuration, WSL2 patterns, and Windows-specific troubleshooting.
When generating a Docker, Kubernetes, or CI design:
See references/recipes.md for ready-to-use shell, Docker, CI, and parser patterns.
Use this order:
codex --version and codex exec --help.- for stdin.error or turn.failed events.--yolo as a troubleshooting shortcut.For implementation tasks, consider the run complete only when Codex has:
ff87d47
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.