``` 克劳德代码例程 ```
Claude Code Routines

原始链接: https://code.claude.com/docs/en/routines

## Claude Routines:自动化代码任务 Claude Routines 提供了一种使用保存的 Claude Code 配置(提示、仓库和连接器)来自动化重复编码任务的方式,这些配置在 Anthropic 的云基础设施上运行。该功能适用于 Pro、Max、Team 和 Enterprise 计划,routines 可以持续执行,即使您的笔记本电脑已关闭。 **主要特性:** * **触发器:** Routines 可以由计划时间(每小时、每天等)、API 调用(通过唯一端点)或 GitHub 事件(PR、推送、问题)启动。触发器的组合是可能的。 * **自主执行:** Routines 在没有审批提示的情况下运行,利用选定的仓库、环境和连接器。 * **用例:** 示例包括自动化的积压整理、告警分类、代码审查、部署验证、文档更新和跨语言库移植。 * **管理:** Routines 可以通过 Web 界面 (claude.ai/code/routines) 或 CLI (/schedule) 创建和管理。 * **连接器和环境:** Routines 利用现有的连接器访问外部服务,并在可配置的云环境中运行,控制网络访问和变量。 **重要注意事项:** * **使用限制:** Routines 会消耗订阅用量,并受到每日运行上限的限制。 * **GitHub 访问:** 需要安装 Claude GitHub App 才能使用事件触发器。 * **研究预览:** 功能可能会发生变化。

## Claude 代码例程:摘要 Anthropic 最近在 Claude 代码中推出了“例程”功能,允许用户通过计划运行、API 触发和事件响应(如 GitHub 更新)来自动化任务。然而,该功能的推出正在 Hacker News 社区引发关于其服务条款和实用性的争论。 用户对使用限制感到困惑——虽然允许在 cron 上使用 `claude -p`,但将 Claude 与 Telegram 机器人等工具集成,除非使用按 token 计费,否则可能违反服务条款。人们担心 Anthropic 正在将用户锁定在其生态系统中,并可能对先前允许的使用进行追溯计费。 讨论还集中在效率问题上。一些人认为例程是对 OpenClaw 的直接回应,而另一些人则质疑与专用工具(如 n8n)相比,使用 LLM 进行自动化的资源密集程度。人们对模型一致性表示怀疑,并担心“削弱”,以及对 Anthropic 不断变化的政策和潜在垄断行为的更广泛担忧。尽管存在这些担忧,许多人承认 Anthropic 在功能交付方面的快速步伐。
相关文章

原文

Routines are in research preview. Behavior, limits, and the API surface may change.

A routine is a saved Claude Code configuration: a prompt, one or more repositories, and a set of connectors, packaged once and run automatically. Routines execute on Anthropic-managed cloud infrastructure, so they keep working when your laptop is closed. Each routine can have one or more triggers attached to it:
  • Scheduled: run on a recurring cadence like hourly, nightly, or weekly
  • API: trigger on demand by sending an HTTP POST to a per-routine endpoint with a bearer token
  • GitHub: run automatically in response to repository events such as pull requests, pushes, issues, or workflow runs
A single routine can combine triggers. For example, a PR review routine can run nightly, trigger from a deploy script, and also react to every new PR. Routines are available on Pro, Max, Team, and Enterprise plans with Claude Code on the web enabled. Create and manage them at claude.ai/code/routines, or from the CLI with /schedule. This page covers creating a routine, configuring each trigger type, managing runs, and how usage limits apply.

Example use cases

Each example pairs a trigger type with the kind of work routines are suited to: unattended, repeatable, and tied to a clear outcome. Backlog maintenance. A schedule trigger runs every weeknight against your issue tracker via a connector. The routine reads issues opened since the last run, applies labels, assigns owners based on the area of code referenced, and posts a summary to Slack so the team starts the day with a groomed queue. Alert triage. Your monitoring tool calls the routine’s API endpoint when an error threshold is crossed, passing the alert body as text. The routine pulls the stack trace, correlates it with recent commits in the repository, and opens a draft pull request with a proposed fix and a link back to the alert. On-call reviews the PR instead of starting from a blank terminal. Bespoke code review. A GitHub trigger runs on pull_request.opened. The routine applies your team’s own review checklist, leaves inline comments for security, performance, and style issues, and adds a summary comment so human reviewers can focus on design instead of mechanical checks. Deploy verification. Your CD pipeline calls the routine’s API endpoint after each production deploy. The routine runs smoke checks against the new build, scans error logs for regressions, and posts a go or no-go to the release channel before the deploy window closes. Docs drift. A schedule trigger runs weekly. The routine scans merged PRs since the last run, flags documentation that references changed APIs, and opens update PRs against the docs repository for an editor to review. Library port. A GitHub trigger runs on pull_request.closed filtered to merged PRs in one SDK repository. The routine ports the change to a parallel SDK in another language and opens a matching PR, keeping the two libraries in step without a human re-implementing each change. The sections below walk through creating a routine and configuring each of these trigger types.

