Claude Code

Claude Code headless mode: claude -p, --output-format json, --bare and CI flags

How do I run Claude Code non-interactively with claude -p in scripts and CI?

Add -p (or --print) to any claude command to run it once without the interactive interface, for example claude -p "What does the auth module do?". Use --output-format json or stream-json for machine-readable output, --allowedTools or --permission-mode to avoid prompts, and --bare in CI so the run skips local hooks, plugins, MCP servers, auto memory and CLAUDE.md.

Basic usage

The -p flag runs Claude Code in non-interactive mode. It reads stdin, so you can pipe data in and redirect the answer out. Claude Code exits with code 0 on success and a non-zero code when the run fails.

Ask a question

claude -p "What does the auth module do?"

Pipe a log in and write the answer to a file

cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt

Piped stdin is capped at 10MB. For bigger inputs, write the content to a file and reference the path in your prompt.

Start faster and reproducibly with --bare

--bare skips auto-discovery of hooks, skills, custom commands, subagents, installed plugins, MCP servers, auto memory and CLAUDE.md. The docs recommend it for scripted and SDK calls and say it will become the default for -p in a future release.

Bare mode never reads OAuth credentials or the system keychain. Set ANTHROPIC_API_KEY first, or supply an apiKeyHelper in the --settings JSON.

Bare mode run

claude --bare -p "Summarize README.md" --allowedTools "Read"
  • --append-system-prompt or --append-system-prompt-file: add to the system prompt
  • --settings <file-or-json>: load settings
  • --mcp-config <file-or-json>: load MCP servers
  • --agents <file-or-json>: load custom agents
  • --plugin-dir <path>: load a plugin

Get structured output

  • text (default): plain text
  • json: result, session ID and metadata, including total_cost_usd
  • stream-json: newline-delimited JSON events

JSON output, extract the text with jq

claude -p "Summarize this project" --output-format json | jq -r '.result'

Force a JSON Schema (result lands in structured_output)

claude -p "Extract the main function names from auth.py" \
  --output-format json \
  --json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}' \
  | jq '.structured_output'

Stream tokens as they arrive

claude -p "Explain recursion" --output-format stream-json --verbose --include-partial-messages

Approve tools without prompts

--allowedTools uses permission rule syntax. A trailing space and * means prefix match, so Bash(git diff *) allows any command starting with git diff.

Run tests and fix failures

claude -p "Run the test suite and fix any failures" \
  --allowedTools "Bash,Read,Edit"

Commit with scoped git permissions

claude -p "Look at my staged changes and create an appropriate commit" \
  --allowedTools "Bash(git diff *),Bash(git log *),Bash(git status *),Bash(git commit *)"

Or set a baseline with --permission-mode: auto (a classifier reviews most actions), dontAsk (deny anything that would prompt, good for locked-down CI) or acceptEdits (write files without prompting). Add --permission-prompts none for unattended jobs (v2.1.259 or later).

Unattended auto mode run

claude -p "Update the dependency pins and run the tests" --permission-mode auto --permission-prompts none

Continue a conversation

Continue the most recent conversation

claude -p "Review this codebase for performance issues"
claude -p "Now focus on the database queries" --continue

Resume a specific session by ID

session_id=$(claude -p "Start a review" --output-format json | jq -r '.session_id')
claude -p "Continue that review" --resume "$session_id"

Use it in a build script

package.json typo linter

{
  "scripts": {
    "lint:claude": "git diff main | claude -p \"you are a typo linter. for each typo in this diff, report filename:line on one line and the issue on the next. return nothing else.\""
  }
}

Run it with npm run lint:claude. For full programmatic control in Python or TypeScript, use the Agent SDK packages.

Sources

More on Claude Code

Other guides

Cursor: allow all terminal commandsClaude Code: allow commands without promptsCodex CLI: approval and sandbox modesGemini CLI: YOLO mode, auto_edit and allowing specific shell commandsCursor Privacy Mode: telemetry, training and data retentionClaude Code telemetry and data retention: what is sent and how to turn it offGemini CLI: turn off usage statistics and telemetryCLAUDE.md: where it goes, how it loads, and how it works with AGENTS.mdClaude Code hooks: format on save, block risky edits, get notifiedClaude Code subagents: create one, limit its tools, and call itAdd MCP servers to Cursor with mcp.json: Keep files out of Cursor with .cursorignore: Configure Codex with config.toml: Configure Gemini CLI with settings.json: Add MCP servers to Claude Code (claude mcp add, .mcp.json, scopes): Claude Code custom slash commands and skills (SKILL.md): GitHub Copilot custom instructions: Adding MCP servers to GitHub Copilot: Claude Code settings.json: file locations, precedence, and key settingsClaude Code plugins and marketplaces: install, create, and shareGemini CLI extensions and custom commands: Configuring OpenCode with opencode.json: Cline Rules: workspace, global, and conditional rulesKiro steering files: .kiro/steering, inclusion modes, and AGENTS.mdZed agent: instruction files, MCP servers, tool permissions, and ACP agentsCursor CLI: install, headless mode, permissions and CIAider: CONVENTIONS.md, .aider.conf.yml and API keysJunie guidelines, MCP and the Action Allowlist: Amp AGENTS.md, settings.json and MCP: Configure Goose extensions, hints, recipes and permissions: Customize OpenHands with skills, AGENTS.md, setup.sh and MCP: Configure Factory Droid CLI: AGENTS.md, settings.json, autonomy, custom droids and MCPConfigure Qwen Code: settings.json, providers, QWEN.md, MCP and approval modesCursor hooks: hooks.json, beforeShellExecution, afterFileEdit and exit codesCursor sandbox: sandbox.json, network allowlist and run modesCursor settings: settings.json, Cursor Settings and the ~/.cursor config filesCursor browser agent: @browser, browser automation and approval modesCursor subagents: .cursor/agents, frontmatter, built-in Explore, Bash and BrowserCursor telemetry and OpenTelemetry export (Privacy Mode, OTLP, Analytics API): Cursor rules setup: .cursor/rules, .mdc frontmatter, globs, alwaysApply and AGENTS.mdClaude Code statusline: custom status bar in settings.json with context %, cost and git branchClaude Code /model: switch models, aliasesClaude Code /cost and /usage: check token spend, usage limits and reduce costsClaude Code VS Code extension: install, shortcuts, settings and /ideCodex AGENTS.md: global, project and override instruction files explainedCodex MCP servers: codex mcp add and [mcp_servers] in config.tomlCodex sandbox modes: read-only, workspace-write, danger-full-access and network accesscodex exec: run Codex non-interactively in scripts and CI with JSON outputCodex skills: create SKILL.md, skill folder locations and $skill invocationGEMINI.md in Gemini CLI: context file locations, imports and /memoryGemini CLI MCP servers: gemini mcp add and mcpServers in settings.jsonGemini CLI custom slash commands: TOML files, {{args}}, !{shell} and @{file}Gemini CLI headless mode: gemini -p, JSON output and exit codes for scripts and CIGemini CLI sandbox: enable Docker, Podman, macOS Seatbelt or gVisorcopilot-setup-steps.yml: set up the GitHub Copilot cloudGitHub Copilot CLI permissions: --allow-tool, --deny-tool, --allow-all-tools and trusted directoriesGitHub Copilot custom agents: .github/agents/*.agent.md profiles, tools and frontmatterGitHub Copilot code review settings: automatic PR reviews, rulesets, effort level and approvals

Get the weekly agent stack update

New official MCP servers, spec changes and harness releases, checked against the source. One email a week, no fluff.

Reviewed Oct 13, 2026. Settings change often; the linked vendor docs are the source of truth.