✻Founding-member pricing: every lesson for $19/mo. The first 3 lessons are free. Start lesson 1 →
Unit 8 · Automate Repeat Work · Lesson 33 of 40

Headless Mode: Scripting Claude with -p

Run Claude Code non-interactively with claude -p: piping input, JSON and streaming output, allowed tools, permission modes and safe limits for scripts and CI.

ACADEMY12 min+74 XP4-question quiz

🎯 By the end of this lesson you'll be able to

  • Run one-shot tasks with claude -p
  • Parse JSON and streaming output
  • Lock down tools and permissions for unattended runs
  • Set turn and budget limits

Claude as a command-line tool

claude -p "<prompt>" (short for --print) runs Claude Code without the interactive UI. It does the task, prints the result and exits, which means you can use it in shell scripts, git hooks, cron jobs and CI pipelines.

bash
claude -p "summarize what changed in the last 10 commits"
git log --oneline -20 | claude -p "write release notes from these commits"
cat build-error.txt | claude -p "explain the root cause in two sentences" > diagnosis.txt

Output formats

FormatFlagUse
Text--output-format text (default)Humans and simple scripts
JSON--output-format jsonIncludes result, session_id and total_cost_usd
Stream--output-format stream-json --verboseNewline-delimited events for live progress
bash
claude -p "count the TODO comments in src/" --output-format json | jq -r '.result'

Need a strict shape? Add --json-schema '<schema>' and read structured_output from the JSON.

Lock it down

Unattended runs should be least-privilege. With no human watching, anything that would normally prompt is denied unless you approve it up front:

bash
claude -p "fix lint errors in src/" \
  --allowedTools "Read,Edit,Bash(pnpm lint *)" \
  --permission-mode dontAsk \
  --max-turns 15
  • --allowedTools pre-approves exactly what's needed, in permission-rule syntax.
  • --permission-mode dontAsk denies everything else.
  • --max-turns and --max-budget-usd stop runaway jobs.
  • --bare skips auto-discovery of local hooks, skills, plugins, MCP servers and CLAUDE.md for reproducible CI runs (it uses an API key).
!
Five questions before any unattended run

1) Is "done" defined? 2) Can it check its own work? 3) What is it allowed to touch? 4) What stops it (turns, budget)? 5) Who reviews the result? If you can't answer all five, keep a human in the loop.

Multi-step scripts

Capture the session ID and continue the same conversation:

bash
sid=$(claude -p "start a review of src/payments" --output-format json | jq -r '.session_id')
claude -p "now check the error handling specifically" --resume "$sid"
✓
Test on three before running on three hundred

Running a fix across many files? Try the command on three first, review the diffs, adjust the prompt, then loop over the rest.

⌨Type the command+15 XP

Run Claude headless to "list the TODOs in src" with JSON output

❯
◎Which flag solves it?pick the best fit
Your script needs the cost and session ID of each run.
The JSON output includes total_cost_usd and session_id.
A CI job should be able to run tests but nothing else.
Pre-approve exactly the test command.
A job occasionally loops forever.
Cap the number of turns.
A second command should continue the first one's conversation.
Resume by session ID.

Check your understanding

?Lesson quiz10 XP per correct answer · +25 for a perfect score
QUESTION 1 OF 4

What does claude -p do?

Why: -p / --print is headless mode for scripts and CI.
QUESTION 2 OF 4

Which output format includes total_cost_usd and session_id?

Why: JSON output has the result plus metadata.
QUESTION 3 OF 4

For a locked-down CI run, which permission mode denies everything not pre-approved?

Why: dontAsk plus --allowedTools is the least-privilege combination.
QUESTION 4 OF 4

How do you continue a headless conversation in a second command?

Why: Capture session_id from the JSON output and pass it to --resume.
0 / 4 correct
Academy lesson

Unlock lesson 33 and the next 7

You've finished the free lessons. Join the Academy for $19/month to unlock every lesson, quiz, terminal simulation and puzzle, plus the final exam and your certificate of completion.

  • All 40 lessons across 9 units
  • 165+ quiz questions
  • Final exam + certificate
  • New lessons as Claude Code evolves
  • XP, streaks and badges
  • Cancel anytime

Already a member? Log in to continue.

Key takeaways

  • claude -p makes Claude a scriptable CLI. Pipe in, print out.
  • Use json or stream-json output for machine-readable results.
  • Unattended means least privilege: --allowedTools + dontAsk + turn and budget limits.
  • Answer the five questions before running without a human.

Nice work. Lesson 33 done?

Mark it complete to earn +74 XP, keep your streak alive and unlock badges.

Written by Curtis Cornell from Anthropic's public Claude Code documentation. Last reviewed November 14, 2026. Claude Code changes fast; if something here differs from your version, the official docs win.

Start lesson 1 free →