Running Claude Code on cron: 4 ways it bills or breaks, and the guard for each A developer published agent-autopilot-kit, an MIT-licensed toolkit for running Claude Code headlessly from cron, documenting four failure modes and a guard for each: unsetting ANTHROPIC_API_KEY so jobs draw from a Pro/Max plan instead of per-token API billing, a non-blocking flock to stop overlapping sessions, a pause-until timestamp that skips retries after usage-limit failures, and run caps, --max-turns, hard timeouts and a kill file to bound runaway jobs. The kit ships ops/agent_tick.sh plus a selftest.sh that validates the launcher against a stub claude binary in 12 checks with zero usage. Running claude -p "..." from cron looks like a one-liner. In practice, four things go wrong. This guide explains each one and the guard that fixes it. Every guard is implemented in ops/agent tick.sh https://github.com/spikeyjr/agent-autopilot-kit/blob/main/ops/agent tick.sh in agent-autopilot-kit https://github.com/spikeyjr/agent-autopilot-kit , and tested by ops/selftest.sh against a stub claude . Unofficial. Not affiliated with Anthropic. Billing rules change: check the linked help-center pages. Signed in with a Pro/Max plan, claude -p draws from your subscription limits support.claude.com/en/articles/15036540 https://support.claude.com/en/articles/15036540-use-the-claude-plan-usage-limits . But if ANTHROPIC API KEY is present in the environment cron gives the job crontab variables, /etc/environment , a sourced profile , Claude Code may use the key, which is billed per token. Guard: unset it in the launcher, every run: unset ANTHROPIC API KEY ANTHROPIC AUTH TOKEN Also check apiKeyHelper in your settings and any cloud-provider env vars, and verify with claude auth status . A session that runs long collides with the next cron slot: two agents editing the same files. Guard: a non-blocking lock. The second run exits immediately instead of queueing. exec 9 state/agent.lock flock -n 9 || { echo "another session running"; exit 0; } On a plan, scheduled runs share the rolling usage window with your interactive use. If a run fails on a limit and the next slot simply tries again, you spend the fresh window on the same failure. Guard: on a failed run, look for limit wording usage limit , rate limit , limit reached , 429 , overloaded , write a pause-until timestamp, and skip runs until then. Never retry automatically. Only check failed runs: successful output can legitimately mention "rate limit" or contain 429 in an id. echo $ $ date +%s + 18000 state/pause until 5 hours Guards: a daily run cap, --max-turns , a hard timeout 45m , an end date checked before every run, and a kill file touch state/STOP . Most "agent" chores are deterministic: syncing data, rotating logs, checking a status page. Run them from a plain cron tick here: ops/ops tick.py every 15 minutes, plus ops/hooks/ .py so they keep working even when you're out of usage, and reserve claude -p for work that needs judgment. Put a stub claude first on PATH that prints canned JSON success, limit message, error, slow run , then assert on the launcher's log. That is what ops/selftest.sh does: 12 checks, zero usage. git clone https://github.com/spikeyjr/agent-autopilot-kit ~/autopilot && cd ~/autopilot $EDITOR ops/config.env AGENTS.md TASKS.md ./install.sh Disclosure: I maintain agent-autopilot-kit https://github.com/spikeyjr/agent-autopilot-kit MIT . Everything above is in the free repo.