Skip to main content

Headless Mode & the Agent SDK

Advanced

Claude Code isn't only interactive. You can run it headless (non-interactive, scriptable) and you can build your own agents on the same underlying harness with the Agent SDK.

What you'll learn
  • How to run a single prompt non-interactively and capture its output
  • Why an unattended run needs a deliberate permission posture, not the interactive default
  • When you have outgrown a one-shot call and should reach for the Agent SDK instead
  • The three modes side by side, so you pick deliberately rather than by habit

Headless mode​

Run a single prompt non-interactively and capture the output — perfect for scripts, pre-commit hooks, and CI:

Review the staged diff from a script

claude -p "Review the staged diff and list any bugs as a Markdown checklist"

Pipe input in, get a result out. Combine with permissions set to a safe, non-interactive posture so it never hangs waiting for approval — and lock it down so an automated run can't touch secrets (see Hardening Autonomous Runs).

Guided walkthrough1 of 5
  1. One prompt, one answer, exit. Everything a headless run needs is a flag, because there is no UI to ask you anything.

The exact flags for each step — and the traps in them — are in The claude CLI: Flags That Change Outcomes. A classic use of all of it together: a CI job that has Claude review every pull request — see the PR-review walkthrough.

The Agent SDK​

The Claude Agent SDK (Python and TypeScript) lets you build production agents on the same loop that powers Claude Code — tool use, file/shell access, permissions, context management — but wired into your application.

Reach for it when you've outgrown a single API call or a hand-rolled loop and want a batteries-included agent runtime. For the spectrum of options — single call → workflow → custom agent → managed — see Building Agents on the API.

Headless/SDK vs interactive​

ModeFor
Interactive Claude CodeDay-to-day dev with a human in the loop
Headless (claude -p)Scripts, pre-commit, CI one-offs
Agent SDKProduction agents embedded in your software

Check yourself​

Check yourself

0/2
  1. What is the defining difference between a headless run and an interactive session?
  2. You have a one-shot `claude -p` call in CI, and now you want the same logic embedded in your own product with custom tools and your own permission handling. What do you reach for?
Key takeaways
  • Headless is the same harness with the human removed — so everything the human would have decided becomes configuration.
  • Narrow the permissions, bound the turns and spend, and ask for structured output. All four are flags, not conventions.
  • Graduate to the Agent SDK when you want the loop inside your own application rather than a CLI call from a script.
  • An unattended run is a security posture as much as a feature: harden what it can reach before you schedule it.

Next​