claude -p: what headless Claude Code actually loads (and when --bare is the right call)
You wire claude -p into a script, it works, and then the usage numbers make no sense: a one-line prompt cost as much as a whole working session. We hit this running Claude Code on a schedule — a cold claude -p in our repo root burned roughly 150,000 tokens before doing any work , because print mode loads everything an interactive session would. This post covers what -p actually loads, the --bare…
Claude -p is a command-line tool that allows non-interactive communication with the Claude Code model. When used in a script, it loads the entire context of the environment, including hooks, skills, plugins, MCP servers, auto memory, and CLAUDE.md files. This can result in high token usage, as demonstrated in a case where a one-line prompt cost 150,000 tokens.
To address this issue, the --bare flag was introduced. When used with -p, --bare skips the auto-discovery process, loading only the necessary components like the specified tools and settings. This approach provides reproducibility in CI environments, as it prevents unintended behavior caused by loaded hooks, skills, and other components.
To use --bare, set the ANTHROPIC_API_KEY or use --settings to provide credentials. Claude -p supports various optional flags, such as --append-system-prompt, --settings, --mcp-config, --agents, and --plugin-dir, which allow customization of the model's behavior. When using --bare, some features like system prompt additions, settings, MCP servers, and custom agents are still available through specific flags.
Additionally, the --output-format flag controls the response shape, with JSON format providing detailed information, including total costs and model breakdowns. Permissions when running headlessly are managed through --allowedTools, which specifies the allowed tools for the command. For more granular control, you can pass permission modes like dontAsk or acceptEdits.
However, it's essential to note that background tasks may be terminated after the response is delivered, which can cause issues in CI environments.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