Create a routine

Create a routine from the web, the Desktop app, or the CLI. All three surfaces write to the same cloud account, so a routine you create in the CLI shows up at claude.ai/code/routines immediately. In the Desktop app, click New task and choose New remote task; choosing New local task instead creates a local Desktop scheduled task, which runs on your machine and is not a routine. The creation form sets up the routine’s prompt, repositories, environment, connectors, and triggers. Routines run autonomously as full Claude Code cloud sessions: there is no permission-mode picker and no approval prompts during a run. The session can run shell commands, use skills committed to the cloned repository, and call any connectors you include. What a routine can reach is determined by the repositories you select and their branch-push setting, the environment’s network access and variables, and the connectors you include. Scope each of those to what the routine actually needs. Routines belong to your individual claude.ai account. They are not shared with teammates, and they count against your account’s daily run allowance. Anything a routine does through your connected GitHub identity or connectors appears as you: commits and pull requests carry your GitHub user, and Slack messages, Linear tickets, or other connector actions use your linked accounts for those services.

Create from the web

Name the routine and write the prompt

Give the routine a descriptive name and write the prompt Claude runs each time. The prompt is the most important part: the routine runs autonomously, so the prompt must be self-contained and explicit about what to do and what success looks like.The prompt input includes a model selector. Claude uses the selected model on every run.

Select repositories

Add one or more GitHub repositories for Claude to work in. Each repository is cloned at the start of a run, starting from the default branch. Claude creates claude/-prefixed branches for its changes. To allow pushes to any branch, enable Allow unrestricted branch pushes for that repository.

Select an environment

Pick a cloud environment for the routine. Environments control what the cloud session has access to:
  • Network access: set the level of internet access available during each run
  • Environment variables: provide API keys, tokens, or other secrets Claude can use
  • Setup script: run install commands before each session starts, like installing dependencies or configuring tools
A Default environment is provided. To use a custom environment, create one before creating the routine.

Select a trigger

Under Select a trigger, choose how the routine starts. You can pick one trigger type or combine several.
Pick a preset frequency: hourly, daily, weekdays, or weekly. See Add a schedule trigger for timezone handling, stagger, and custom cron intervals.

Review connectors

All of your connected MCP connectors are included by default. Remove any that the routine doesn’t need. Connectors give Claude access to external services like Slack, Linear, or Google Drive during each run.

Create the routine

Click Create. The routine appears in the list and runs the next time one of its triggers matches. To start a run immediately, click Run now on the routine’s detail page.Each run creates a new session alongside your other sessions, where you can see what Claude did, review changes, and create a pull request.

Create from the CLI

Run /schedule in any session to create a scheduled routine conversationally. You can also pass a description directly, as in /schedule daily PR review at 9am. Claude walks through the same information the web form collects, then saves the routine to your account. /schedule in the CLI creates scheduled routines only. To add an API or GitHub trigger, edit the routine on the web at claude.ai/code/routines. The CLI also supports managing existing routines. Run /schedule list to see all routines, /schedule update to change one, or /schedule run to trigger it immediately.

Create from the Desktop app

Open the Schedule page in the Desktop app, click New task, and choose New remote task. The Desktop app shows both local scheduled tasks and routines in the same grid. See Desktop scheduled tasks for details on the local option.

Configure triggers

A routine starts when one of its triggers matches. You can attach any combination of schedule, API, and GitHub triggers to the same routine, and add or remove them at any time from the Select a trigger section of the routine’s edit form.

Add a schedule trigger

A schedule trigger runs the routine on a recurring cadence. Pick a preset frequency in the Select a trigger section: hourly, daily, weekdays, or weekly. Times are entered in your local zone and converted automatically, so the routine runs at that wall-clock time regardless of where the cloud infrastructure is located. Runs may start a few minutes after the scheduled time due to stagger. The offset is consistent for each routine. For a custom interval such as every two hours or the first of each month, pick the closest preset in the form, then run /schedule update in the CLI to set a specific cron expression. The minimum interval is one hour; expressions that run more frequently are rejected.

