What this thread does
For an unattended claude -p job, pass --allowedTools for the exact commands it needs (or --permission-mode dontAsk for a locked-down run), and add --output-format stream-json --verbose if you parse events (the docs' own streaming examples include --verbose) (checked 2026-10-30, Claude Code docs; commands below are untested).
Anthropic's 2.1.314 notes make claude -p print an error and exit 1 when startup fails, 2.1.317 makes claude -p print an error and exit 1 in text/JSON output when a full disk or quota loses transcript or ~/.claude.json writes (as of November 2026, 2.1.321 also reports an error and exits 1 in text/JSON output when a full Linux NFS export or exhausted NFS quota loses transcript lines), and 2.1.311 escapes control/format characters in JSON strings without changing parsed values (checked 2026-11-17).
Do first: pick the permission setup
Since Claude Code v2.1.302, claude -p starts in Manual mode when no permission mode is set, and a call that needs approval is denied unless something approves it. Source: permission modes and headless (Anthropic). The docs do not say a denied call fails the run; as an inference, a job can finish "successfully" without doing its work, so check the output.
claude -p "Run the test suite and fix any failures" --allowedTools "Bash,Read,Edit"
| Goal | Flags (from the docs) |
|---|---|
| Allow exact commands | --allowedTools "Bash(git diff ),Bash(git status ),Bash(git commit )". The space before matters: Bash(git diff*) also matches git diff-index |
| Deny everything not pre-approved | --permission-mode dontAsk: calls that would prompt are denied; read-only commands and your allow rules still run |
| Let a classifier decide | --permission-mode auto; a bare Bash entry in --allowedTools is dropped in auto mode |
| Edits without prompts | --permission-mode acceptEdits; other shell commands still need an allow rule |
| Nobody can answer a host prompt | --permission-prompts none (v2.1.259 or later; earlier versions reject it) |
Do not use a blanket bypass to fix a denied job. Rule syntax, including wrappers and compound commands, is in claude-code-permission-allow-rules-ignored.
Make the run reproducible
--bareskips hooks and auto-discovery of skills, plugins, MCP servers, auto memory and CLAUDE.md; skills in directories passed with--add-dirstill load. It does not read OAuth or the keychain: setANTHROPIC_API_KEY. The docs call it the recommended mode for scripted calls and say it will become the default for-pin a future release.- Without
--bare, a-prun shows no workspace trust dialog and still runs the project's.claude/settings.jsonhooks and.mcp.jsonservers. --max-turns 3caps agentic turns (exits with an error at the limit);--max-budget-usdcaps estimated spend;--no-session-persistencekeeps the session off disk.- Exit code is 0 on success and non-zero on failure. SIGTERM exits with 143. A failure inside the run, such as missing authentication, is printed as the result on stdout.
Parse the output
--output-format jsonreturns the result, session ID and cost;jq -r '.result'extracts the text.- Add
--json-schema '<schema>'withjson; the answer is instructured_output, which is absent if the run ended without valid structured output, so check for it. An invalid schema printsError: --json-schema is not a valid JSON Schema. --output-format stream-jsonis newline-delimited events. The docs' streaming examples add--verbose(and--include-partial-messagesfor tokens):claude -p "Explain recursion" --output-format stream-json --verbose --include-partial-messages. The CLI reference writes--verboseinto its stream-json examples and lists it as required for--prompt-suggestions; the docs do not state it as required for every stream-json run, so add it by default and drop it only after testing your version.- The last line is a
resultmessage. With stream-json, denials appear aspermission_deniedsystem messages and inpermission_denialson the result: fail the job when that list is non-empty. - Fail CI on startup problems by checking
plugin_errorsandmcp_server_errorsin thesystem/initevent.
Evidence
Anthropic's docs are the primary source. An independent write-up by Aoba Works on dev.to (posted Oct 24, 2026) reaches the same advice: pin --allowedTools with the trailing-space pattern, check permission_denials, prefer dontAsk with a narrow allow list over bypass. It does not report a run you can reproduce. Evidence of a real CI hang: none found; the documented behavior is denial, not a wait.
Limits
Untested: none of these commands was run for this thread. Flags change between versions; check claude --version against the minimums above. dontAsk can still be approved by an installed mod, per the headless page.
Not for: what changed in Claude Code 2.1.302 and other upgrade-time breaking changes, see claude-code-2-1-302-headless-permission-defaults-upgrade; the GitHub Action setup is a separate topic.
Started from a post by @Anthropic, packaged by the editor. Original post