Delegate coding tasks to OpenAI Codex CLI as a sub-agent. Use when the user asks to delegate work, run parallel coding tasks, get a code review or second opinion, explore code with fresh context, or when multiple independent subtasks can be parallelized. Use only when the task justifies delegation overhead (> 2 min of direct work) and benefits from fresh context or parallelism.
Delegate coding tasks to OpenAI's Codex CLI (codex exec) as a sub-agent. This gives you (the host agent) sub-agent capabilities — running independent AI agents that work on focused tasks and return results.
You — the AI coding agent reading this skill right now — are the host agent. Whether you are Windsurf/Cascade, Claude Code, Cursor, Codex, Gemini CLI, or any other SKILL.md-compatible agent, this skill is talking to you. When this document says "the host agent should...", it means you should.
Tool Priority — always check before delegating:
Prefer direct work when:
codex CLI installed and on PATH (npm i -g @openai/codex)codex login status returns success--skip-git-repo-check)Model strictness controls how aggressively the host agent routes tasks to cheaper/faster models:
| Strictness | Behavior | When to Use |
|---|---|---|
| conservative | Always use best model | Production code, security-sensitive |
| balanced (default) | Downgrade when >80% confident task is simple | Most everyday work |
| aggressive | Always try cheapest first, upgrade on failure | Cost-conscious, high-volume |
Set via environment variable: export CODEX_SUBAGENT_MODEL_STRICTNESS=balanced
Model routing guidance (when balanced):
| Task Type | Override Flags |
|---|---|
| Simple search/explain | -m <cheaper-model> -c model_reasoning_effort="low" |
| Standard review/analyze | -c model_reasoning_effort="medium" |
| Complex implement/refactor | -c model_reasoning_effort="high" |
| Critical/security | -c model_reasoning_effort="xhigh" |
Run once before the first delegation in a session:
python3 <skill_dir>/scripts/codex_preflight.py
Read the output. If any [FAIL] line appears, follow the hint printed next to it before delegating.
[WARN] lines are informational — they do not block use.
# Skip the git repo check (mirrors the wrapper's own flag):
python3 <skill_dir>/scripts/codex_preflight.py --skip-git-repo-check
# Skip auth check in CI / no-TTY environments:
python3 <skill_dir>/scripts/codex_preflight.py --skip-auth
The wrapper script (scripts/run_codex.py) is the only interface you should use to invoke Codex. Never call codex directly. The wrapper handles safety enforcement, default flags, temp files, git worktrees, and error messages automatically.
# Basic usage — pipe prompt via stdin:
echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode read-only -
# Write task with high collision confidence:
echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode write --collision high -
# Write task with medium collision confidence (auto git worktree):
echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode write --collision medium -
# Enable web search:
echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode read-only --web-search -
# Review with a review prompt file:
echo "<file paths and context>" | python3 <skill_dir>/scripts/run_codex.py --mode read-only --review-prompt /path/to/security-prompt.md -
# Resume a previous session:
echo "<FOLLOW-UP>" | python3 <skill_dir>/scripts/run_codex.py --resume -
| Flag | Required | Values | Default | Purpose |
|---|---|---|---|---|
| --mode | Yes (unless --resume) | read-only, write | read-only | Sets sandbox level |
| --collision | Only for --mode write | high, medium | high | Controls git worktree isolation |
| --web-search | No | (flag) | off | Enables web search for this spawn |
| --review-prompt | No | file path | none | Reads file and prepends to stdin before passing to codex |
| --timeout | No | 300, 600, 1200, 2400 | 600 | Max seconds before kill |
| --resume | No | (flag) | off | Resume last session |
| --persist | No | (flag) | off | Keep session on disk (required for future --resume) |
| --skip-git-repo-check | No | (flag) | off | Run outside a git repository |
| --max-parallel | No | integer | 6 | Max concurrent sub-agents (override not recommended) |
| --status | No | (flag) | off | Print active/stale agent status and exit |
| - | Yes | (literal dash) | — | Read prompt from stdin (must be last argument) |
Wrapper flags are parsed first. Any unrecognized flags are passed through to codex exec after a safety scan (see Passthrough Flags below). The wrapper will reject dangerous flags with helpful error messages.
You can also use the standard POSIX -- separator to explicitly mark the boundary between wrapper flags and passthrough flags:
echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode read-only -- -m o3 -c model_reasoning_effort="high" -
Any flag not listed in the Wrapper Flags table above is forwarded directly to codex exec — unless it is blocked by the safety scanner. This is how you pass model overrides, config tweaks, feature toggles, and subcommand args.
Safe passthrough flags:
| Flag | Example | Purpose |
|---|---|---|
| -m / --model | -m o3 | Override model |
| -c / --config | -c model_reasoning_effort="high" | Config override (non-sandbox keys only) |
| --output-schema | --output-schema /path/to/schema.json | Structured JSON output via schema |
| -i / --image | -i screenshot.png | Attach image(s) to prompt |
| --enable / --disable | --enable streaming | Toggle codex features |
| -p / --profile | -p fast | Load a named config profile |
| --oss | --oss | Use open-source provider |
| --local-provider | --local-provider ollama | Specify local model provider |
| review --uncommitted | review --uncommitted | Review subcommand: uncommitted changes |
| review --base | review --base main | Review subcommand: diff against branch |
Blocked flags — never pass these; the wrapper exits with an error and usage guidance if any are detected:
| Flag | Reason |
|---|---|
| --sandbox / -s | Controlled by --mode |
| --dangerously-bypass-approvals-and-sandbox | Not permitted by skill policy |
| --full-auto | Controlled by --mode write |
| --ephemeral | Controlled by --persist |
| -o / --output-last-message | Managed by wrapper (result file) |
| --json | stdout is captured internally — output would be lost |
| --cd / -C / --add-dir | Wrapper controls working directory |
| -c sandbox* | Sandbox config overrides not permitted |
The wrapper prints the output file path to stdout (one line):
/tmp/codex-abc123/result.txt
On worktree mode, it also prints the worktree branch:
/tmp/codex-abc123/result.txt
WORKTREE_BRANCH=codex-work-abc123
WORKTREE_DIR=/tmp/codex-wt-codex-work-abc123
Always run the wrapper as a non-blocking/background command and poll for completion:
rm -rf <result-dir> (wrapper does NOT auto-delete)| Tier | Timeout | When to Use |
|---|---|---|
| Short | --timeout 300 | Simple searches, single-file reviews |
| Standard | --timeout 600 | Code reviews, analysis, standard implementation |
| Long | --timeout 1200 | Large refactors, multi-file implementation |
| Extended | --timeout 2400 | Major migrations, full-codebase analysis |
On timeout: inform user, re-launch with --persist + next tier up. If 40min times out, report to user.
--collision medium)echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode write --collision high -
git diff, incorporate changesgit diff HEAD..<WORKTREE_BRANCH>, merge if approved, cleanupecho "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode read-only -
codex exec review)review passthrough:
echo "Focus on security" | python3 <skill_dir>/scripts/run_codex.py --mode read-only review --uncommitted -
Or against a branch:
echo "Focus on performance" | python3 <skill_dir>/scripts/run_codex.py --mode read-only review --base main -
--output-schema passthrough:
echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode read-only --output-schema /path/to/schema.json -
--persist:
echo "<PROMPT>" | python3 <skill_dir>/scripts/run_codex.py --mode write --collision high --persist -
echo "<FOLLOW-UP>" | python3 <skill_dir>/scripts/run_codex.py --resume -
When to resume vs new invocation:
Before every write delegation, assess how likely the sub-agent's work will conflict with your own:
| Level | Confidence | Example | Action |
|---|---|---|---|
| High | >80% no conflict | You edit src/auth/, delegating src/payments/ | Delegate directly |
| Medium | 40-80% | Both touch files in src/api/ but different ones | Delegate via git worktree (--collision medium) |
| Low | <40% | Both need to edit the same file | Do NOT delegate writes — do it yourself or delegate as read-only |
Assessment criteria:
The wrapper handles worktree setup automatically with --collision medium:
WORKTREE_BRANCH from wrapper stdoutgit diff HEAD..<WORKTREE_BRANCH>git merge <WORKTREE_BRANCH>git worktree remove <WORKTREE_DIR> and git branch -d <WORKTREE_BRANCH>run_codex.py) — never call codex directly--review-prompt — pipe text into the wrapper or point to a file, never interpolate prompt content into shell argsgit diff before telling the user the task is done--persist if you plan to --resume later — otherwise the session is ephemeral--max-parallel N if absolutely necessary (not recommended — increases resource usage and cost)python3 <skill_dir>/scripts/run_codex.py --status to see active agents, their mode, start time, and run durationrm -rf <result-dir>)When a delegation fails or returns unexpected results, read references/ERROR_HANDLING.md for exit code reference, retry strategy, and troubleshooting table.
See references/PROMPT_PATTERNS.md for templates and examples. See references/PROMPT_ENGINEERING.md for OpenAI/Codex best practices.
Key principles:
[GOAL], [CONTEXT], [FILES], [CONSTRAINTS], [OUTPUT] sectionsNever include these in prompts or delegate access to them:
.env, *.pem, *.key, credentials.*, secrets.*Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer