Headless mode

Headless mode runs Claude Code non-interactively. No terminal UI, no approval prompts (either pre-authorized or blocked). The agent takes input, does the work, and emits output, ready to be wired into automation.

Why you'd want it

How to run Claude Code headless

The CLI supports a non-interactive mode, pass a prompt, get output, exit.

echo "Analyze yesterday's error logs and write a summary" | claude --headless --print

Options you'll want:

What changes in headless mode

Permission design for headless

In headless mode, the permission list has to be right. No user to rescue you.

Patterns

Task-runner pattern

Write a shell script or cron entry that pipes a task description into Claude Code:

#!/bin/bash
claude --headless --print --max-turns 50 << 'EOF'
Check yesterday's server logs for 5xx errors. Summarize into
a report. Post to Slack channel #alerts.
EOF

Webhook handler pattern

A web server receives a webhook, formulates a prompt, invokes Claude Code headless, returns the response.

CI/CD step pattern

In a GitHub Action: after the test suite runs, invoke Claude headless to analyze failures and open an issue.

Logging

Every headless run should log:

Use a PostToolUse hook to pipe tool calls to your log system.

Common pitfalls