Claude Code as a Sub-Agent
Claude Code is Anthropic's terminal coding agent. Its headless mode,
claude -p (a.k.a. --print), makes it a disposable sub-agent: you write the
metaprompt, Claude explores/edits/runs commands on its own, and returns a final
message you capture and verify. You stay the planner; Claude is the implementer —
it's a strong long-context planner, so this is useful for reviews, big-picture
design, and cross-cutting refactors.
Golden rule: Claude's self-report is a claim, not the truth. Capture its
output, then independently verify the artifacts yourself (read the files, re-run
the tests) before treating the task as done.
Verified locally: claude 2.1.167. Confirm install with claude --version.
The core command
claude -p "your metaprompt here" \
--add-dir <workdir> \
--permission-mode <default|acceptEdits|plan|bypassPermissions> \
--output-format json >/tmp/claude_<rand>.json 2>>/tmp/claude_<rand>.log
-p, --print — headless: print the response and exit. Required for scripting.
- Prompt as the arg, OR pipe via stdin:
echo "$PROMPT" | claude -p (good for
long/generated prompts).
--output-format — text (default), json (single object with result +
session_id + cost/usage), or stream-json (NDJSON events as they arrive).
Use json for clean capture and to grab the session_id for resume.
- Redirect stdout to a capture file and stderr to a log so progress noise
doesn't pollute your context. Generate
<rand> once per run.
--model <alias|id> — pin the model (e.g. opus, sonnet, or a full id).
--add-dir <dir>... — extra directories Claude may read/write.
--append-system-prompt <text> — inject extra instructions/persona.
Permission modes (pick the least privilege)
default — Claude asks before edits/commands. In headless -p it cannot
prompt, so write actions are effectively blocked → good for read-only
review/analysis.
plan — Claude plans but does not edit. Use for "design this / find the bug".
acceptEdits — auto-accepts file edits. Use for real implementation.
bypassPermissions (or --dangerously-skip-permissions) — no gating at all.
Powerful; only use when the user has explicitly authorized autonomous edits AND
the working dir is scoped, or in a throwaway dir. When unsure, ask which mode
before granting write access.
Output & session resume
Delegation patterns
- One-shot task — scope
--add-dir, acceptEdits, capture, verify.
- Second opinion / adversarial review —
plan mode, ask Claude to critique
your diff or find bugs. Different model, different blind spots.
- Plan/implement split — Claude designs; you implement (or vice-versa).
- Parallel fan-out — for N independent tasks, launch N
claude -p runs, each
with its own capture/log file. For concurrent WRITE runs, give each its own
git worktree add dir and point --add-dir there to avoid edit collisions.
The supervision loop (never skip)
- Delegate —
claude -p ... --output-format json >out 2>>log.
- Capture — read the
result from out (and log/stream-json for detail).
- Verify INDEPENDENTLY — do not trust "tests pass" / "done":
git status / git diff to see what actually changed.
- Read the changed files yourself.
- Re-run the build/tests/linter yourself and read the real output.
- Iterate — if wrong,
claude -p --continue "<correction>" and repeat.
- Report — state what changed, what you verified, and how. Say plainly when
something failed or was skipped.
Cleanup: remove /tmp/claude_* capture/log files (and any throwaway worktrees)
when done.
1---2name: claude-code3description: Drive Anthropic's Claude Code CLI (`claude -p`) as a non-interactive coding sub-agent from inside Codex. Use when you want to delegate a coding/analysis/refactor task to Claude, get a second opinion / adversarial review from another model, hand off long-context planning, or fan out parallel agents. Covers the exact `claude -p` flags, permission modes, output capture, JSON/stream-json modes, session resume, parallel fan-out, and the mandatory "delegate → capture → independently verify, never trust the self-report" supervision loop.4metadata:5 short-description: Delegate tasks to the Claude Code CLI6---78# Claude Code as a Sub-Agent910Claude Code is Anthropic's terminal coding agent. Its **headless** mode,11`claude -p` (a.k.a. `--print`), makes it a disposable sub-agent: you write the12metaprompt, Claude explores/edits/runs commands on its own, and returns a final13message you capture and verify. You stay the planner; Claude is the implementer —14it's a strong long-context planner, so this is useful for reviews, big-picture15design, and cross-cutting refactors.1617**Golden rule:** Claude's self-report is a claim, not the truth. Capture its18output, then independently verify the artifacts yourself (read the files, re-run19the tests) before treating the task as done.2021Verified locally: `claude` 2.1.167. Confirm install with `claude --version`.2223## The core command2425```bash26claude -p "your metaprompt here" \27 --add-dir <workdir> \28 --permission-mode <default|acceptEdits|plan|bypassPermissions> \29 --output-format json >/tmp/claude_<rand>.json 2>>/tmp/claude_<rand>.log30```3132- `-p, --print` — headless: print the response and exit. Required for scripting.33- Prompt as the arg, OR pipe via stdin: `echo "$PROMPT" | claude -p` (good for34 long/generated prompts).35- `--output-format` — `text` (default), `json` (single object with `result` +36 `session_id` + cost/usage), or `stream-json` (NDJSON events as they arrive).37 Use `json` for clean capture and to grab the `session_id` for resume.38- Redirect stdout to a capture file and stderr to a log so progress noise39 doesn't pollute your context. Generate `<rand>` once per run.40- `--model <alias|id>` — pin the model (e.g. `opus`, `sonnet`, or a full id).41- `--add-dir <dir>...` — extra directories Claude may read/write.42- `--append-system-prompt <text>` — inject extra instructions/persona.4344## Permission modes (pick the least privilege)4546- `default` — Claude asks before edits/commands. In headless `-p` it cannot47 prompt, so write actions are effectively blocked → good for **read-only**48 review/analysis.49- `plan` — Claude plans but does not edit. Use for "design this / find the bug".50- `acceptEdits` — auto-accepts file edits. Use for real implementation.51- `bypassPermissions` (or `--dangerously-skip-permissions`) — no gating at all.52 Powerful; only use when the user has explicitly authorized autonomous edits AND53 the working dir is scoped, or in a throwaway dir. When unsure, ask which mode54 before granting write access.5556## Output & session resume5758- Parse `--output-format json`: the `result` field is the final message; the59 `session_id` field lets you continue the same conversation.60- Resume with context intact:61 ```bash62 claude -p --continue "now also update the tests" # most recent session63 claude -p --resume <session-id> "..." # a specific session64 ```65- Use resume for multi-turn delegation (draft → refine → fix) instead of66 re-sending all context.6768## Delegation patterns6970- **One-shot task** — scope `--add-dir`, `acceptEdits`, capture, verify.71- **Second opinion / adversarial review** — `plan` mode, ask Claude to critique72 your diff or find bugs. Different model, different blind spots.73- **Plan/implement split** — Claude designs; you implement (or vice-versa).74- **Parallel fan-out** — for N independent tasks, launch N `claude -p` runs, each75 with its own capture/log file. For concurrent WRITE runs, give each its own76 `git worktree add` dir and point `--add-dir` there to avoid edit collisions.7778## The supervision loop (never skip)79801. **Delegate** — `claude -p ... --output-format json >out 2>>log`.812. **Capture** — read the `result` from `out` (and `log`/stream-json for detail).823. **Verify INDEPENDENTLY** — do not trust "tests pass" / "done":83 - `git status` / `git diff` to see what actually changed.84 - Read the changed files yourself.85 - Re-run the build/tests/linter yourself and read the real output.864. **Iterate** — if wrong, `claude -p --continue "<correction>"` and repeat.875. **Report** — state what changed, what you verified, and how. Say plainly when88 something failed or was skipped.8990Cleanup: remove `/tmp/claude_*` capture/log files (and any throwaway worktrees)91when done.