---
title: "Claude Code headless mode — claude -p for a team · Selan"
description: "How to run Claude Code headless with claude -p: output formats, permissions, --bare, which credential a job should use, and how to cap what a run spends."
url: https://selan.ai/guides/claude-code-headless
updated: 2026-11-27T10:00:38+02:00
---

Guide · Claude Code for a team

# How do you run Claude Code headless for a team?

Claude Code's headless mode is `claude -p`: it takes one prompt, runs the agent with no interactive screen, prints the result and exits. Anthropic now calls it non-interactive mode. A run signed in with a Claude plan draws that person's subscription limits. A job the team shares needs a credential of its own and a cap on what it spends.

## What does claude -p do?

It runs the same agent loop, tools and context handling as an interactive session, then exits. Add `-p`, or `--print`, to any `claude` command.

```bash
claude -p "Find and fix the bug in auth.py" --allowedTools "Read,Edit,Bash"
git diff main | claude -p "list every typo in this diff" > typos.txt
claude -p "Summarize this project" --output-format json | jq -r '.result'
```

-   **Exit codes:** 0 on success and non-zero when the run fails, so a script can branch on it.
-   **Output:** `text` by default, `json` with the result, session ID and metadata, or `stream-json`, one event per line.
-   **A schema:** `--json-schema` with `json` output puts the answer in a `structured_output` field. A run that ends without valid output has no such field, so check for it.
-   **Piped input** is capped at 10MB. For more, write a file and name it in the prompt.
-   **Follow-ups:** `--continue` resumes the latest conversation, and `--resume` takes the `session_id` from the JSON.

## What can a headless run do without asking?

Only what needs no approval, unless you allow more. From Claude Code v2.1.302, a `claude -p` run starts in Manual mode when nothing sets a permission mode, and a call that needs approval is denied.

-   **`--allowedTools`** approves named tools, in permission rule syntax: `Bash(git diff *)` matches `git diff` with or without arguments.
-   **`--permission-mode`** sets a baseline: `acceptEdits` writes files, `dontAsk` denies everything that would prompt, and `auto` has a classifier review most actions.
-   **`--permission-prompts none`** (v2.1.259 and later) denies anything still waiting on an answer, so an unattended run never waits.

**A headless run trusts the folder it starts in.** Without `--bare`, it runs the hooks in the project's `.claude/settings.json` and connects the servers in its `.mcp.json`, even in a folder you have never trusted, with no trust dialog. In CI, that is whatever the runner checked out.

