# 7.7 Routines：Claude Code 自动化任务

Routine 是 Claude Code 的自动化能力扩展——你只需配置一次（提示词、仓库、连接器），此后 Claude 就可以按计划自动执行、响应 API 调用或在 GitHub 事件发生时触发运行。

> 💡 Routines 于 2026 年 4 月 14 日推出（研究预览），适用于 Pro、Max、Team 和 Enterprise 计划。

## 7.7.1 三种触发模式

### Scheduled Routine（定时触发）

按 Cron 表达式周期性运行。典型场景：

* “每晚 2 点：从 Linear 拉取优先级最高的 Bug，尝试修复并开 Draft PR”
* “每周一早上：检查文档是否与代码同步，偏差超过阈值则发 Slack 通知”

### API Routine（HTTP 触发）

通过 HTTP POST 请求触发，支持自定义 Payload。适合与告警系统、部署钩子、CI/CD 流水线集成：

```bash
export ROUTINE_FIRE_URL="https://api.anthropic.com/v1/claude_code/routines/trig_.../fire"
export ROUTINE_TOKEN="<per-routine-token>"

curl -X POST "$ROUTINE_FIRE_URL" \
  -H "Authorization: Bearer $ROUTINE_TOKEN" \
  -H "anthropic-beta: experimental-cc-routine-2026-04-01" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"text":"High Error Rate in payment-api. Dashboard: https://..."}'
```

`ROUTINE_TOKEN` 是每个 Routine 单独生成的 Bearer token，应通过本地 secret 管理或 CI secret 注入，不要写入脚本、仓库或日志。

Claude 接收到 Payload 后启动独立会话，分析问题并执行预设的修复流程。

### Webhook Routine（GitHub 事件触发）

自动响应 GitHub 仓库事件（PR 创建、CI 失败、Issue 评论等）。Claude 为每个匹配的事件创建独立会话，并能处理后续更新：

* PR 创建时自动进行代码审查
* CI 失败时分析日志并尝试修复
* Issue 标记特定标签时自动分诊

## 7.7.2 执行限额

Routines 有账号级每日运行上限；当前消耗和剩余额度以 `claude.ai/code/routines` 或 `claude.ai/settings/usage` 显示为准。一次性运行不计入每日 routine run cap。

## 7.7.3 常见使用模式

| 模式      | 触发方式           | 描述                             |
| ------- | -------------- | ------------------------------ |
| 夜间积压分诊  | Scheduled      | 每晚扫描 Backlog，按优先级整理并发 Slack 摘要 |
| 文档漂移检测  | Scheduled（每周）  | 对比代码与文档，标记不一致之处                |
| 部署后验证   | API（部署钩子）      | 部署完成后自动运行冒烟测试                  |
| 告警关联分析  | API（监控系统）      | 收到告警后关联近期变更，定位根因               |
| PR 自动审查 | Webhook（PR 创建） | 对新 PR 执行安全/性能/风格审查             |
| CI 失败修复 | Webhook（CI 失败） | 分析失败日志，尝试修复并推送                 |

## 7.7.4 最佳实践

* **提示词设计**：Routine 的提示词应明确定义“完成”标准，避免 Agent 陷入无限循环
* **幂等性**：确保 Routine 多次执行不会产生副作用（如重复创建 PR）
* **结果通知**：将执行结果推送到 Slack、GitHub Discussion 或邮件，便于团队追踪
* **资源限制**：为 Routine 设置合理的执行时长和 Token 上限
* **监控告警**：对连续失败的 Routine 设置告警，避免“沉默失败”

## 7.7.5 本地 `/goal`/`/loop` vs 云端 `/schedule`

Claude Code 同时提供本地自动化原语（见 7.6.11）和云端 Routines（`/schedule`），两者面向不同场景：

| 维度    | 本地 `/goal` + `/loop`             | 云端 `/schedule`（Routine）      |
| ----- | -------------------------------- | ---------------------------- |
| 触发方式  | 当前会话内驱动                          | Cron / HTTP / GitHub Webhook |
| 持续时间  | 终端关闭即结束（除非配合 `claude --bg` 后台会话） | 笔记本关机仍继续，云端运行                |
| 状态可见性 | 当前 TUI 直接看到                      | `claude.ai/code/routines` 后台 |
| 适合场景  | "今天下午我想离开半小时让它跑完测试"              | "每晚 2 点扫积压、PR 创建时自动审"        |
| 计入额度  | 你的会话 Token 用量                    | Routine 每日运行上限               |

实操建议：

1. **可验证的短任务用 `/goal`**——比如让 Claude 把 lint 修干净、把测试跑绿
2. **定时/事件触发用 `/schedule`**——比如每周文档漂移检测、PR 自动审查
3. **跨设备协作用 `claude --bg` + `claude agents`**——后台会话比 Routine 灵活，但比 `/goal` 更难驯服


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/claude_guide/di-san-bu-fen-jin-jie-pian/07_coding/7.7_routines.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
