← 返回 FEED
CLAUDE2026-04-21

Claude Code Routines 完全指南:定时、API 与 GitHub 触发器

场景对比

没有 Routines 的周一早上: 打开笔记本,发现周末积压了 40 个 issue,全没标签、没分配。3 个 PR 从周四就在等审查。周五的部署出去了但没人验证。你花一整天做分类工作。

有 Routines 的周一早上: 每条周末 issue 都已被标签化并分配。3 个 PR 各自有审查摘要,包含安全、性能、风格的内联评论。Slack 的 #eng-standup 频道里,一条消息确认了周五部署是干净的。Claude 在你笔记本合上的时候完成了所有这些。

Routines 是什么

Claude Code Routines 是保存的配置(Prompt + 仓库 + 连接器),运行在 Anthropic 托管的云基础设施上。通过定时、HTTP API 调用或 GitHub 事件触发。

三种触发方式可以组合:同一个 Routine 可以同时设置定时触发、API 触发和 GitHub 事件触发。

与 /loop 和 Desktop 定时任务的区别: /loop 绑定 session,Desktop 定时任务绑定机器。Routines 在笔记本关机时继续运行——这是核心差异。

Routine 的三个组成部分

  1. Prompt(最重要):Routine 没有人工逐步审批,Prompt 承担全部认知负荷
  2. 仓库:Claude 克隆并在其内工作的代码仓库
  3. 连接器(可选):MCP 集成(Slack、Sentry、Linear、GitHub 等)

创建入口

  1. Web UI(claude.ai/code/routines):支持全部三种触发方式,是标准入口
  2. CLI /schedule:在活跃 Claude Code session 内创建,只能创建定时触发,之后在 web 编辑添加 API/GitHub 触发
  3. Desktop app(New Task > New Remote Task):区别于本地 Desktop 定时任务(跑在你机器上)

Prompt 怎么写

好 Routine 和坏 Routine 的区别在于具体程度。

坏 Prompt:"Check for issues."

好 Prompt:"Read all GitHub issues opened today in {repo}, apply a label from [bug, feature, docs, question, needs-triage] to each, assign it based on which files it references, and post a summary to #eng-standup with the count and breakdown."

Routine 没有人工逐步审批,所以要明确指定:完成什么样算完成、什么连接器要使用、异常情况怎么处理。

触发器 1:定时触发(Schedule)

预设节奏:每小时、每天、工作日(周一至周五)、每周。也可以用自定义 cron 表达式,精确到每周二早 9 点或每月 1 号。最小间隔 1 小时,低于 1 小时的 cron 会被拒绝。

设计原则:按照"某个时间范围内完成"而不是"精确在某个时刻"。如果需要精确到 23:00:00 触发,定时触发不是正确工具。

backlog grooming 模板:

# Nightly backlog grooming

It's end of day. Read all GitHub issues opened today in {repo}.

For each issue:
- Apply the appropriate label from: bug, feature, docs, question, needs-triage
- Assign it to the relevant owner based on which files or directories it references
- If the issue is unclear or missing reproduction steps, leave a comment requesting more information

After processing all issues, post a summary to #dev-standup in Slack:
- Total issues processed today
- Breakdown by label
- Any issues flagged as needing human attention

Keep the Slack message concise. Use bullet points. If zero issues were filed today, post a single line: "No new issues today."

触发器 2:API 触发

API 触发给每个 Routine 一个专属 HTTP 端点:

POST https://api.anthropic.com/v1/claude_code/routines/{trigger_id}/fire

完整 curl 示例:

curl -X POST https://api.anthropic.com/v1/claude_code/routines/{trigger_id}/fire \
  -H "Authorization: Bearer {your_token}" \
  -H "anthropic-beta: experimental-cc-routine-2026-04-01" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"text": "Production alert: error rate on /api/checkout exceeded 5% threshold."}'

成功返回 session ID 和 URL,可以在浏览器里实时观看 Claude 在做什么,或者手动继续对话。

三个要注意的点:

  1. text 字段是纯字符串。如果发送 {"alert_id": "123"},Claude 读到的是 JSON 字符串,不是结构化数据
  2. 每个 token 绑定一个 Routine,轮换不影响其他
  3. beta header 可能轮换,当前是 experimental-cc-routine-2026-04-01

实用场景: 监控工具在 error rate 超过阈值时调用 /fire,Routine 拉取堆栈跟踪、与最近提交关联、打开发起修复 PR。on-call 工程师审查 PR,而不是凌晨 2 点对着空白 terminal 开始工作。

触发器 3:GitHub 触发

GitHub 触发翻转了激活模型:不是你的工具链调用 Claude,而是 GitHub 在仓库事件发生时自动调用 Claude。

支持的 GitHub 事件:

  • pull_request:opened、closed、assigned、labeled、synchronized,或其他更新
  • release:created、published、edited、deleted

Setup 两个步骤(很多人只做了第一个):

  1. 在 Claude Code 里运行 /web-setup 授予仓库访问权限(clone 用)
  2. 单独安装 Claude GitHub App 到目标仓库,以启用 webhook 投递(/web-setup 不会安装 GitHub App,两者都需要)

过滤条件: 可以按 Author、Title、Body、Base branch、Head branch、Labels、Is draft、Is merged、From fork 过滤。所有条件都匹配才触发。

Regex 注意: . 匹配整个字段值,不是子字符串。匹配标题含 "hotfix" 的 PR 要写 .*hotfix.*,而不是 hotfix

Branch 权限: 默认 Claude 只能 push 到 claude/ 前缀的分支。如需推送到其他分支,在 Routine 设置里启用 "Allow unrestricted branch pushes"。

PR code review 模板:

# PR code review checklist

A new pull request has been opened. Review it against our team checklist.

## Security
- Any hardcoded secrets, API keys, or credentials in the diff?
- Any unvalidated user inputs that could enable injection attacks?
- Any new dependencies? If so, check for known CVEs.

## Performance
- N+1 query patterns in ORM calls?
- Missing database indexes for new query patterns in this PR?
- Large synchronous operations that should be async?

## Code style
- Follows our eslint configuration?
- Consistent naming with the rest of the codebase?
- Functions and variables named clearly enough to be self-documenting?

Leave an inline comment on each specific issue found. Give the line number and the specific fix, not vague observations. Post a top-level summary comment with a pass/fail for each category.

容量限制

  • 每日运行上限: 每个计划有每日上限,在 claude.ai/code/settings 查看。Anthropic 尚未在文档中发布官方 per-plan 数字
  • GitHub 每小时上限: 独立于每日上限。达到上限后事件被丢弃,不排队。保持过滤规则严格
  • 超额使用: Team 和 Enterprise 计划如果启用了计量超额使用,可以在达到每日上限后继续按量计费;其他计划在窗口重置前被拒绝

与其他工具的比较

  • GitHub Actions:语言无关的 CI/CD 流水线(构建、测试、部署)
  • Routines:需要 AI 推理的开发工作(审查合并了什么、分类失败的东西、验证部署了什么)
  • n8n / Zapier:连接 10+ 非编码 SaaS 工具
  • Cron:不需要 AI 推理的简单本地脚本

结论:对大多数团队来说是两者都用。用 Actions 做构建/测试/部署流水线,用 Routines 做这些流水线周围的 AI 推理工作。

局限性

  • Routine 所有权归个人账户,不属于团队或组织
  • PR 和 commit 以你的个人 GitHub 身份出现,不是 bot 账户
  • 研究预览期间不支持团队共享或共同所有权
  • 触发器上限被达到后事件被丢弃,不排队