AI Learn
Anthropic · Practical Tutorial

Automate a recurring repository task with Claude Code Routines

Turn a repeatable coding workflow into a scheduled, API-triggered or event-driven routine with an explicit scope, approvals and review path.

Reviewed September 19, 2026Independent tutorialFeatures may vary
Not official support. AI Learn is an independent education site. Check the provider’s current documentation for plan-specific limits, pricing and feature availability.
Before you start

Recurring automation magnifies both good instructions and bad ones. Start with a task that has stable inputs, clear stop conditions and a safe review path. Do not begin with production deploys or open-ended “fix everything” prompts.

1. Choose a recurring task whose input changes but workflow stays stable

Pick work whose inputs change but whose procedure is stable: dependency audits, issue triage, report generation or recurring test sweeps. If humans still change the procedure every run, it is too early to schedule it.

2. Write the routine prompt with repository, trigger, stop condition and allowed actions

Define the repository scope, trigger, allowed writes, required tests and explicit stop conditions. A routine should know when to produce a draft, when to ask for help and when to do nothing.

3. Start with draft outputs or draft PRs instead of automatic merges

Start with a low-consequence output such as a report, issue or draft PR. This lets you evaluate false positives and permissions before the routine is allowed to modify protected branches or production systems.

4. Run it manually once and inspect logs, artifacts and permissions before scheduling

Run the routine manually once and read the full log. Verify which files changed, what credentials or tools were requested, whether tests actually ran and whether the produced artifact matches the intended scope.

5. Set a cadence or event trigger, then review failed or surprising runs regularly

Only then schedule it. Review failed and surprising runs periodically, because repository structure, dependencies and permissions change over time even when the routine definition does not.

Try this prompt

Create a Claude Code routine for [recurring task]. Trigger: [schedule/event]. Repository: [repo]. Allowed actions: [read/test/draft PR]. Stop if [condition]. Never merge, deploy, change secrets or broaden permissions automatically. For every run, produce a short evidence log and link to the draft output.

Common mistakes

Automating a workflow before running it manually; no stop condition; letting the routine merge its own fixes; allowing broad connectors by default; never reviewing failures because most runs succeed.

Finish check

A manual test run is understood, recurring runs produce reviewable drafts and evidence, and the routine cannot silently cross the actions you deliberately reserved for humans.

开始之前

周期自动化会同时放大好规则和坏规则。先选择输入稳定、停止条件清晰、复核路径安全的任务,不要一开始就自动部署生产,也不要用“把所有问题都修掉”这类开放指令。

1. 选择“输入会变化,但流程基本固定”的周期任务

选择“输入会变化,但处理步骤长期稳定”的工作,例如依赖审计、Issue 分类、定期报告或测试巡检。如果每次运行都需要人重新决定流程,就还不适合自动排程。

2. 在 Routine 指令里写清仓库、触发条件、停止条件和允许动作

写清仓库范围、触发条件、允许写入的位置、必须运行的测试和停止条件。Routine 应知道什么时候生成草稿、什么时候求助,以及什么时候应该什么都不做。

3. 先让它生成草稿或 Draft PR,不要直接自动合并

先让它产生低风险结果,例如报告、Issue 或 Draft PR。这样可以在允许它修改受保护分支或生产系统之前,先观察误报率、权限需求和输出质量。

4. 正式定时前先手工跑一次,检查日志、产物和权限

先手动运行一次并完整查看日志。确认它改了哪些文件、请求了哪些凭据或工具、测试是否真的执行,以及产物有没有超出原定范围。

5. 设置时间或事件触发后,定期查看失败运行和异常行为

通过手动验证后再设置定时运行,并定期检查失败或异常运行。仓库结构、依赖和权限会变化,所以“曾经正确”不代表自动化会永远正确。

可以直接套用的提示词

创建一个 Claude Code Routine 来处理 [周期任务]。触发:[时间/事件]。仓库:[repo]。允许动作:[读取/测试/创建 Draft PR]。遇到 [条件] 就停止。不得自动合并、部署、修改密钥或扩大权限。每次运行都输出简短证据日志和草稿结果链接。

常见错误

手工流程都没跑通就直接自动化;没有停止条件;让 Routine 自己合并自己的修改;默认开放大量连接器;因为大多数运行成功就从不复盘失败记录。

完成检查

手工测试运行已经看懂,周期运行会产出可复核的草稿和证据,而且 Routine 无法悄悄越过你明确保留给人工的操作。

開始之前

週期自動化會同時放大好規則和壞規則。先選擇輸入穩定、停止條件清晰、復核路徑安全的任務,不要一開始就自動部署生產,也不要用“把所有問題都修掉”這類開放指令。

1. 選擇“輸入會變化,但流程基本固定”的週期任務

選擇“輸入會變化,但處理步驟長期穩定”的工作,例如依賴審計、Issue 分類、定期報告或測試巡檢。如果每次運行都需要人重新決定流程,就還不適合自動排程。

2. 在 Routine 指令裡寫清儲存庫、觸發條件、停止條件和允許動作

寫清倉庫範圍、觸發條件、允許寫入的位置、必須運行的測試和停止條件。Routine 應知道甚麼時候生成草稿、甚麼時候求助,以及甚麼時候應該甚麼都不做。

3. 先讓它生成草稿或 Draft PR,不要直接自動合併

先讓它產生低風險結果,例如報告、Issue 或 Draft PR。這樣可以在允許它修改受保護分支或生產系統之前,先觀察誤報率、權限需求和輸出質量。

4. 正式定時前先手工跑一次,檢查記錄、產物和權限

先手動運行一次並完整查看日誌。確認它改了哪些檔案、請求了哪些憑據或工具、測試是否真的執行,以及產物有沒有超出原定範圍。

5. 設定時間或事件觸發後,定期查看失敗執行和異常行為

通過手動驗證後再設置定時運行,並定期檢查失敗或異常運行。倉庫結構、依賴和權限會變化,所以“曾經正確”不代表自動化會永遠正確。

可以直接套用的提示詞

創建一個 Claude Code Routine 來處理 [週期任務]。觸發:[時間/事件]。儲存庫:[repo]。允許動作:[讀取/測試/創建 Draft PR]。遇到 [條件] 就停止。不得自動合併、部署、修改密鑰或擴大權限。每次執行都輸出簡短證據記錄和草稿結果連結。

常見錯誤

手工流程都沒跑通就直接自動化;沒有停止條件;讓 Routine 自己合併自己的修改;預設開放大量連接器;因為大多數執行成功就從不復盤失敗記錄。

完成檢查

手工測試執行已經看懂,週期執行會產出可復核的草稿和證據,而且 Routine 無法悄悄越過你明確保留給人工的操作。

Official references

Check current product details at the source

Related practical tutorials

Continue with another single-task workflow.

All tutorials →