Headless automation: add retry, verification, and resume to `claude -p`
Problem
claude -p runs a single prompt headlessly but has no built-in loop for iterating until an objective is met. In practice, headless automation needs three things that are currently missing:
1. Verification loop
Run Claude, then run a verification command (e.g. npm test, cargo build). If it fails, feed the error back to Claude and retry. Keep going until verification passes or max iterations hit.
Today you need an external wrapper script for this. Something like:
while true; do
claude -p "$PROMPT" --resume "$SESSION_ID" --output-format json
npm test && break # verify
PROMPT="Tests failed: $(npm test 2>&1). Fix it."
done
2. Crash recovery / resume
If Claude crashes mid-run (rate limit, OOM, network), the session is lost unless you manually --resume with the session ID. A headless runner should:
- Persist session ID and iteration count to disk
- On restart, detect the previous session and resume automatically
- Classify the failure (transient → retry with backoff, auth → stop, poison context → new session)
3. Exit code contract
claude -p should exit with meaningful codes:
0= task completed successfully1= task failed (Claude gave up or max iterations)2= crash/infrastructure failure (retryable)
This would let CI/CD pipelines and cron jobs use Claude reliably.
Proposed UX
# Run until `npm test` passes, max 5 iterations, auto-resume on crash
claude -p "Fix the failing tests" \
--verify "npm test" \
--max-iterations 5 \
--resume-on-crash
Or a config file approach:
# .claude/headless.yaml
prompt: "Fix the failing tests"
verify:
- name: tests
command: npm test
- name: typecheck
command: npx tsc --noEmit
max_iterations: 5
resume_on_crash: true
Why this matters
Headless Claude is increasingly used for CI, batch processing, and autonomous coding loops. Without built-in retry/verify/resume, every team writes their own bash wrapper — and most get crash recovery wrong.
This is the single biggest gap between "Claude as a tool" and "Claude as a reliable automation primitive."
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