cd /news/ai-agents/running-claude-code-on-cron-4-ways-i… · home › topics › ai-agents › article
[ARTICLE · art-176406] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

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.

recommendation

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.

more in playbook: Running AI in production
by read2 min views1 publishedNov 25, 2026

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 in 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).

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 -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/_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 (MIT). Everything above is in the free repo.

── more in #ai-agents 4 stories · sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/running-claude-code-…] indexed:0 read:2min 2026-11-25 · —