{"slug": "running-claude-code-on-cron-4-ways-it-bills-or-breaks-and-the-guard-for-each", "title": "Running Claude Code on cron: 4 ways it bills or breaks, and the guard for each", "summary": "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.", "body_md": "Running `claude -p \"...\"` from cron looks like a one-liner. In practice, four things go wrong.\n\nThis guide explains each one and the guard that fixes it. Every guard is implemented in\n\n[`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`.\n\n*Unofficial. Not affiliated with Anthropic. Billing rules change: check the linked help-center pages.*\n\nSigned in with a Pro/Max plan, `claude -p` draws from your subscription limits\n\n([support.claude.com/en/articles/15036540](https://support.claude.com/en/articles/15036540-use-the-claude-plan-usage-limits)).\n\nBut if `ANTHROPIC_API_KEY` is present in the environment cron gives the job (crontab variables,\n\n`/etc/environment`, a sourced profile), Claude Code may use the key, which is billed per token.\n\n**Guard:** unset it in the launcher, every run:\n\n```\nunset ANTHROPIC_API_KEY ANTHROPIC_AUTH_TOKEN\n```\n\nAlso check `apiKeyHelper` in your settings and any cloud-provider env vars, and verify with `claude auth status`.\n\nA session that runs long collides with the next cron slot: two agents editing the same files.\n\n**Guard:** a non-blocking lock. The second run exits immediately instead of queueing.\n\n```\nexec 9>state/agent.lock\nflock -n 9 || { echo \"another session running\"; exit 0; }\n```\n\nOn a plan, scheduled runs share the rolling usage window with your interactive use. If a run fails on a\n\nlimit and the next slot simply tries again, you spend the fresh window on the same failure.\n\n**Guard:** on a failed run, look for limit wording (`usage limit`, `rate limit`, `limit reached`, `429`,\n\n`overloaded`), write a pause-until timestamp, and skip runs until then. Never retry automatically.\n\nOnly check *failed* runs: successful output can legitimately mention \"rate limit\" or contain `429` in an id.\n\n```\necho $(( $(date +%s) + 18000 )) > state/pause_until    # 5 hours\n```\n\n**Guards:** a daily run cap, `--max-turns`, a hard `timeout 45m`, an end date checked before every run,\n\nand a kill file (`touch state/STOP`).\n\nMost \"agent\" chores are deterministic: syncing data, rotating logs, checking a status page. Run them from a\n\nplain cron tick (here: `ops/ops_tick.py` every 15 minutes, plus `ops/hooks/*.py`) so they keep working even\n\nwhen you're out of usage, and reserve `claude -p` for work that needs judgment.\n\nPut a stub `claude` first on `PATH` that prints canned JSON (success, limit message, error, slow run),\n\nthen assert on the launcher's log. That is what `ops/selftest.sh` does: 12 checks, zero usage.\n\n```\ngit clone https://github.com/spikeyjr/agent-autopilot-kit ~/autopilot && cd ~/autopilot\n$EDITOR ops/config.env AGENTS.md TASKS.md\n./install.sh\n```\n\n*Disclosure: I maintain [agent-autopilot-kit](https://github.com/spikeyjr/agent-autopilot-kit) (MIT). Everything above is in the free repo.*", "url": "https://wpnews.pro/news/running-claude-code-on-cron-4-ways-it-bills-or-breaks-and-the-guard-for-each", "canonical_source": "https://dev.to/spikeyjt/running-claude-code-on-cron-4-ways-it-bills-or-breaks-and-the-guard-for-each-4n65", "published_at": "2026-11-25 22:09:32+00:00", "updated_at": "2026-11-25 22:13:37.874753+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "mlops"], "entities": ["Claude Code", "Anthropic", "agent-autopilot-kit", "Claude Pro", "Claude Max"], "also_reported_by": [], "takeaway": "Unset ANTHROPIC_API_KEY in the cron launcher so claude -p bills against a Pro/Max plan rather than per-token API usage, and gate retries behind a pause-until timestamp written only when a failed run's output contains limit wording.", "recommendation": {"advice": "Unset ANTHROPIC_API_KEY in the cron launcher, wrap runs in flock -n, and on failed runs only, write a pause-until timestamp when output shows usage-limit wording instead of retrying automatically.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/running-claude-code-on-cron-4-ways-it-bills-or-breaks-and-the-guard-for-each", "markdown": "https://wpnews.pro/news/running-claude-code-on-cron-4-ways-it-bills-or-breaks-and-the-guard-for-each.md", "text": "https://wpnews.pro/news/running-claude-code-on-cron-4-ways-it-bills-or-breaks-and-the-guard-for-each.txt", "jsonld": "https://wpnews.pro/news/running-claude-code-on-cron-4-ways-it-bills-or-breaks-and-the-guard-for-each.jsonld"}}