Add an API trigger

An API trigger gives a routine a dedicated HTTP endpoint. POSTing to the endpoint with the routine’s bearer token starts a new session and returns a session URL. Use this to wire Claude Code into alerting systems, deploy pipelines, internal tools, or anywhere you can make an authenticated HTTP request. API triggers are added to an existing routine from the web. The CLI cannot currently create or revoke tokens.

Open the routine for editing

Go to claude.ai/code/routines, click the routine you want to trigger via API, then click the pencil icon to open Edit routine.

Add an API trigger

Scroll to the Select a trigger section below the prompt, click Add another trigger, and choose API.

Copy the URL and generate a token

The modal shows the URL for this routine along with a sample curl command. Copy the URL, then click Generate token and copy the token immediately. The token is shown once and cannot be retrieved later, so store it somewhere secure such as your alerting tool’s secret store.

Call the endpoint

Send the token in the Authorization: Bearer header when you POST to the URL. The Trigger a routine section below shows a complete example.
Each routine has its own token, scoped to triggering that routine only. To rotate or revoke it, return to the same modal and click Regenerate or Revoke.

Trigger a routine

Send a POST request to the /fire endpoint with the bearer token in the Authorization header. The request body accepts an optional text field that’s appended to the routine’s configured prompt as a one-shot user turn. Use text to pass context like an alert body or a failing log. The example below triggers a routine from a shell:
curl -X POST https://api.anthropic.com/v1/claude_code/routines/trig_01ABCDEFGHJKLMNOPQRSTUVW/fire \
  -H "Authorization: Bearer sk-ant-oat01-xxxxx" \
  -H "anthropic-beta: experimental-cc-routine-2026-04-01" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"text": "Sentry alert SEN-4521 fired in prod. Stack trace attached."}'
A successful request returns a JSON body with the new session ID and URL:
{
  "type": "routine_fire",
  "claude_code_session_id": "session_01HJKLMNOPQRSTUVWXYZ",
  "claude_code_session_url": "https://claude.ai/code/session_01HJKLMNOPQRSTUVWXYZ"
}
Open the session URL in a browser to watch the run in real time, review changes, or continue the conversation manually.

The /fire endpoint ships under the experimental-cc-routine-2026-04-01 beta header. Request and response shapes, rate limits, and token semantics may change while the feature is in research preview. Breaking changes ship behind new dated beta header versions, and the two most recent previous header versions continue to work so that callers have time to migrate.

API reference

For the full API reference, including all error responses, validation rules, and field limits, see Trigger a routine via API in the Claude Platform documentation. The /fire endpoint is available to claude.ai users only and is not part of the Claude Platform API surface.

Add a GitHub trigger

A GitHub trigger starts a new session automatically when a matching event occurs on a connected repository. Each matching event starts its own session.
During the research preview, GitHub webhook events are subject to per-routine and per-account hourly caps. Events beyond the limit are dropped until the window resets. See your current limits at claude.ai/code/routines.
GitHub triggers are configured from the web UI only.

Open the routine for editing

Add a GitHub event trigger

Scroll to the Select a trigger section, click Add another trigger, and choose GitHub event.

Install the Claude GitHub App

The Claude GitHub App must be installed on the repository you want to subscribe to. The trigger setup prompts you to install it if it isn’t already.

Running /web-setup in the CLI grants repository access for cloning, but it does not install the Claude GitHub App and does not enable webhook delivery. GitHub triggers require installing the Claude GitHub App, which the trigger setup prompts you to do.

Configure the trigger

Select the repository, choose an event from the supported events list, and optionally add filters. Save the trigger.

Supported events

