Day 5, 25 minutes · advanced
Why it matters
So far you started every run by typing in a Claude Code session. That is fine for one task. It does not work for a task you want to run from a script, from a scheduled job, or ten times in a row for ten files, because there is nobody at the prompt to type or to answer a question.
For that Claude Code has a non-interactive mode, also called headless: claude -p with your prompt. It runs once, prints the result and exits, like any other command. Your script can read the output and check the exit code to see whether the run worked.
Today you run Claude Code from your terminal without opening a session. You pick exactly which tools a run may use, put a cap on it, get the result as JSON, continue a run with a follow-up, and read the exit code. At the end Ines has a one-line command that adds a card and proves the check still passes.
The flags you will use
All of these come from the headless page and the CLI reference.
| Flag | What it does |
|---|---|
-p "prompt" |
Runs the prompt once without the interactive screen and prints the answer |
--allowedTools "..." |
Tools that run without asking, in the same rule syntax as your settings, such as Read,Edit,Bash(git check) |
--permission-mode dontAsk |
Refuses anything that would need a prompt and is not allowed in advance |
--max-turns 10 |
Stops after ten turns. A run that hits the limit ends with an error |
--max-budget-usd 1 |
Stops once Claude Code's own cost estimate reaches that amount. The estimate can differ from your bill |
--output-format json |
Prints one JSON object with the answer in result, the session_id and a cost estimate |
--continue |
Continues the most recent conversation in this folder |
Who answers the questions?
In a -p run nobody can answer a permission prompt. When nothing sets a mode, a -p run starts in Manual mode, and every call that would need your approval is refused. So the run can only do what you allowed in advance: with --allowedTools, with allow rules in your settings, or with a mode.
The docs pair --permission-mode dontAsk with an exact allow-list for locked-down runs. Your settings still apply: your deny rule keeps check.sh safe and your ask rule for git reset is refused, because dontAsk never asks. Your Stop hook from lesson 4 also runs in a -p run, so the run cannot end while the check fails.
Two things to know about trust. A -p run never shows the trust question. In a folder you have never trusted, the allow rules in its shared settings file are not used, but its hooks do run. So only run claude -p in folders you know. Your practice folder was trusted in lesson 1.
You may read about --bare, which skips hooks, CLAUDE.md and other project files for a faster start. It does not use your Claude login, so it needs an API key, and it skips the hooks this course relies on. Leave it out for now.
Exit codes
Claude Code exits with code 0 on success and with another number when the run fails. A wrong flag is reported before the run starts. To read the code right after a command:
- macOS, Linux and Git Bash:
echo $? - Windows PowerShell:
$LASTEXITCODE
In a script you check that number and only carry on when it is 0. Also ask Claude for a clear last word, such as OK or FAIL, so a person or a script can read the answer quickly.
Continue a run
claude -p "..." --continue continues the most recent conversation in the folder, with everything Claude learned in the first run. If you run several conversations at once, take the session_id from the JSON output and pass it to --resume instead.
The steps
- Ask a question without opening a session, and read the exit code.
- Run a task with an exact allow-list, a mode that never asks, a turn limit and JSON output.
- Read the JSON: the answer, the session ID and the cost estimate.
- Check the work yourself with
git checkandgit log. - Continue the run with a follow-up question.
- Hit a limit on purpose and see how a failed run looks.
- Save your command as a note for the next time.
Follow the steps for Claude Code below.
Example: Ines's one-line card
Ines saved this command in a text file: add a card, run the check, commit, reply OK or FAIL, at most ten turns, only the tools that job needs. It printed one JSON object with OK in result and exit code 0. When she tried it with --max-turns 1, it stopped early with an error and no OK. That is the signal her future script will look for: it checks the exit code first, then the answer.
Common mistakes
- No allow-list. The run starts in Manual mode, every edit is refused and Claude reports that it could not do the work.
--allowedTools "Bash". That allows every shell command. Name the exact commands.- Trusting the text, not the exit code. A script should look at the exit code first and the answer second, and treat a run that ended with an error as failed.
Today's actions
Ticks are not saved without an accountDo it in Claude Code
Claude Code
- In your terminal, go into the practice folder and ask one question without opening a session. Claude only reads files, which needs no approval.
cd kettleby-tools claude -p "How many cards are in items/ and which ones have Status: out? Answer in a short list." - Read the exit code of that run: type
echo $?on macOS, Linux or Git Bash, or$LASTEXITCODEin Windows PowerShell. 0 means the run worked. - Run a real task with an exact allow-list, a mode that never asks, a turn limit and JSON output. Paste it as one line.
claude -p "Add items/glue-gun.txt for a glue gun on shelf B2 with Status: in. Run git check, then commit the card with the message: Add glue gun. Reply with only OK if git check passed, otherwise FAIL." --allowedTools "Read,Edit,Write,Bash(git check),Bash(git add *),Bash(git commit *)" --permission-mode dontAsk --max-turns 10 --output-format json - Read the JSON: find
result(it should say OK),session_idandtotal_cost_usd, which is an estimate. Then read the exit code again and check the work yourself.git check git log --oneline -3 - Continue the same conversation with a follow-up question.
claude -p "Which file did you add in the last run, and what did git check print?" --continue - Now hit the limit on purpose with one turn. The run ends with an error instead of an answer. Read the exit code and the output: a script should check both.
claude -p "Add items/claw-hammer.txt for a claw hammer on shelf B2 with Status: in. Run git check, then commit the card. Reply with only OK or FAIL." --allowedTools "Read,Edit,Write,Bash(git check),Bash(git add *),Bash(git commit *)" --permission-mode dontAsk --max-turns 1 - Run
git status. If it listsitems/claw-hammer.txt, the run stopped halfway: delete that file. Then save the glue gun command in a note with one line per flag.git status
Note: Paste each claude -p command as one line; the same line works in PowerShell, Git Bash and macOS or Linux terminals. Do not add --bare: it skips your login and needs an API key. A -p run in a folder you never trusted does not use that folder's shared allow rules but does run its hooks, so only use it in folders you know.
Paid planNeeds a paid Claude plan (Pro, Max, Ultra, Team or Enterprise) or a Claude Console account. The free Claude plan does not include Claude Code.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up