Headless Mode, Output Formats and CI
`claude -p` runs the full Claude Code agent loop without the terminal UI, which makes it the building block for scripts, CI jobs and other-language integrations. Know the three output formats, how sessions resume across runs, how permissions behave when nobody can answer a prompt, how to relay questions to a person, and how the GitHub and GitLab integrations authenticate and stay bounded.
Key points
- 1
claude -p "prompt"(or--print) runs non-interactively, reads piped stdin (capped at 10 MB) and exits 0 on success or non-zero on failure. Flags such as--output-format,--json-schema,--max-turns,--max-budget-usdand--no-session-persistencework only with-p. - 2
--output-format textis the default.jsonprints one object withresult,session_id, usage andtotal_cost_usd.stream-jsonprints newline-delimited events, ending with aresultline. For token deltas add--verbose --include-partial-messagesand filterstream_eventitems whosedelta.typeistext_delta. - 3
--output-format json --json-schema '<schema>'puts the validated object instructured_output. An invalid schema stops the run at startup,formatis only an annotation, and the field is absent when the run ends without valid output. - 4
--continueresumes the most recent conversation in the directory;--resume <id>resumes a specific one and also accepts the absolute path of a.jsonltranscript. Interactiveclaude --continueskips-pand SDK sessions, butclaude -p --continueincludes them. Sessions are local files, so a fresh CI runner cannot resume another runner's session by ID. - 5
Since v2.1.302, a
-prun with no permission mode set starts in Manual (default). Calls that need approval are denied unless an allow rule, hook,canUseToolcallback or--permission-prompt-toolapproves them. A-p --resumerun also starts in that default mode rather than the mode the session used before. - 6
--permission-prompts none(v2.1.259+) denies whatever nothing else resolves, tells Claude not to retry, removesAskUserQuestionand reports denials aspermission_deniedmessages and inpermission_denials. Without it, a run with a permission host waits for that host. - 7
To relay questions and approvals to a person: an SDK app uses
canUseTool, andAskUserQuestionanswers go back asupdatedInputholdingquestionsplusanswers(question text mapped to label). Plainclaude -puses an MCP tool named in--permission-prompt-tool. When answers can take longer than the process lives, aPreToolUsehook defers the call and the session resumes later. Stdinstream-jsonuser messages ({"type":"user","message":{...}}) steer the conversation; they do not answer a pending tool call. - 8
--bareskips hooks, skills, plugins, MCP servers, auto memory and CLAUDE.md discovery, starts no background tasks and drops system reminders. Auth is onlyANTHROPIC_API_KEYor anapiKeyHelperfrom--settings, never OAuth. Context goes in with--append-system-prompt(-file),--settings,--mcp-config,--agentsand--plugin-dir. It is recommended for scripts and planned as the future-pdefault. - 9
--dangerously-skip-permissionsequalsbypassPermissions. Deny rules still block, it refuses root on Linux and macOS outside a recognized sandbox, and it offers no protection against prompt injection, so use it only in isolated containers without internet. For CI, prefer--permission-mode dontAskwith an exact--allowedToolslist, orauto. - 10
Caps:
--max-turns(no default limit; exits with an error) and--max-budget-usd(client-side estimate that includes subagents; can overshoot by one response; stops background work when reached; restored totals from earlier runs do not count). A finished run can stay open while background commands, subagents or Monitor watches it started are still running. - 11
GitHub Actions (
anthropics/claude-code-action@v1): apromptinput means automation mode and no prompt means@claudeinteractive mode. CLI flags go inclaude_args, auth inanthropic_api_keyorclaude_code_oauth_token(fromclaude setup-token), or OIDC federation withid-token: write. The action checks for write access and rejects bot actors unless they are listed inallowed_bots. - 12
GitLab CI/CD is a beta integration maintained by GitLab: a
.gitlab-ci.ymljob installs the CLI on your runner and callsclaude -pwithANTHROPIC_API_KEYas a masked variable, or uses OIDC for Bedrock and Agent Platform. From a language without an SDK, such as Go or Java, run the CLI as a subprocess with-p --output-format json.
Common traps
Assuming
-pauto-approves tools: it starts in Manual mode, and anything not pre-approved is denied rather than prompted.Relying on project
.claude/settings.jsonallow rules in CI: in a folder that was never trusted,-pdoes not use them, though it still runs the repository's hooks andenvblock. Pass--allowedToolsexplicitly.Using
CLAUDE_CODE_OAUTH_TOKENwith--bare: bare mode never reads OAuth credentials. Use an API key orapiKeyHelper.Expecting an allow rule to let
AskUserQuestionthrough indontAskmode: it is always denied there, and--permission-prompts noneremoves it entirely.Writing a person's answer to stdin as a chat message and expecting it to answer a pending
AskUserQuestioncall: answers travel through the permission host.Treating SIGTERM as a clean stop: it exits 143 with the turn unfinished. Send SIGINT or call
interrupt()first.Passing
github_token: ${{ secrets.GITHUB_TOKEN }}to the action: commits made with that token do not trigger CI workflows.Removing
--allowedToolsfromclaude_argsin the review workflow because the skill's frontmatter names the tool: the action starts the inline-comment MCP server only whenclaude_argsnames it.
Read the source
Further reading
Suggest a resourceNo extra reading for Headless Mode, Output Formats and CI yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on Headless Mode, Output Formats and CI
Ten questions, with the answer and explanation after each one.