Turning every prompt off is a bigger step, and [skipping permissions safely](https://selan.ai/guides/claude-code-skip-permissions) covers where it is safe to run.

## What does --bare change?

It starts faster by skipping auto-discovery: hooks, skills, custom commands, subagents, [plugins](https://selan.ai/guides/claude-code-plugin-marketplace), MCP servers, auto memory and `CLAUDE.md`. Settings files and their permission rules are still read. You pass what the run needs with flags such as `--settings`, `--mcp-config` and `--append-system-prompt`.

Bare mode never reads a subscription sign-in. It skips OAuth credentials, the keychain and `CLAUDE_CODE_OAUTH_TOKEN`, so it needs `ANTHROPIC_API_KEY` or an `apiKeyHelper`. Bedrock, Google Cloud's Agent Platform and Foundry read their own credentials as usual.

**Good to know:** Anthropic recommends `--bare` for scripted calls and says it will become the default for `-p` in a future release. A script that relies on a subscription sign-in is worth testing with `--bare` before that happens.

## Which credential should a headless job use?

For a job the team shares, one that belongs to the job, not to a person. Claude Code picks the first credential it finds, in this order:

1.  A cloud provider, when `CLAUDE_CODE_USE_BEDROCK`, `CLAUDE_CODE_USE_VERTEX` or `CLAUDE_CODE_USE_FOUNDRY` is set.
2.  `ANTHROPIC_AUTH_TOKEN`, sent as a bearer token to a gateway.
3.  `ANTHROPIC_API_KEY`. In a `-p` run, the key is always used when present.
4.  An `apiKeyHelper` script.
5.  `CLAUDE_CODE_OAUTH_TOKEN`, from `claude setup-token`.
6.  Anthropic profile and federation credentials.
7.  The subscription sign-in from `/login`.

`claude setup-token` makes a one-year token on a Pro, Max, Ultra, Team or Enterprise plan. It can only make model requests. It authenticates with the subscription of the person who made it, and signed in with a plan, `claude -p` still draws from that plan's subscription limits.

On Max, Ultra and Team plans, monthly API credits cover `claude -p` run with an API key from the linked Claude Console organization.

A token from claude setup-token is one person's, and so is every limit it draws.

## How do you cap what a headless run spends?

-   **`--max-turns`** limits the agentic turns. There is no limit by default.
-   **`--max-budget-usd`** stops the run once its estimated spend reaches the amount. Subagents count toward it. It checks a client-side estimate, and spend can pass the cap, so leave headroom.
-   **`total_cost_usd`** in the JSON output is the same kind of estimate, and can differ from the bill.
-   **Background work:** a run waits for its background subagents and workflows, and stops them after 10 minutes of idle waiting by default (`CLAUDE_CODE_PRINT_BG_WAIT_CEILING_MS`).

Every one of these bounds a single run. None adds up the runs, so the hundredth run of a nightly job is capped exactly as the first was, and nothing stops the hundred and first.

## How does Selan run Claude Code headless?

Selan is a hosted gateway that runs Claude Code and Codex on the accounts your company already pays for. `selan claude` passes every argument through, so `-p`, `--output-format` and the permission flags work as they do on `claude`. An unattended job signs in with an API token an owner mints for it:

```bash
export SELAN_TOKEN=selanct_...
selan claude -p "review this diff" --output-format json
```

-   **No sign-in and nothing on disk.** With the token set there is no browser and nothing written, so a container image holds no credential.
-   **The job spends as itself**, under an address like `ci-nightly@tokens.selan.ai`, so Usage and Logs show the job, not whoever set it up.
-   **A limit across every run.** The company's per-person spend limit applies to a token as it does to a person, and a token can have a limit of its own. [Spend limits](https://selan.ai/solutions/spend) covers how they bind.
-   **API keys only, if you want.** An owner can switch a token to API only, so Selan serves it from the company's own API keys and skips every connected subscription account.
-   **A pinned version.** `selan claude` checks for a new release once a day and installs it; set `SELAN_NO_AUTO_UPDATE` on a build machine to pin the version.

`selan whoami` names the token in use and prints only its last four characters, so it is safe in a CI log. The setup is in [running unattended](https://selan.ai/docs#ci), and [Agents in CI](https://selan.ai/docs/agents) runs an agent from `.claude/agents/` the same way.

**`seland claude -p` skips every permission prompt.** Piped or in CI it starts at once, with no confirmation. What the job can reach is the boundary; the details are in [running without prompts](https://selan.ai/docs#seland).

[Start for free](https://auth.selan.ai/r)

Free for up to three seats, and the Free plan needs no card. A CI token takes no seat: Free holds up to 10, and every paid plan any number.

## When is plain claude -p the better answer?

For one developer scripting on their own machine and their own plan, plain `claude -p` is all it takes. One job with one Console API key in one secret is simple and enough too. To build Claude Code into your own program, the Agent SDK's Python and TypeScript packages give you callbacks and message objects; [the Agent SDK page](https://selan.ai/docs/claude-agent-sdk) points them at company accounts.

A gateway earns its place once several jobs share the bill and each should show and be capped on its own. [Claude Code in GitHub Actions](https://selan.ai/guides/claude-code-github-actions) and [Claude Code on a schedule](https://selan.ai/guides/claude-code-scheduled-tasks) cover the two places headless runs usually live.

### The short version

-   `claude -p` runs one prompt with no interactive screen, prints the result and exits with a status a script can read.
-   From v2.1.302 a headless run starts in Manual mode, so a call that needs approval is denied unless a flag or rule allows it.
-   `--bare` skips hooks, MCP servers and `CLAUDE.md`, and reads no subscription sign-in.
-   Signed in with a plan, `claude -p` draws that person's subscription limits.
-   `--max-turns` and `--max-budget-usd` cap one run; through Selan, a job's own token has a limit across all its runs.

How `claude -p`, its flags, bare mode and credentials work is as Anthropic's pages on running Claude Code programmatically, the CLI reference and authentication (code.claude.com/docs/en/headless, /cli-reference and /authentication) stated it on 25 November 2026. How a plan's limits and API credits cover `claude -p` is as Anthropic's help centre article "Use the Claude Agent SDK with your Claude plan" stated it on that date. These change: check the date before relying on them. What Selan does is as its command line, gateway and docs stated it then.
