Cap turns and spend in automation
cli flags & headless
// what it does
--max-turns limits how many agentic turns Claude takes before stopping, and --max-budget-usd sets a dollar ceiling on API calls in print mode — together they keep an unattended run from looping or overspending. Pair with a permission mode so the run doesn't stall on a prompt: dontAsk denies anything outside your allow rules, useful for locked-down CI.
// shell
$ claude -p "fix the failing tests" --max-turns 5 --max-budget-usd 1.00$ claude -p "apply lint fixes" --permission-mode dontAsk --max-turns 3// gotcha
--max-budget-usd only applies to print mode and stops the run mid-task when hit, so the work may be left half-done. Set it high enough to finish a realistic task, and read the JSON output's cost fields to calibrate.