codex-cli-invoke-once
Programmatically invoke Codex via the `codex` CLI for headless, non-interactive runs using `codex exec`, machine-readable JSONL via `--json`, final-message capture via `-o/--output-last-message`, and multi-turn continuation via `codex exec resume`. Use when you need scriptable Codex automation for repo reviews, issue investigation, implementation planning, or structured one-shot outputs.
Best use case
codex-cli-invoke-once is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Programmatically invoke Codex via the `codex` CLI for headless, non-interactive runs using `codex exec`, machine-readable JSONL via `--json`, final-message capture via `-o/--output-last-message`, and multi-turn continuation via `codex exec resume`. Use when you need scriptable Codex automation for repo reviews, issue investigation, implementation planning, or structured one-shot outputs.
Teams using codex-cli-invoke-once should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/codex-cli-invoke-once/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How codex-cli-invoke-once Compares
| Feature / Agent | codex-cli-invoke-once | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Programmatically invoke Codex via the `codex` CLI for headless, non-interactive runs using `codex exec`, machine-readable JSONL via `--json`, final-message capture via `-o/--output-last-message`, and multi-turn continuation via `codex exec resume`. Use when you need scriptable Codex automation for repo reviews, issue investigation, implementation planning, or structured one-shot outputs.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
Related Guides
SKILL.md Source
# Codex CLI Invoke Once ## Wrapper commands Users may provide Codex wrappers with any name. Many teams use names like `codex-dev`, `codex-prod`, or `codex-openai`, but do not assume a `codex-*` prefix. - If the wrapper is an executable script or command on PATH, such as `.sh`, `.bat`, or a normal executable, substitute it directly for `codex`. - If the wrapper is a shell alias, shell function, or PowerShell function, invoke it through the corresponding shell instead of treating it like a plain executable. ## Quick start (shell) One-shot run with the last assistant message written to a file: ```bash codex exec -o /tmp/codex-last.txt "Summarize this repo" cat /tmp/codex-last.txt ``` Machine-readable JSONL events on stdout: ```bash codex exec --json -o /tmp/codex-last.txt "Explain recursion" ``` Persist a session and resume it later: ```bash log="$(mktemp)" codex exec --json -o /tmp/codex-last.txt "Start a review of this repo" | tee "$log" thread_id="$(jq -r 'select(.type=="thread.started").thread_id' "$log" | head -n1)" codex exec resume "$thread_id" --json -o /tmp/codex-last-2.txt "Continue that review with a focus on tests" ``` Structured final output with a JSON Schema: ```bash codex exec --output-schema /path/to/schema.json -o /tmp/result.json "Return the requested fields only" ``` ## Heartbeats and deadlines - For long-running work, prefer `--json` so stdout emits a stream of JSONL events. Treat each line as a heartbeat and update a last-seen timestamp while the process is active. - In automation, surface minimal heartbeat status instead of dumping every event unless the caller explicitly wants raw event logs. - Do not add timeouts or terminate a healthy Codex run while heartbeats are still arriving unless one of these applies: (1) the user explicitly requested a deadline, (2) the process is clearly stalled and has stopped emitting heartbeats beyond the caller's chosen inactivity threshold, or (3) the process is in a hard error state. - Do not interrupt simply because an intermediate message already looks good enough. Let the run reach normal completion and then use the final assistant message from `-o` or the terminal JSONL event stream. ## Workflow decision 1. Need one final answer: use `codex exec` and `-o` to capture the last assistant message. 2. Need streaming machine events: add `--json` and parse JSONL from stdout. 3. Need multi-turn state across invocations: capture the `thread_id` from `thread.started` and use `codex exec resume`. 4. Need scripted repo reviews: prefer `codex exec review` over top-level `codex review` because `exec review` supports `--json` and `-o`. 5. Need no durable session files: add `--ephemeral` and do not expect resume to work afterward. ## Session handling (must follow) - If the user provides a Codex `thread_id` or session id, use `codex exec resume <id>`. - If the user says “continue latest chat”, “continue last chat”, or similar, use `codex exec resume --last`. - If the prior run used `--ephemeral`, start a new session instead of attempting resume. - Persist the first `thread.started` event if you may need a follow-up turn later. ## Codex-specific CLI notes - `codex exec` does not use Claude-style `-p` or `--output-format`; pass the prompt as a positional argument and use `--json` for JSONL events. - `codex exec` does not accept `-a` or `--ask-for-approval`. Use the flags listed in `codex exec --help` instead. - `codex exec` supports `--sandbox`, `--full-auto`, `--dangerously-bypass-approvals-and-sandbox`, `--ephemeral`, `--output-schema`, `--json`, and `-o`. - `codex exec resume` supports `--json`, `-o`, `--last`, and `--ephemeral`, but its option surface is smaller than plain `exec`. - Users may provide wrapper commands with arbitrary names. Prefer the exact wrapper name they gave you instead of guessing from `codex`. - Treat stdout as the machine channel and stderr as diagnostics. - Parse JSONL defensively and prefer matching on event `type` instead of hard-coding full schemas. ## Example prompts (to trigger this skill) - “Use Codex CLI to generate a machine-readable repo summary.” - “Use Codex CLI to investigate this error and save the last reply to a file.” - “Use Codex CLI to start a review, then continue it in a second invocation.” ## Credentials Ensure the `codex` CLI is installed and authenticated in the current environment before automating it. ## Resources - Full guide: `references/howto-control-codex-cli-programmatically.md`
Related Skills
copilot-invoke-once
Programmatically invoke GitHub Copilot CLI via the `copilot` command (headless `-p`, `--silent`, `--stream on|off`, `--resume`/`--continue`) to automate tasks like producing code review notes for specific files/dirs, investigating an issue, or drafting an implementation plan. Use when you need scriptable, non-interactive Copilot runs with optional streaming progress and session continuation.
codex-cli-invoke-persist
Create and resume Codex CLI sessions with a persistent session-name-to-thread_id mapping plus last-used model stored in a workspace-scoped JSON file under system temp. Invoke only when the user explicitly names `$codex-cli-invoke-persist` or otherwise clearly requests this exact skill. Use it for deterministic, session-persistent Codex automation across turns and processes.
codex-cli-install
A three-part Codex CLI setup skill with subskills for installation, skip-login configuration, and custom API-key launcher setup. Use when the user wants Codex CLI installed or configured on the current host, with instructions adapted to the actual operating system and runtime environment.
claude-code-invoke-persist
Create and resume Claude Code CLI sessions with a persistent session-name-to-session_id mapping (plus last-used model and reasoning effort) stored in a workspace-scoped JSON file under system temp. Use when the user wants deterministic, session-persistent Claude Code automation across turns and processes.
claude-code-invoke-once
Programmatically invoke Claude Code via the `claude` CLI (headless `-p`, `--output-format json|stream-json`, `--resume`/`--continue`) to automate tasks like producing code review reports for specific files/dirs, investigating an issue, or drafting an implementation plan. Use when you need scriptable, machine-readable output and/or session-persistent multi-turn workflows with Claude Code.
pixi-make-offline-channel
Use when the user wants to create a self-hosted, offline-installable Conda channel (mirror) containing a specific subset of packages using Pixi.
pixi-make-cu-build-env
Guides the agent to setup a new or existing Pixi environment for compiling C++ and CUDA code. It ensures the correct compilers, toolkits, and CMake configurations are in place for a robust user-space build.
pixi-install-nvidia
Use when the user says "use pixi to install <some nvidia tool>" (or similar) and wants NVIDIA/CUDA/GPU packages installed via Pixi (no sudo/apt), e.g., CUDA toolkit pieces, cuDNN/NCCL, PyTorch CUDA builds, RAPIDS.
pei-docker-usage
Helper for PeiDocker (`pei-docker-cli`). Trigger ONLY when the user explicitly requests PeiDocker usage OR when working within a PeiDocker-generated project (indicated by `user_config.yml`).
conan-basic-usage
Basic operations for the Conan C++ package manager. Use when the user explicitly asks to 'use conan' for tasks like creating projects, installing dependencies, or building packages, or asks for 'how to' guidance on Conan setup.
explore-dnn-model
Manual invocation only; use only when the user explicitly requests `explore-dnn-model` by name. Explore how to run a given DNN model checkpoint in the current Python environment by locating weights + upstream source code, resolving dependencies with user confirmation, running reproducible experiments under `tmp/`, and producing reports about I/O contracts, timing, and profiling.
openspec-ext-revise-by-decision
Manual invocation only; use only when the user explicitly requests `openspec-ext-revise-by-decision` by exact name. Revise OpenSpec change artifacts from a review or decision document that contains questions plus `DECISION` blocks, applying chosen decisions from a review file such as `openspec/changes/<change>/review/review-*.md` back into proposal, design, specs, and tasks.