Scheduled Autonomy: Loops, Wakeups & Cron
Master Claude Code's scheduled autonomy: /loop dynamic mode, ScheduleWakeup timing, prompt-cache economics, session cron tasks, and cloud scheduled tasks for standing automation.
What You’ll Learn
In Session 11 you saw agents that operate without a human in the loop. This session covers the missing dimension: time. An agent that watches a deploy or runs a nightly audit must decide not just what to do, but when to wake up and do it — and that decision has real economics attached.
By the end, you’ll understand:
- How
/loopworks: fixed intervals, self-paced “dynamic mode”, and the built-in maintenance prompt - The ScheduleWakeup tool: delay clamping, self-scheduling, and
stopsemantics - Prompt-cache economics — why the right sleep depends on whether your cache lives five minutes or one hour
- What to poll (external state) and what never to poll (harness-tracked work)
- Session-scoped cron tasks vs one-shot wakeups, and when to reach for cloud scheduled tasks or
/goalinstead - Why long foreground sleeps are blocked, and what to use instead (Monitor, background Bash)
The Problem
Say you push a commit and want Claude Code to merge the PR once CI goes green. CI takes about eight minutes. What does the agent do for those eight minutes?
The naive options are all bad:
Option 1: Busy-wait Option 2: sleep 480 Option 3: poll every 60 s
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ Check. Check. │ │ Block the whole │ │ 8 wake-ups, 8 │
│ Check. Check... │ │ session on a │ │ full turns, for │
│ Burns tokens │ │ foreground sleep │ │ one bit of info │
│ continuously │ │ (blocked anyway) │ │ ("done yet?") │
└──────────────────┘ └──────────────────┘ └──────────────────┘
There’s a subtler problem underneath: sleeping is not free either. Every wake-up re-reads your accumulated context. Wake within the prompt cache’s time to live (TTL) and that read comes from cache — cheap. Sleep past it and the entire context is reprocessed uncached, at full price, on every wake. How long the TTL is depends on how the session is billed, so the same sleep can be cheap in one session and a trap in another.
So scheduled autonomy is really two problems: a mechanism problem (how does an agent resume itself?) and an economics problem (which intervals are cheap, and which are traps?). Claude Code’s harness answers both.
How It Works
/loop and Dynamic Mode
/loop (alias /proactive) runs a prompt repeatedly while the session stays open. What you pass decides how it paces itself:
| You type | What happens |
|---|---|
/loop 5m check the deploy | Fixed interval: Claude converts it to a cron schedule |
/loop check the deploy | Dynamic mode: Claude picks the delay after each iteration |
/loop | The built-in maintenance prompt, or your .claude/loop.md, at a self-paced interval |
In dynamic mode — no fixed interval at all — the model paces itself using a tool called ScheduleWakeup: at the end of each iteration it schedules its own resumption, passing the same loop prompt back to itself, and prints the delay it chose and why.
Dynamic loop lifecycle:
┌────────────┐ ScheduleWakeup ┌────────────┐
│ Iteration N │ ──(delaySeconds)──▶ │ sleep │
└────────────┘ └─────┬──────┘
▲ │ harness re-invokes
│ same loop prompt │ with the loop prompt
└──────────────────────────────────┘
Exit: ScheduleWakeup with stop: true → loop ends
The interval is a decision the model makes each round, not a config value. A CI watch and a nightly tidy-up job need completely different pacing, and only the agent in the loop knows which situation it’s in. To stop a self-paced loop yourself, press Esc, which clears the pending wakeup.
ScheduleWakeup Semantics
Two fields matter most:
delaySeconds— clamped to the range [60, 3600]. Ask for 10 seconds, you get 60. Ask for two hours, you get 3600. The floor stops runaway tight-looping; the ceiling guarantees the loop checks in at least hourly.stop: true— ends the loop (2.1.202+). When the goal is reached (or is unreachable), the agent stops scheduling and the loop terminates cleanly.
Each scheduling call also carries the loop prompt to repeat, a one-line reason shown to you, and noop (true when nothing changed, so quiet ticks fold into one line). If an iteration ends without rescheduling or stopping, Claude Code schedules one fallback wakeup about 20 minutes later and ends the loop if that iteration doesn’t reschedule either. In simplified form:
ScheduleWakeup(delaySeconds: 270) # resume in 4.5 minutes
ScheduleWakeup(stop: true) # goal reached — end the loop
Know Your Cache TTL
Here is the fact that should drive every delay you choose: how long your prompt cache lives. Claude Code picks the TTL per request, and loop wakeups run in the main conversation:
| Session | Main-conversation cache TTL |
|---|---|
| Claude subscription, within the plan’s included usage | One hour |
| API key, cloud provider, or usage credits | Five minutes |
The promptCacheTtl setting (5m or 1h) overrides the default, and FORCE_PROMPT_CACHING_5M=1 forces five minutes. Subagents and workflow agents default to five minutes either way.
On a five-minute TTL, there is a cliff. The delay range splits into three regimes:
delaySeconds chosen
60 ──────── 270 ─ 300 ─────────── 1200 ──────── 1800 ────── 3600
│ WARM ZONE │ CLIFF │ DEAD ZONE │ COMMITTED SLEEP ZONE │
│ cache hit │ worst │ cache miss, │ cache miss, but one │
│ every wake │ of │ yet you're │ miss buys 20–60 min │
│ │ both │ still waking│ of silence — worth it │
│ │ │ frequently │ │
- Under ~270 s — you wake before the cache expires; every wake reads context warm. This is the regime for active polling, and ~270 (not 290) leaves margin under the cliff.
- ~300 s — the worst-of-both choice: just long enough to lose the cache, so every wake pays a full uncached read — while still waking every five minutes.
- 1200 s and up — committed sleep. The wake is a cache miss, but one miss buys 20–60 minutes of not running at all.
The anti-pattern is the dead zone between the cliff and a committed sleep: cold-read prices at warm-poll frequency.
On a one-hour TTL, the cliff disappears. Every delay the clamp allows wakes with the context still cached (2.1.304 fixed wakeups at the one-hour maximum that fired just after expiry). What you pay for is the number of wakes, so match the delay to how fast the thing you’re watching changes — and never add wakeups just to keep the cache warm.
In both regimes, 1200–1800 s is the sane default for idle ticks with nothing specific to watch. Claude Code’s built-in pacing guidance makes the same TTL distinction, so a well-behaved loop already knows which regime it’s in; your job is to recognize the choice and check the bill. This is Session 21’s cost-optimization thinking applied to the time axis, and /usage has a Loops breakdown that ranks your heaviest recent loops by tokens.
Poll External State — Never Harness-Tracked Work
The second discipline: know what deserves polling at all.
Work the harness itself tracks — background Bash commands, spawned agents, running workflows (the machinery from Session 8) — re-invokes the agent automatically when it finishes. Polling it is pure waste: the harness would have told you anyway. The only wakeup worth scheduling against harness-tracked work is a long fallback, a single distant check in case something hangs.
What deserves polling is external state: CI runs, deploy rollouts, remote queues — things that change outside the harness’s view. Match the delay to how fast that state changes: an eight-minute pipeline doesn’t need a 60-second poll; a queue that drains hourly doesn’t need a five-minute one.
Session Cron Tasks vs One-Shot Wakeups
ScheduleWakeup is a one-shot: it fires once, and the loop must re-schedule each round. A fixed-interval /loop, or a reminder you ask for in plain language (“in 45 minutes, check whether the integration tests passed”), uses the cron tools instead: CronCreate, CronList, and CronDelete manage prompts scheduled with a 5-field cron expression in your local time.
These are session-scoped, not standing infrastructure:
- they fire only while Claude Code is running and idle, between your turns, with no catch-up for missed fires;
- recurring tasks expire after seven days, and a session holds at most 50;
--resumeor--continuerestores unexpired cron tasks, but not a self-paced/loop;- recurring tasks start a little late on purpose (jitter), except every-five-minute ones, which start 15 seconds early so each run lands inside a five-minute cache.
CLAUDE_CODE_DISABLE_CRON=1 turns off the scheduler, the cron tools, and /loop.
When you run a bare /loop, the prompt it schedules is an internal sentinel — <<autonomous-loop-dynamic>> for self-paced wakeups, <<autonomous-loop>> for cron-based loops — that the runtime expands back into the autonomous-loop instructions each time it fires. With a prompt of your own, the same prompt is simply passed back. You may see the sentinels in transcripts; you never manage them yourself.
Standing Jobs and Goals
For a job that must outlive the session — a nightly report, a weekly dependency audit — use a durable scheduler instead:
| Option | Runs on | Machine must be on? | Minimum interval |
|---|---|---|---|
Cloud scheduled task (/schedule, alias /routines; formerly routines) | Anthropic-managed cloud, or your self-hosted environment | No | 1 hour |
| Desktop scheduled task | Your machine, while the Desktop app is open | Yes | 1 minute |
/loop and session cron tasks | Your machine, inside an open session | Yes | 1 minute |
Cloud scheduled tasks are a research preview on Pro, Max, Team, and Enterprise plans and need a claude.ai login. Each run is a full cloud session on a fresh clone, with the connectors you configure per task, and a task can also start from an API call or a GitHub event. GitHub Actions’ schedule trigger is another unattended option.
When the trigger is a condition rather than a clock — “keep going until every test in test/auth passes” — use /goal. It starts the next turn as soon as the previous one finishes, and an evaluator model checks the condition after each turn until it is met, judged impossible, or cleared with /goal clear.
Permissions in Unattended Runs
A scheduled prompt fires into a session as it is, so it inherits that session’s permission mode. Current Claude Code starts interactive sessions in auto mode when no mode is configured, while claude -p and Agent SDK runs with no mode set start in Manual (2.1.302+), where calls that need approval are denied unless allow rules, a hook, or --permission-mode auto cover them. A /goal doesn’t change the permission mode either. Cloud scheduled tasks have no permission mode to choose: they run autonomously, scoped by the repositories, environment, and connectors you give them. In every case, a scheduled fire is not a human: it can’t approve a pending action, and Claude Code asks before the first publish of an artifact in a turn that a scheduled task started.
No Foreground Sleeps
Two supporting rules complete the picture:
- Long foreground sleeps are blocked wherever the Monitor tool is available. You can’t park a turn on
sleep 480. To wait on a condition, Claude runs an until-loop such asuntil <check>; do sleep 2; doneunder the Monitor tool, which notifies it when the loop exits, instead of blocking the session. Each watch has a deadline — five minutes by default, at most 30 — after which Claude can start another. For a dynamic/loop, Claude may use Monitor directly instead of re-running the prompt. - Background Bash runs a command detached via
run_in_background(no&needed). It keeps running across turns and re-invokes the agent when it exits — which is exactly why you never poll it.
Hands-On Example
Let’s pace a real CI watch. You’ve pushed a commit; the pipeline historically takes about eight minutes. The loop’s job: wake up, check the PR’s checks, merge when green, and stop.
First, compare the three candidate schedules:
Pipeline duration: ~480 s (8 min)
Strategy A: 60 s polls W─W─W─W─W─W─W─W─✓ 8 wakes, all warm
8 turns for 1 bit of information
Strategy B: one 480 s sleep ────────────────✓ 1 wake: uncached on a 5-min TTL,
cached on a 1-hour TTL
Strategy C: two 270 s sleeps ───warm───┬──warm──✓ 2 wakes, cached on either TTL
(270 s) (540 s ≈ 9 min — CI done)
Which strategy wins depends on your TTL. On a five-minute TTL, Strategy C wins: each sleep stays under the ~300 s cliff, so both wakes read context warm, and two turns is a quarter of Strategy A’s overhead. On a one-hour TTL, Strategy B wins: every wake is cached anyway, so one check at ~480 s costs a single turn. Here is Strategy C, the loop for a five-minute TTL:
# Iteration 1 (t = 0): just pushed. CI will take ~8 min.
gh pr checks 142 # → pending
# Too early to poll tightly. Sleep just under the cache TTL.
# ScheduleWakeup(delaySeconds: 270)
# Iteration 2 (t = 270 s): warm wake — context read from cache.
gh pr checks 142 # → still running (expected; ~4.5 min elapsed)
# ScheduleWakeup(delaySeconds: 270)
# Iteration 3 (t = 540 s): warm wake again. ~9 min elapsed.
gh pr checks 142 # → all checks passed
gh pr merge 142 --squash
# Goal reached:
# ScheduleWakeup(stop: true)
On a one-hour TTL the same loop is shorter: one ScheduleWakeup(delaySeconds: 480), then check, and merge and stop if the checks are green.
Two judgment calls worth noticing:
- If the pipeline took 25 minutes instead, warm polling stops making sense. Commit to a single
delaySeconds: 1500sleep. On a five-minute TTL, one cache miss buys the whole wait; on a one-hour TTL it isn’t even a miss. - If the “pipeline” were a local test suite launched with background Bash, you wouldn’t schedule polls at all — the harness re-invokes you when the command exits. At most, set one distant fallback (
delaySeconds: 1800) in case it hangs.
And when the loop finally acts, the norms from Session 11 apply: reversible, in-scope actions (merging the PR you were asked to merge) proceed without asking; destructive or scope-changing decisions are where an autonomous loop stops and reports instead.
What Changed
| Before | Today |
|---|---|
| Autonomy meant a foreground session you babysat | Loops schedule their own wakeups with ScheduleWakeup |
| Fixed polling intervals, chosen once | Delay chosen per iteration, matched to what you’re waiting for |
sleep 480 in the shell | Long foreground sleeps blocked; Monitor waits on conditions |
| Poll everything, including your own background tasks | Harness-tracked work auto re-invokes; poll only external state |
| Recurring jobs wired up in external cron | Session cron tasks for in-session polling; cloud or Desktop scheduled tasks for standing jobs |
| Sleep duration was an afterthought | The cache TTL — five minutes or one hour — makes it a priced decision |
Key Insight
An agent that knows when to wake is cheaper than one that never sleeps.
Everything in this session — clamps, TTLs, sentinels — serves one idea: in scheduled autonomy, the interval is part of the answer. On a five-minute TTL, a loop that sleeps 300 seconds and one that sleeps 270 look almost identical but sit on opposite sides of a price cliff, which leaves exactly two good regimes — stay warm (under ~270 s, actively converging) or commit to the sleep (1200 s+, idling). On a one-hour TTL the cliff disappears, and the question becomes simply how long this wait is worth. Either way, the cheapest schedule of all is no schedule, because harness-tracked work reports back on its own.
A cloud scheduled task answers “run this every night.” Dynamic wakeups answer the harder question — “how long is this particular wait worth?” — and that judgment is what you gain by letting the model hold the clock.
Next Session
Session 32 closes the module with The New Tool Belt: Artifacts, Tasks & Typed Outputs — the current user-facing tools: hosted Artifacts pages, where the task tools still apply, typed review findings, and how background work surfaces through output files and notifications.