Off with its head: running Claude headless instead of calling the API

If you pay for Claude Pro or Max and want a script on your own computer to ask Claude things, you do not need an API key. You can run Claude Code in headless mode, claude -p, and use the sign-in you already have. This post shows how that works, how it differs from a real API call, and when you should use the API anyway.
Despite the name, this is not another Halloween print.
Two ways to get an answer out of Claude
The API. You make an account in the Claude Console, get an API key (a long secret password for programs), and pay for every token. A token is a chunk of text, about three quarters of a word. Your code sends a message and gets text back. That is all you get. If you want Claude to read a file or run a command, your code has to do that part and send the result back to it.
Headless mode. This is the same Claude Code you chat with in a terminal, with no chat window. You hand it a prompt, it prints an answer, and it exits. It uses your normal Claude sign-in, so the usage counts against your plan's limits, the same pool as your chats. No key, no separate bill.
Here is the API version in Python:
import anthropic
client = anthropic.Anthropic()
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user",
"content": "Summarize: ..."}],
)
print(msg.content[0].text)
anthropic.Anthropic() finds your key in the ANTHROPIC_API_KEY setting. And here is the headless version:
import json, subprocess
cmd = ["claude", "-p",
"--output-format", "json",
"--tools", ""]
proc = subprocess.run(
cmd, input="Summarize: ...",
capture_output=True, text=True)
print(json.loads(proc.stdout)["result"])
It starts the claude program, feeds it the prompt, and reads back a small JSON answer. --tools "" turns off every tool, so it can only answer, not touch your files.
How I use it
My work dashboard has Claude write a short summary on each card. From day one I told Claude I wanted that on "not an anthropic api call, just my local claude account (max subscription)." So the dashboard runs claude -p in the background.
One trap: if an API key is set on your computer, headless mode uses the key, not your sign-in. So the dashboard removes any API key settings before it starts Claude. A stray key cannot send me a bill.
It also keeps Claude polite. One call at a time, at most 30 calls an hour, and no new summary until a card stops changing. Your plan has limits, and a script in a loop can eat them fast.
My other example is a tool that reads my finished Claude sessions and pulls out a few short facts. Its call looks like this:
[exe, "-p", "--safe-mode",
"--no-session-persistence",
"--tools", "",
"--model", model,
"--output-format", "json",
"--system-prompt", SYSTEM_PROMPT,
"--json-schema", json.dumps(SCHEMA)]
What each piece does:
--safe-modeskips all my own setup: my rule files, skills, hooks, and plug-ins. My sign-in still works.--no-session-persistencedoes not save the run, so it does not clutter my session history.--system-promptreplaces Claude Code's own instructions with mine.--json-schemamakes Claude fill in a form I define. The answer comes back in astructured_outputfield, so my code does not have to pick facts out of prose.
--safe-mode mattered more than I expected. My setup loads a lot of rules, and every headless call loaded them too. Adding it cut each call from about 37,000 tokens of overhead to about 1,200.
There is also a --bare mode that strips even more. I tried it first, but it will not use a subscription sign-in. It wants an API key.
When to use the API instead
Headless mode is for your own stuff, on your own computer. Use the API when:
- Other people use the thing you are building. Anthropic is clear here: products for other people use API keys, not your claude.ai sign-in. Your plan is for you.
- It runs on a server. A server has nobody signed in to Claude Code, and an API key is the normal way in.
- You want a separate bill. Per-token billing shows exactly what a project costs. Headless calls share a pool with your own chats, so a busy script can leave you rate limited in the middle of your workday.
The docs, if you want more: headless mode, every CLI flag, and the API.
Drafted with an assistant, edited by @wintaru
0 comments