GitHub triggers can subscribe to any of the following event categories. Within each category you can pick a specific action, such as pull_request.opened, or react to all actions in the category.
EventTriggers when
Pull requestA PR is opened, closed, assigned, labeled, synchronized, or otherwise updated
Pull request reviewA PR review is submitted, edited, or dismissed
Pull request review commentA comment on a PR diff is created, edited, or deleted
PushCommits are pushed to a branch
ReleaseA release is created, published, edited, or deleted
IssuesAn issue is opened, edited, closed, labeled, or otherwise updated
Issue commentA comment on an issue or PR is created, edited, or deleted
Sub issuesA sub-issue or parent issue is added or removed
Commit commentA commit or diff is commented on
DiscussionA discussion is created, edited, answered, or otherwise updated
Discussion commentA discussion comment is created, edited, or deleted
Check runA check run is created, requested, rerequested, or completed
Check suiteA check suite completes or is requested
Merge queue entryA PR enters or leaves the merge queue
Workflow runA GitHub Actions workflow run starts or completes
Workflow jobA GitHub Actions job is queued or completes
Workflow dispatchA workflow is manually triggered
Repository dispatchA custom repository_dispatch event is sent

Filter pull requests

Use filters to narrow which pull requests start a new session. All filter conditions must match for the routine to trigger. The available filter fields are:
FilterMatches
AuthorPR author’s GitHub username
TitlePR title text
BodyPR description text
Base branchBranch the PR targets
Head branchBranch the PR comes from
LabelsLabels applied to the PR
Is draftWhether the PR is in draft state
Is mergedWhether the PR has been merged
From forkWhether the PR comes from a fork
A few example filter combinations:
  • Auth module review: base branch main, head branch contains auth-provider. Sends any PR that touches authentication to a focused reviewer.
  • External contributor triage: from fork is true. Routes every fork-based PR through an extra security and style review before a human looks at it.
  • Ready-for-review only: is draft is false. Skips drafts so the routine only runs when the PR is ready for review.
  • Label-gated backport: labels include needs-backport. Triggers a port-to-another-branch routine only when a maintainer tags the PR.

How sessions map to events

Each matching GitHub event starts a new session. Session reuse across events is not available for GitHub-triggered routines, so two pushes or two PR updates produce two independent sessions.

Manage routines

Click a routine in the list to open its detail page. The detail page shows the routine’s repositories, connectors, prompt, schedule, API tokens, GitHub triggers, and a list of past runs.

View and interact with runs

Click any run to open it as a full session. From there you can see what Claude did, review changes, create a pull request, or continue the conversation. Each run session works like any other session: use the dropdown menu next to the session title to rename, archive, or delete it.

Edit and control routines

From the routine detail page you can:
  • Click Run now to start a run immediately without waiting for the next scheduled time.
  • Use the toggle in the Repeats section to pause or resume the schedule. Paused routines keep their configuration but don’t run until you re-enable them.
  • Click the pencil icon to open Edit routine and change the name, prompt, repositories, environment, connectors, or any of the routine’s triggers. The Select a trigger section is where you add or remove schedules, API tokens, and GitHub event triggers.
  • Click the delete icon to remove the routine. Past sessions created by the routine remain in your session list.

Repositories and branch permissions

Routines need GitHub access to clone repositories. When you create a routine from the CLI with /schedule, Claude checks whether your account has GitHub connected and prompts you to run /web-setup if it doesn’t. See GitHub authentication options for the two ways to grant access. Each repository you add is cloned on every run. Claude starts from the repository’s default branch unless your prompt specifies otherwise. By default, Claude can only push to branches prefixed with claude/. This prevents routines from accidentally modifying protected or long-lived branches. To remove this restriction for a specific repository, enable Allow unrestricted branch pushes for that repository when creating or editing the routine.

Connectors

Routines can use your connected MCP connectors to read from and write to external services during each run. For example, a routine that triages support requests might read from a Slack channel and create issues in Linear. When you create a routine, all of your currently connected connectors are included by default. Remove any that aren’t needed to limit which tools Claude has access to during the run. You can also add connectors directly from the routine form. To manage or add connectors outside of the routine form, visit Settings > Connectors on claude.ai or use /schedule update in the CLI.

Environments

Each routine runs in a cloud environment that controls network access, environment variables, and setup scripts. Configure environments before creating a routine to give Claude access to APIs, install dependencies, or restrict network scope. See cloud environment for the full setup guide.

Usage and limits

Routines draw down subscription usage the same way interactive sessions do. In addition to the standard subscription limits, routines have a daily cap on how many runs can start per account. See your current consumption and remaining daily routine runs at claude.ai/code/routines or claude.ai/settings/usage. When a routine hits the daily cap or your subscription usage limit, organizations with extra usage enabled can keep running routines on metered overage. Without extra usage, additional runs are rejected until the window resets. Enable extra usage from Settings > Billing on claude.ai.
联系我们 contact @ memedata.com