skip to content

How do `--max-turns` and `--max-budget-usd` cap a `claude -p` run, and what does each count or leave out?

level: middleimportance: should knowfreq 12%

answer

  1. print mode only, both of them
  2. no turn limit by default
  3. subagents spend from the same budget
  4. resumed totals don't eat the cap
  5. estimates, not the invoice

basics

~20 s

--max-turns limits agentic turns and exits with an error when reached; --max-budget-usd stops the run once estimated spend, subagents included, hits the cap. Both are print-mode only; neither has a default, and resumed spend is not counted.

solid answer

~40 s

Both flags apply to print mode only. `--max-turns 20` limits the agentic turns; there is no limit by default, and reaching it makes the run exit with an error. `--max-budget-usd 5.00` stops the run once spend on API calls reaches five dollars, and subagent spend counts: at the cap, spawning another subagent fails with `Budget limit reached` and running background subagents are stopped. On a `--continue` or `--resume` run, totals restored from earlier runs don't count toward the cap, even though `total_cost_usd` in json output reports the whole conversation. The spend is a client-side estimate, and neither flag is a wall-clock limit, so the CI job still needs a timeout.

code

bash · 8 lines
bash
claude -p "Fix the failing lint rules listed in lint.log" \
  --permission-mode acceptEdits \
  --max-turns 25 \
  --max-budget-usd 3.00 \
  --output-format json > run.json
status=$?
jq '{cost: .total_cost_usd, session: .session_id}' run.json
exit $status

go deeper

for a junior

Recall that --max-turns limits agentic turns and --max-budget-usd limits estimated dollars, both for claude -p only, and that there is no turn limit unless you set one.

for a middle

Explain that subagent spend counts toward the budget, that resumed totals don't, and that reaching --max-turns makes the run exit with an error.

for a senior

Show how a multi-call script bounds total spend itself, reads total_cost_usd without double counting, and pairs the caps with a job timeout.

for a principal

Set cap policy for a fleet of unattended jobs: per-run limits from observed runs, aggregate spend alerts, and which failures should page someone.

## Why an unattended run needs caps An interactive session has a person who notices when Claude is going in circles. A `claude -p` job in a nightly pipeline has nobody, and by default it has **no turn limit**: if the task is vague, a tool keeps failing or the model keeps retrying, the run continues until it finishes on its own. The general theory of stop conditions and budgets for agent loops belongs to the agent-loop foundations; this answer is about the two Claude Code flags that impose them on a `-p` run. Both are **print-mode only**. | Flag | What it counts | When reached | Default | | --- | --- | --- | --- | | `--max-turns N` | Agentic turns in the run | Exits with an error | No limit | | `--max-budget-usd X` | Estimated dollars spent on API calls, subagents included | Stops the run | No cap | ## `--max-turns` `claude -p --max-turns 3 "query"` limits how many agentic turns the run may take. When the limit is reached, Claude Code **exits with an error**, so a script sees a failed run rather than a half-finished answer presented as complete. Choosing the number is a trade-off: - Too low, and legitimate work fails, such as a fix that needs several read-edit-test cycles. - Too high, and the cap only triggers after a run has already wasted most of its time. One edge: with `--input-format stream-json`, a message still queued when the limit ends a turn stays queued and starts a new turn with its own limit. A long-lived streaming process is therefore bounded per turn, not in total; the budget flag is the total bound. ## `--max-budget-usd` `claude -p --max-budget-usd 5.00 "query"` stops the run once spend on API calls reaches the cap. Three details decide whether it actually protects you: 1. **Subagents count.** Spend from subagents goes against the same cap. Once spend reaches it, spawning another subagent fails with `Budget limit reached`, and Claude Code stops background subagents that are still running. 2. **Resumed spend does not count.** When a run continues a conversation with `--continue` or `--resume`, the totals restored from earlier runs do not count toward the cap, so each invocation gets the full amount again. The `total_cost_usd` field in `--output-format json` output still reports the conversation's whole total, earlier runs included. A multi-step script that wants a ceiling for the whole job has to add up spend itself and stop calling. 3. **It is an estimate.** Claude Code computes cost on the client from a price table, including multipliers such as the data-residency premium, so the figure can differ from the bill. Set the cap with some headroom below what you would actually be unhappy to pay. ## Reading why a run stopped With `--output-format json`, the final result object carries the session ID, usage and `total_cost_usd` whichever way the run ended, so a script can log spend even for a capped run. The Agent SDK documents the same result message with a `subtype` that distinguishes a normal finish from hitting the turn limit or the budget (`error_max_turns`, `error_max_budget_usd`); a script can branch on that instead of parsing text. ## What the caps leave out - **Wall-clock time.** Neither flag measures elapsed time. A command that hangs or a slow API keeps the clock running, so the CI job needs its own timeout. - **Background work after the result.** If Claude starts a background subagent or workflow, `claude -p` stays open until it completes, by default ending the wait after 10 minutes of continuous idle waiting (`CLAUDE_CODE_PRINT_BG_WAIT_CEILING_MS` changes that). - **Permissions.** A cap limits how much the run does, not what it is allowed to do; that is the job of `--allowedTools` and `--permission-mode`. ## A sensible default for a nightly job - Set `--max-turns` from observed runs, comfortably above the longest successful one. - Set `--max-budget-usd` per invocation, and track the sum across invocations yourself. - Keep a job-level timeout, and alert on `total_cost_usd` trends rather than single values. Revisit both numbers when the prompt, the model or the repository changes, because a cap tuned for last month's runs can start cutting off legitimate work or stop protecting you at all.

  • A script makes five `claude -p --resume` calls, each with `--max-budget-usd 2`; can the job spend more than two dollars?
    Yes. Totals restored from earlier runs of the conversation don't count toward a resumed run's cap, so each call may spend up to two dollars again. To bound the whole job, sum the spend yourself; `total_cost_usd` on each resumed call already reports the conversation total, so read the latest one rather than adding them.
  • Why is `--max-turns` not enough on its own for a job with a strict time budget?
    It counts agentic turns, not elapsed time. A single turn can be slow, a command can hang until its timeout, and background subagents can keep the process open after the result. Pair the turn cap with a CI-level timeout.

  • --max-turns has a small safe default, so leaving it off is fine
  • --max-budget-usd matches the invoice to the cent
  • Subagent spend sits outside --max-budget-usd
  • Resuming a session spends from what is left of the previous run's cap
  • A turn cap also bounds wall-clock time, so no job timeout is needed