An unattended script that talks to an AI agent should have a leash. Two flags give it one.
In print mode, --max-turns limits the number of agentic turns, and --max-budget-usd stops the run once the estimated spend on API calls reaches an amount. There is no limit by default.
claude -p --max-turns 3 --max-budget-usd 2 "Fix the failing test in auth.py" --allowedTools "Read,Edit,Bash"
Example: put that in a nightly job and a confused run ends after three turns or two dollars, whichever comes first. Spend from subagents counts toward the cap.
A cap is cheap insurance, and I would add one to any scheduled job before it runs for the first time. Pair it with a narrow --allowedTools list and a runaway loop has both a short rope and a small toolbox. Pick a low number first and raise it only when a real run hits the ceiling.
The caveat is in the word estimated. Claude Code checks the cap against its own client-side cost estimate, which can differ from your bill, and spend can pass the cap. The docs tell you to leave headroom. Both flags only work with -p; in an interactive session they do nothing for you.
Further reading: CLI reference – Claude Code docs, Run Claude Code programmatically – Claude Code docs, On this site: –json-schema makes claude -p return JSON you can trust, On this site: scope –allowedTools to single git commands, with a space before the star