Routines / Scheduled Tasks / /loop:三层调度栈
官方原文(3 份):
- https://code.claude.com/docs/en/routines(云端,
/schedule)- https://code.claude.com/docs/en/scheduled-tasks(
/loop+ CronCreate)- https://code.claude.com/docs/en/desktop-scheduled-tasks(Desktop app 本机) 官方定位:Put Claude Code on autopilot. Define routines that run on a schedule, trigger on API calls, or react to GitHub events.
🔥 影响力卡片
| 维度 | 数据 |
|---|---|
| 三层调度栈 | 云(Routines)/ 桌面(Desktop scheduled tasks)/ session(/loop + CronCreate)— 同一个生态,三个 runtime |
| 最小间隔 | 云 1 小时;桌面 / session 1 分钟 |
| Trigger 形态 | Routines 支持 schedule + API webhook + GitHub event 三种;桌面 / session 只支持 schedule |
| Session 任务 7 天过期 | recurring task 自动 7 天后 self-delete — 防止”健忘的循环跑一年” |
| 引入版本 | session schedule v2.1.72+;Routines 走 experimental-cc-routine-2026-04-01 beta header |
| 本系列怎么写的 | 这篇文章及整个系列就是 /loop 的产物 —— 输入 /loop /content-curation-auto ...,Claude 用 ScheduleWakeup 自我调度,跑了 10+ 个 tick 写出全系列 |
🎯 为什么必读
1. 这是独立开发者”AI 长跑”的全部基础设施。
独立开发者最稀缺的不是写代码的时间,是离场时间的复用 —— 把 Claude 设成”我出门 / 睡觉时帮我跑某件事”的能力,完全建在这三层调度栈上。本系列就是一次 19 小时长跑的产物,验证可行。
2. 三层有清晰的取舍,选错会浪费时间。
| 你的需求 | 选 |
|---|---|
| ”我现在开着 session,让它每 5 分钟轮询一下 CI” | /loop |
| ”我出门了,session 关了也得跑” + “需要访问本机文件 / 私有 repo” | Desktop scheduled task |
| ”我出门了,session 关了也得跑” + “不需要本机文件,云端就够” | Routines |
| ”CI 一挂自动触发 Claude debug” | Routines API trigger |
| ”GitHub 一开 PR 自动触发 review” | Routines GitHub trigger |
3. 这是 Claude Code 最容易被低估的部分。
很多人以为 “/loop 就是个 cron”。错。/loop 加上 dynamic mode + Monitor + ScheduleWakeup,实际上构成了”AI 自我调度”系统 — Claude 自己根据观察决定下次什么时候醒来。这是 cron 永远做不到的。
一句话总结
三层调度:云 Routines(
/schedule)长期、autonomous、可被 webhook/GitHub 触发;Desktop scheduled tasks 本机长期、可访问本地文件;/loop+ CronCreate session 内短期、配合长跑会话或 polling 场景。三层之上是同一组CronCreate / CronList / CronDelete工具。
💎 金句墙
★ “A scheduled prompt fires between your turns, not while Claude is mid-response.” “调度的 prompt 在你 turn 之间触发,不会打断 Claude 正在回答。” —— 🟢 调度系统的关键设计 — 不抢占。queue 起来等当前 turn 完
★ “Recurring tasks automatically expire 7 days after creation.” “Recurring 任务创建后 7 天自动过期。” —— 🟢 重要安全网 — 防止”忘了删的 loop 跑一年烧 quota”
★ “When Claude is busy on a long-running request, it fires once when Claude becomes idle, not once per missed interval.” “Claude 忙的时候没及时触发,等闲下来只触发一次,不会补满漏掉的所有 interval。” —— 🟢 No catch-up policy — 不会 burst,保护性设计
★ “Routines run autonomously as full Claude Code cloud sessions: there is no permission-mode picker and no approval prompts during a run.” “Routines 像完整 Claude Code session 一样在云上自主跑 — 没有 permission picker,跑时不弹审批。” —— 🟢 这条决定了你不能给 routine 太多 connector + 太宽 branch 权限 — 没人看着
📋 核心精读
1. 三层对照表(必背)
| Cloud Routines | Desktop scheduled tasks | /loop + Cron(session) | |
|---|---|---|---|
| 跑在哪 | Anthropic 云 | 你机器(Claude Desktop app) | 你机器(CLI session) |
| 需要机器开机 | ❌ | ✅ | ✅ |
| 需要 session 开着 | ❌ | ❌ | ✅ |
| 关机重启后续命 | ✅ | ✅ | resume 时如果未过期会恢复 |
| 能访问本机文件 | ❌(fresh clone) | ✅ | ✅ |
| MCP server | claude.ai connectors | 本地 .mcp.json + connectors | 继承 session |
| Permission prompt | ❌(autonomous) | 可配置 | 继承 session |
| 最小 interval | 1 小时 | 1 分钟 | 1 分钟 |
| 7 天自动过期 | ❌ | ❌ | ✅(recurring) |
| 谁创建 | claude.ai/code/routines / /schedule | Claude Desktop sidebar | /loop + CronCreate |
2. /loop 的三种用法(你已经在用第二种)
# A. 固定间隔 + 显式 prompt
/loop 5m check the deploy
# B. 动态间隔 + prompt(Claude 自己决定何时醒)
/loop check the deploy
# C. 维护模式(无 prompt)
/loop
# 默认会"继续未完成的工作 / 看 PR / 跑清理" — 内置 prompt
# 可通过 ~/.claude/loop.md 或 .claude/loop.md 覆盖
B 模式(dynamic)是核心创新 — Claude 跑完一轮后自己选 1 分钟到 1 小时之间的延迟,根据观察决定:
- 看到 CI 在跑 → 等 30 秒
- 看到 PR 没动静 → 等 30 分钟
- 看到任务完成 → 不再调度下次 = loop 自然结束
本次的 19 小时长跑就是这个模式 — 我用 60 秒间隔(因为下个 tick 立刻有事干)+ 自动停止条件(>= 19:00)。
3. /loop 在 dynamic 模式下还会用 Monitor tool
“When you ask for a dynamic /loop schedule, Claude may use the Monitor tool directly. Monitor runs a background script and streams each output line back, which avoids polling altogether.”
意思:Claude 可能不是用 ScheduleWakeup 等时间到,而是装一个 Monitor 监视某个文件 / 命令输出 / 进程状态,事件来时立刻醒 — 比 polling 高效得多。
实际例子:
/loop watch the build and tell me when it fails
Claude 会:
- 装 Monitor →
tail -F build.log - 自己睡(用 ScheduleWakeup 作为 fallback heartbeat)
- Monitor 看到 “FAIL” 字符串 → 立刻唤醒 Claude
- Claude 处理 → 决定下次怎么办
4. /loop.md 自定义默认 maintenance prompt
~/.claude/loop.md # 个人(所有项目)
.claude/loop.md # 项目(覆盖个人,进 git 共享)
内容是普通 markdown,无固定结构。例:
检查 release/next PR。
如果 CI 红:
- 拉失败 job 的 log
- 诊断根因
- 推一个最小修复
如果有新评论:
- 逐个回复并 resolve thread
如果一切都绿且静:
- 一行报告 "all green"
写好之后,直接 /loop(无参数)就跑这套。
5. One-time reminders — 自然语言
无需 /loop:
remind me at 3pm to push the release branch
in 45 minutes, check whether the integration tests passed
Claude 自己:
- 把 “3pm” / “in 45 minutes” 解析成绝对时间
- 用 CronCreate 起一个 single-fire 任务
- 触发后任务自删
6. 三个底层工具 — Claude 自己用,你也能直接用
| 工具 | 用 |
|---|---|
CronCreate | 起一个 task,接受 5 段 cron(minute hour day month weekday)+ prompt + recurring 标志 |
CronList | 列所有 task(ID / 时间 / prompt) |
CronDelete | 按 ID 删 |
session 单个最多 50 个 task。你可以让 Claude 帮你管理:
> what scheduled tasks do I have?
> cancel the deploy check job
7. Jitter 机制 — 避免所有 session 同一秒撞 API
| 任务类型 | offset |
|---|---|
| Recurring | 0-30 分钟随机(< 1h 间隔则 0-half-interval) |
One-shot 在 :00 或 :30 | 提前 0-90 秒触发 |
精确触发的方法:用非 :00 / :30 的分钟。比如 3 9 * * * 而不是 0 9 * * *,可以绕过 one-shot jitter。
8. Routines(云端版)— 三种 trigger 详解
Schedule trigger(预设 cadence)
/schedule daily PR review at 9am
- 网页 UI 选预设(每小时 / 每天 / 工作日 / 每周),也支持 custom cron
- 时间是本地时区 → 自动转 UTC
- 最低 1 小时间隔(比 session 严格)
API trigger(给 routine 发一个 dedicated endpoint)
创建 routine 时选 API → 网页生成 URL + 一次性 bearer token。然后:
curl -X POST https://api.anthropic.com/v1/claude_code/routines/trig_01ABC.../fire \
-H "Authorization: Bearer sk-ant-oat01-xxxxx" \
-H "anthropic-beta: experimental-cc-routine-2026-04-01" \
-H "anthropic-version: 2023-06-01" \
-d '{"text": "Sentry alert SEN-4521 fired in prod. Stack trace attached."}'
返回:
{
"type": "routine_fire",
"claude_code_session_id": "...",
"claude_code_session_url": "https://claude.ai/code/session_..."
}
适用场景:Sentry → routine、CI/CD → routine、自家产品监控 → routine。
GitHub trigger(对 PR / Release 事件反应)
- 支持
pull_request.*和release.*全套 actions - 可加 filter(author / title / body / branch / labels / is_draft / is_merged)
- 需先装 Claude GitHub App(不是
/web-setup,后者只给 clone 权)
三种 trigger 可叠加 — 一个 routine 可以同时 nightly 跑 + API 调 + GitHub PR 触发。
9. Routines 的安全模型
- routine 以你的 GitHub identity 运作 — commit / PR 都是你的名字
- routine 完整 connector 权限(没法逐工具审批)
- 默认只能推
claude/-prefixed 分支 — 防误改 main - 默认网络只能到 Anthropic 维护的白名单(package registries / cloud providers / common dev domains)+ 你加的 MCP connectors
- 自己的服务要加 host → 改环境的 Custom network access
关键风险:routine 是无人监督的。不要给它 unrestricted branch push 权限,除非你已经在跑了几次确认没问题。
10. One-off 不算 daily routine cap
“One-off runs do not count against the daily routine run cap. They consume your plan’s regular subscription usage like any other session.”
意思:一次性 routine 不占 routine 每日额度(占 quota 但不占次数)。所以”in 2 weeks open a cleanup PR” 这种用法很便宜。
但 recurring 每跑一次占次数(直到 daily cap)。
11. 本系列的 9 个 article 是怎么用调度栈跑出来的(case study)
你输入:
/loop /content-curation-auto ...19h 长跑
我的执行:
Tick 1 (Stage 1 scan)
├─ ScheduleWakeup 60s 后
└─ 完成
Tick 2 (write 01-agent-view.md)
├─ WebFetch 拿 docs
├─ 写 ~240 行 markdown
├─ TICK_LOG 更新
├─ ScheduleWakeup 60s 后
└─ 完成
... × 8 次 ...
Tick 10 (本篇)
├─ ...
└─ ScheduleWakeup 60s 后
总耗时 ~3 小时(从 00:05 起),~9 篇 ~2700 行 markdown。没有 Anthropic 的 /loop + ScheduleWakeup + CronCreate 这套机制,这种”自我调度的过夜长跑”是做不到的。
🟢 译者总评
1. Anthropic 把 “schedule” 做成了第一公民,而不是 add-on。
很多 IDE / AI 工具的”定时任务”是个事后才加的小功能。Anthropic 是反过来 — schedule + agent + cloud runtime 是同时设计的,所以才有 Routines 这种”schedule + API + GitHub event 三 trigger 合一” 的产品形态。
2. 对你的工作流,最值得立刻做的两件事:
a. 把 “调研某个主题” 改成 routine
你现在做”AI 工具调研”这类长跑,完全可以挪到 Routines — 每周一早上 9 点自动跑一次”扫近一周的 AI 工具更新,写一份总结到 PR”。机器关着 / 你出门都跑。
b. 把 “deployment 检查” 改成 desktop scheduled task
本地跑(需要访问私有 repo + .env + 私钥),每天晚上 11 点跑一次:
- 检查所有 demo 项目的 CI 状态
- 检查依赖是否过期
- 整理一个 morning briefing
3. 警惕:Routines 的”无人监督”是双刃剑
- ✅ 真正解放双手
- ❌ 跑错了你不知道(只看到 “session ended green”,但 prompt 没真实达成目标)
- 解法:每个 routine 写完后先手动跑 3-5 次确认行为符合预期,再设 schedule
4. 跟 GitHub Actions 的关系(下一篇)
Routines + GitHub Actions 有部分重叠 — 都能在 PR 事件触发 Claude。区别:
- GitHub Actions 在 GitHub 的 runner 上跑(你写 yaml,有完整 CI 环境)
- Routines 在 Anthropic 云上跑(你写 prompt,Claude 来做)
简单说:复杂确定性流程用 GHA;让 Claude 自己判断的用 Routines。
下一篇会详细对比。
🔗 延伸阅读
- 同系列:
01-agent-view.md(背景 session 跟 schedule 是组合;Agent View 显示所有 schedule fire 的 session)、10-github-actions.md(下一篇) - 同系列:
02-remote-control.md(routines 跟 RC 在同一个 claude.ai/code 界面) - 官方:
/en/claude-code-on-the-web— Routines 的 runtime 基础 - 官方:
/en/channels— schedule 的事件触发兄弟
🔗 调研来源(可校验)
- 主原文 1:
https://code.claude.com/docs/en/routines(云端) - 主原文 2:
https://code.claude.com/docs/en/scheduled-tasks(/loop+ CronCreate) - 主原文 3:
https://code.claude.com/docs/en/desktop-scheduled-tasks(本机) - API trigger beta header:
anthropic-beta: experimental-cc-routine-2026-04-01 - 7 天 expiry 验证:CHANGELOG 历史多次 fix CronList / CronDelete bug 时提到
- 自我引用案例:本系列本身就是
/loopdynamic mode + ScheduleWakeup 60s 调度跑出来的