Steering Claude Code
Claude Code · Essay · 2026-06-18

Steering Claude Code: when to use CLAUDE.md, skills, hooks, and subagents

驾驭 Claude Code:CLAUDE.md、skill、hook、subagent 各自何时该用
Claude is built to work the way you work, and in Claude Code you can customize it. There are seven methods for instructing Claude's behavior—each trades context cost against authority.

Claude 天生就要贴着你的工作方式来,而在 Claude Code 里你可以定制它。共有七种方法来给 Claude 下达指令——每种都在"上下文开销"与"权威度"之间做权衡,选对方法本身就是大半的功夫。
来源:Claude 官方博客(claude.com/blog)· 2026-06-18 · 约 1,900 词 · 3 章(含 7 种方法)· 全文双语对照 · 作者 Michael Segner
TL;DR · 速读

Seven ways to steer, one tradeoff

  1. Each method trades context cost against authority—choosing the right one is most of the work.

    七种方法的核心权衡:上下文开销 vs 指令权威度。

    每种方法决定了:指令何时加载进上下文、能否熬过长会话的压缩(compaction)、以及携带多大的权威。

  2. CLAUDE.md is for always-on facts. Keep it under 200 lines, give it an owner.

    CLAUDE.md 放"永远为真的事实",不是流程。

    整段会话常驻、压缩后重读,每行都费 token;控制在 200 行内,指定负责人,像代码一样 review。

  3. Path-scoped rules load only when matching files are touched.

    Rules 是硬约束;用 paths: 限定作用域来省 token。

    不限定作用域的 rule 等同于塞进 CLAUDE.md——永远加载、永远费 token。

  4. Skills run in the main thread; subagents run in isolation and return only a summary.

    Skill = 流程(主线程内可见可控);Subagent = 隔离的旁支任务。

    Subagent 在自己的上下文窗口里跑,只有最终结论回到主会话——适合会污染主线的深搜 / 日志分析 / 依赖审计。

  5. Hooks fire deterministically on lifecycle events, at low context cost.

    Hook = 确定性自动化,上下文开销低。

    在文件编辑、工具调用、会话开始等生命周期事件上触发;配置活在主上下文之外,由 harness 执行。

  6. Output styles overwrite the default system prompt; append-system-prompt is only additive.

    Output style 会覆盖默认系统提示(权威最高,慎用);append 只做加法。

    改 output style 默认会抹掉"你是软件工程助手"等关键指令;--append-system-prompt 只在本次调用叠加,不改角色。

  7. A real guardrail needs to be deterministic—not a prompted rule.

    "绝不能做 X" 不该写进 CLAUDE.md,要用 hook / 权限 / 托管设置。

    prompt 里的规则在长会话、模糊情境或 prompt 注入下会失守;真正的护栏必须是确定性的。

Chapter 01

The seven methods for delivering instructions

下达指令的七种方法 · 总览与对照表
CLAUDE.md · rules · skills · subagents · hooks · output styles · append

Claude is built to work the way you work, and in Claude Code you can customize it.

Claude 天生就要贴着你的工作方式来,而在 Claude Code 里,你可以定制它。

There are seven methods for instructing Claude's behavior: CLAUDE.md files, rules, skills, subagents, hooks, output styles, and appending the system prompt.

有七种方法来指挥 Claude 的行为:CLAUDE.md 文件、rules(规则)、skills(技能)subagents(子代理)hooks(钩子)、output styles(输出风格),以及追加系统提示(append the system prompt)。

Each method controls:

每种方法都控制着三件事:

  • When an instruction loads into context;指令何时加载进上下文;
  • Whether it persists through long sessions (compaction behavior); and它能否在长会话中留存(即压缩 compaction 时的行为);以及
  • How much authority it carries.它携带多大的权威。

The table below provides a quick summary of key differences across each method while the post provides additional detail and decision framework for determining where each of your Claude instructions belongs.

下表快速汇总了各方法之间的关键差异,而正文会给出更多细节,以及一套判断"你的每条 Claude 指令该放在哪里"的决策框架。

Method方法 When it's loaded何时加载 Compaction behavior压缩时的行为 Context cost上下文开销 When to use何时使用
CLAUDE.md
(root)
Session start; stays in context for the entire session会话开始时;整段会话常驻上下文 Memoized. Read once and cached for the session; cache cleared and re-read after compaction记忆化:读一次、整段会话缓存;压缩后清缓存并重读 High. Every line costs tokens whether relevant or not高。无论相关与否,每行都费 token Build commands, directory layout, monorepo structure, coding conventions, team norms构建命令、目录结构、monorepo 布局、编码约定、团队规范
CLAUDE.md
(subdirectory)
On-demand, when Claude reads a file under that subdirectory按需:当 Claude 读取该子目录下的文件时 Lost until that subdirectory is touched again丢失,直到该子目录再次被触及 Low. Only consumes context when the relevant subdirectory is being worked on低。仅在处理相关子目录时才占用上下文 Conventions specific to a subdirectory某个子目录专属的约定
Rules Session start (user-level rules) or only when matching files are touched (path-scoped)会话开始(用户级规则),或仅当匹配文件被触及时(路径限定) Re-injected on compaction压缩时重新注入 Medium. Always-on unless path-scoped中。除非做路径限定,否则常驻 Specific constraints or conventions (e.g., all API handlers must validate input with Zod)具体的约束或约定(如:所有 API handler 必须用 Zod 校验输入)
Skills Name and description at session start; full body loads when the skill is invoked会话开始时加载名称和描述;完整正文在 skill 被调用时才加载 Invoked skills re-injected up to a shared budget; oldest dropped first已调用的 skill 在共享预算内重新注入;最早的先被丢弃 Low. Full body loads only when invoked; subject to a shared token budget across invoked skills低。完整正文仅在调用时加载;受所有已调用 skill 的共享 token 预算约束 Procedural workflows (deploy or release checklists)流程性工作流(部署或发版清单)
Subagents Name, description, and tool list at session start; body loads only when called via the Agent tool会话开始时加载名称、描述和工具清单;正文仅在经 Agent 工具调用时加载 Only the final message (summary plus metadata) returns to the main session只有最终消息(结论 + 元数据)返回主会话 Low. Zero cost in main context until called; runs in its own isolated context window低。被调用前在主上下文里零开销;在自己的隔离上下文窗口中运行 Running work in parallel or side tasks that should run in isolation and return only a summary (deep search, log analysis, dependency audit)并行工作,或该隔离运行、只回结论的旁支任务(深度搜索、日志分析、依赖审计)
Hooks Fire on lifecycle events在生命周期事件上触发 Bypass compaction entirely完全绕过压缩 Low. Configuration lives outside main context; some output may return (e.g., blocking errors)低。配置活在主上下文之外;部分输出可能回传(如阻断错误) Deterministic automation: run linters, post to Slack on completion, block commands, back up chat history on PreCompact确定性自动化:跑 linter、完成时发 Slack、拦截命令、在 PreCompact 时备份聊天记录
Output styles Session start; injected into the system prompt会话开始;注入系统提示 Never compacted永不被压缩 High. Occupies context window, but overwrites default system prompt高。占用上下文窗口,但会覆盖默认系统提示 Significant role changes (code assistant to general assistant)重大的角色转变(从编码助手变成通用助手)
Appending the
system prompt
Session start; passed as a CLI flag会话开始;以 CLI 参数传入 Never compacted; applies only to that invocation永不被压缩;仅对该次调用生效 Moderate. Cached after first request in a session中等。会话内第一次请求后被缓存 Tone, response length, formatting preferences语气、回复长度、格式偏好

There are seven ways to customize Claude Code's behavior: CLAUDE.md files for always-on project context, rules for hard constraints, skills for reusable procedures, subagents for delegated work, hooks for deterministic automation, and output styles or system-prompt appends for global changes. Each method trades context cost against authority—choosing the right one is most of the work.

定制 Claude Code 行为有七种方式:CLAUDE.md 文件用于常驻的项目上下文,rules 用于硬性约束,skills 用于可复用的流程,subagents 用于委派出去的工作,hooks 用于确定性自动化,而 output styles 或系统提示追加用于全局性改动。每种方法都在上下文开销与权威度之间权衡——选对方法本身就是大半的功夫。

CLAUDE.md files

CLAUDE.md 文件 · 常驻的项目事实

CLAUDE.md is a markdown file at the root of your project. It loads into context at session start and stays there for the entire session.

CLAUDE.md 是放在项目根目录的一个 markdown 文件。它在会话开始时加载进上下文,并在整段会话中一直留在那里。

Build commands, directory layout, monorepo structure, coding conventions, and team norms all fit naturally here.

构建命令、目录布局、monorepo 结构、编码约定和团队规范,都很自然地属于这里。

There are two types, and they load differently:

它有两种类型,加载方式不同:

  • Always loaded: The first type is a root CLAUDE.md file, either in a shared repository and/or saved locally for your personal preferences specific to a project. All these files load at session start, and won't get lost or degraded across long sessions. When Claude Code compacts the conversation, it re-reads these files.始终加载:第一种是根目录的 CLAUDE.md 文件,可以在共享仓库里,也可以本地保存你针对某项目的个人偏好。这些文件都在会话开始时加载,不会在长会话中丢失或衰减。当 Claude Code 压缩对话时,它会重新读取这些文件。
  • On-demand: CLAUDE.md files in subdirectories below the folder where you initialized the session. For example, app/api/CLAUDE.md loads when Claude reads a file under app/api, not at session start. It shares the compaction behavior of path-scoped rules: gone until that subdirectory is touched again.按需加载:位于你初始化会话的目录之下的子目录里的 CLAUDE.md。例如 app/api/CLAUDE.md 会在 Claude 读取 app/api 下的文件时加载,而非会话开始时。它的压缩行为和路径限定规则一样:一旦压缩就消失,直到该子目录再次被触及。
Subdirectory CLAUDE.md loading
All subdirectory CLAUDE.md files below the cwd load when Claude reads a file within that directory.当前工作目录之下的所有子目录 CLAUDE.md,都会在 Claude 读取该目录内文件时加载。

In a shared repository, CLAUDE.md grows the way any unowned config file does: every team appends its own instructions and nothing gets deleted. The cost compounds at scale.

在共享仓库里,CLAUDE.md 会像任何"无主"配置文件一样膨胀:每个团队都往里追加自己的指令,却没人删东西。规模一大,代价就复利式累积。

Every line loads into every session for every engineer working in the repo, whether it's relevant to their task or not. This consumes tokens and dilutes adherence to the instructions that actually matter. As the file grows, push team-specific conventions into path-scoped rules and procedures into skills, where they load only when relevant.

每一行都会为仓库里每一位工程师的每一次会话加载,无论它是否与其任务相关。这既消耗 token,又稀释了对真正重要的指令的遵循度。随着文件变大,把团队专属的约定挪进路径限定的 rule、把流程挪进 skill——它们只在相关时才加载。

Tip · 提示

Keep CLAUDE.md under 200 lines, give it an owner, and review changes to it like code. The content itself should follow the same rules as any prompt: writing effective prompts means being explicit, explaining the why behind constraints, and showing examples.

把 CLAUDE.md 控制在 200 行以内,给它指定一个负责人,并像 review 代码一样 review 它的改动。内容本身应遵循和任何 prompt 相同的准则:写出有效的 prompt 意味着要明确、解释约束背后的原因、并给出示例。

Think of this file as giving Claude an overview of your codebase, or as an index pointing to other files where Claude can find more information as needed.

把这个文件想成是给 Claude 一份你代码库的概览,或者一个索引——指向其他文件,让 Claude 按需去那里找更多信息。

In monorepos, give each team's directory its own subdirectory CLAUDE.md so teams only load their own conventions, and developers can use the claudeMdExcludes setting to skip files from teams whose code they never touch.

在 monorepo 里,给每个团队的目录配一份自己的子目录 CLAUDE.md,这样各团队只加载自己的约定;开发者还能用 claudeMdExcludes 设置,跳过那些自己从不碰其代码的团队的文件。

For standards that must apply to every repository in the organization — security policies, compliance requirements — a centrally managed CLAUDE.md can be deployed to developer machines via MDM or config management, and it can't be excluded by individual settings.

对于必须适用于组织内每一个仓库的标准——安全策略、合规要求——可以通过 MDM 或配置管理把一份集中管理的 CLAUDE.md 部署到开发者机器上,而且它无法被个人设置排除掉。

More on setting up CLAUDE.md in our blog post, CLAUDE.md files: Customizing Claude Code for your codebase.

关于如何配置 CLAUDE.md,更多内容见我们的博客文章 《CLAUDE.md files: Customizing Claude Code for your codebase》

Rules

Rules · 硬性约束,可按路径限定

Rules are markdown files in .claude/rules/ that give Claude specific constraints or conventions.

Rules 是放在 .claude/rules/ 里的 markdown 文件,给 Claude 特定的约束或约定。

Unscoped rules behave like CLAUDE.md in that they are always loaded at session start and get re-injected on compaction. This can waste tokens by loading context even when it's not relevant for the task at hand.

未限定作用域的 rule 表现得和 CLAUDE.md 一样:总是在会话开始时加载,并在压缩时重新注入。这可能浪费 token——即便对手头任务并不相关,也会把上下文加载进来。

Path-scoped rules allow you to load rule instructions only when they are relevant by adding a paths field that controls when they load.

路径限定的 rule 让你只在相关时才加载规则指令——通过添加一个 paths 字段来控制它们何时加载。

For example: a rule scoped to src/api/** stays out of context during a docs-only session. It would only be loaded whenever Claude reads files within that src/api/ directory.

例如:一个限定到 src/api/** 的规则,在纯写文档的会话里就不会进入上下文。它只会在 Claude 读取 src/api/ 目录内的文件时才被加载。

Here's what that looks like:

它长这样:

---
paths:
  - "src/api/**"
  - "**/*.handler.ts"
---
All API handlers must validate input with Zod before processing.
Tip · 提示

A file-specific constraint, like "migrations are append-only," fits best as a rule placed in your paths: frontmatter. Reach for a path scoped rule over a nested CLAUDE.md file when the instruction regards a cross-cutting concern or file that appears in multiple (but not all) corners of the codebase.

针对特定文件的约束,比如"迁移文件只能追加(append-only)",最适合做成一条放在 paths: frontmatter 里的 rule。当指令涉及的是横切关注点、或出现在代码库多个(但非全部)角落的文件时,优先选路径限定 rule,而不是嵌套的 CLAUDE.md 文件。

Skills

Skills · 按需加载的可复用流程

Skills live in .claude/skills/ as folders of instructions, scripts, and resources that Claude loads dynamically. Each skill has a SKILL.md file with a name, description, and body.

Skills 以文件夹形式住在 .claude/skills/ 里,包含指令、脚本和资源,由 Claude 动态加载。每个 skill 都有一个 SKILL.md 文件,含名称、描述和正文。

Only the name and description load at session start; the full body loads when Claude invokes the skill, either through a slash command (/code-review) or by auto-matching the task.

会话开始时只加载名称和描述;完整正文在 Claude 调用该 skill 时才加载——要么通过斜杠命令(/code-review),要么通过与任务自动匹配。

Skills triggered via system prompt
Skills are triggered via your system prompt.Skill 是通过你的系统提示来触发的。

For example, /code-review is a built-in skill that reviews your current diff and reports its findings without editing files. The skill defines the playbook so Claude follows the same structured approach every time you invoke it.

例如,/code-review 是一个内置 skill,它会审查你当前的 diff 并报告发现,但不改动文件。这个 skill 定义了一套"打法",使得你每次调用时 Claude 都遵循相同的结构化方法。

On compaction, Claude Code re-injects invoked skills up to a total budget across all invoked skills. If you've invoked many skills during a session, the oldest ones drop first.

压缩时,Claude Code 会在"所有已调用 skill"的一个总预算范围内重新注入这些 skill。如果你在一次会话里调用了很多 skill,最早的会先被丢弃。

Tip · 提示

Instructions that are procedural, like deploy workflows, release checklists, or review processes, belong in a skill rather than in CLAUDE.md.

流程性的指令——比如部署工作流、发版清单或审查流程——应该放进 skill,而不是 CLAUDE.md。

Claude Code ships with skills, but you can also write your own custom skills. Our complete guide to building skills for Claude shows you how.

Claude Code 自带一些 skill,但你也可以写自己的自定义 skill。我们的《构建 Claude skill 完整指南》会教你怎么做。

Subagents

Subagents · 隔离运行、只回结论的委派

Subagents are markdown files in .claude/agents/ that define isolated assistants for specific side tasks. Each file uses YAML frontmatter (name, description, plus optional fields for model and tool access) followed by a body that becomes that subagent's system prompt.

Subagents 是放在 .claude/agents/ 里的 markdown 文件,为特定的旁支任务定义隔离的助手。每个文件用 YAML frontmatter(名称、描述,外加 model 和工具访问权限等可选字段),后面跟一段正文——这段正文会成为该 subagent 的系统提示。

Subagents are similar to skills in that the name, description, and tool list load at session start, but the larger context within the body of the agent doesn't auto-invoke. Claude calls them via the Agent tool, passing in a prompt string.

Subagent 与 skill 相似之处在于:名称、描述和工具清单在会话开始时加载,但 agent 正文里那一大段上下文不会自动触发。Claude 通过 Agent 工具调用它们,传入一个 prompt 字符串。

Claude Code context window
Claude Code's context window holds everything Claude knows about your session.Claude Code 的上下文窗口装着 Claude 对你这次会话所知的一切。

The interactive timeline here walks through what loads and when.

这里的交互式时间线会带你走一遍:什么在何时加载。

Not only does the larger instructional context within the body of the subagent not auto-invoke, it never enters the parent conversation at all.

subagent 正文里那一大段指令上下文,不仅不会自动触发,它压根就从不进入父对话。

The subagent then runs in its own fresh context window, and the only thing that returns to your main session is the subagent's final message (often the aggregated result of many subtasks) plus metadata.

于是这个 subagent 在自己全新的上下文窗口里运行,而唯一返回到你主会话的,只有它的最终消息(通常是许多子任务的汇总结果)加上元数据。

This pattern scales: subagents can nest up to five levels deep, and dynamic workflows orchestrate tens to hundreds of background agents without requiring you to specify each detail of the subagent architecture. The orchestration plan and intermediate results live in script variables rather than in Claude's context window, which enables scale without losing instructional fidelity.

这套模式可以规模化:subagent 最多能嵌套五层,而 dynamic workflows(动态工作流)能编排数十到数百个后台 agent,而无需你逐一指定 subagent 架构的每个细节。编排方案和中间结果活在脚本变量里,而不是 Claude 的上下文窗口里——这让规模化得以实现,又不损失指令的保真度。

Tip · 提示

That isolation is one of the main reasons to reach for a subagent instead of a skill. Use a subagent when a side task like deep search, a log analysis pass, or a dependency audit would clutter your main conversation with intermediate results you won't reference again. Use a skill when you want the procedure to play out inside the main thread so you can see and steer each step.

这种隔离,正是"选 subagent 而非 skill"的主要理由之一。当一个旁支任务——如深度搜索、一遍日志分析、或一次依赖审计——会用你不再引用的中间结果把主对话搞乱时,用 subagent。而当你想让流程在主线程里展开、以便你能看到并引导每一步时,用 skill。

Hooks

Hooks · 确定性触发的自动化

Hooks are user-defined commands, HTTP endpoints, or LLM prompts that provide more deterministic control over Claude's behavior by firing on specific events in Claude's lifecycle like file edits, tool calls, or session start.

Hooks 是用户自定义的命令、HTTP 端点或 LLM prompt,它们通过在 Claude 生命周期中的特定事件(如文件编辑、工具调用、会话开始)上触发,来对 Claude 的行为提供更具确定性的控制。

Hook lifecycle events map
A map of events in a Claude Code session when a hook can fire.一张 Claude Code 会话中"hook 可触发时机"的事件地图。

You register hooks in settings.json, managed policy settings, or skill/agent frontmatter.

你可以在 settings.json、托管策略设置、或 skill / agent 的 frontmatter 里注册 hook。

There are several types of hooks: command, HTTP, mcp_tool, prompt, and agent. All hooks are deterministically triggered. The first three execute deterministically while the latter two, prompt and agent, use Claude's judgment rather than a set of rules to determine the output.

hook 有几种类型:command、HTTP、mcp_tool、prompt 和 agent。所有 hook 都是确定性触发的。前三种确定性地执行,而后两种——prompt 和 agent——用的是 Claude 的判断,而非一套规则,来决定输出。

Hooks have low context costs because the configuration or instruction lives outside the main context window. The harness runs the handler (command, http, mcp_tool) or makes model calls with separate windows (prompt, agent) depending on the hook type.

hook 的上下文开销很低,因为配置或指令活在主上下文窗口之外。视 hook 类型而定,harness 要么运行处理器(command、http、mcp_tool),要么用独立的窗口发起模型调用(prompt、agent)。

Some hooks may have the output saved to the main context window. For example, a blocking hook's standard error is saved within context so Claude knows why the call was denied.

有些 hook 的输出可能会被保存到主上下文窗口。例如,一个阻断型 hook 的标准错误(stderr)会被保存进上下文,好让 Claude 知道那次调用为何被拒。

But most hooks won't have the output saved to the main window unless the configuration explicitly returns it. If you backed up your chat history into another file for later reference before compaction using the PreCompact event, Claude wouldn't know which file had the chat history saved.

但大多数 hook 除非配置里明确返回,否则输出不会被保存到主窗口。如果你用 PreCompact 事件在压缩前把聊天记录备份到另一个文件里以备日后查阅,Claude 是不会知道哪个文件存了聊天记录的。

This makes these hook types fundamentally different from CLAUDE.md, rules, and skills. You can learn more in our post how to configure hooks.

这使得这些 hook 类型与 CLAUDE.md、rules 和 skills 有着根本区别。更多内容见我们的文章《如何配置 hook》

Tip · 提示

Use hooks for anything that should happen deterministically: running linters after edits, posting to Slack on completion, or blocking specific commands before they execute. A PreToolUse hook can inspect any tool call and exit code 2 to deny it.

凡是应当确定性发生的事,都用 hook:编辑后跑 linter、完成时发 Slack、或在特定命令执行前拦截它。一个 PreToolUse hook 可以检查任何工具调用,并以退出码 2 来拒绝它。

They have low context cost because they are code that the harness runs rather than instructions to Claude that get loaded into context.

它们上下文开销低,因为它们是 harness 运行的代码,而不是加载进上下文、发给 Claude 的指令。

Output styles

Output styles · 权威最高,会覆盖默认系统提示

Output styles are files in .claude/output-styles/ that inject instructions into the system prompt. They never get compacted, load at the start of every session, and are cached after the first request within a session, meaning they have a moderate context cost.

Output styles 是放在 .claude/output-styles/ 里、把指令注入系统提示的文件。它们永不被压缩,在每次会话开始时加载,并在会话内第一次请求后被缓存——也就是说上下文开销中等。

Because they sit in the system prompt, output styles carry the highest instruction-following weight of any method that we've covered so far and should be used judiciously.

由于它们位于系统提示中,output style 在我们目前讲过的所有方法里,携带着最高的"指令遵循权重",因此应当审慎使用。

Changes to the output style will replace the default output style (unless you set keep-coding-instructions: true in the style's frontmatter).

对 output style 的改动会替换掉默认的 output style(除非你在该风格的 frontmatter 里设置 keep-coding-instructions: true)。

In Claude Code, this would remove instructions that tell Claude it is helping users with software engineering tasks and contains other critical default instructions such as:

在 Claude Code 里,这会移除那些告诉 Claude"你在帮用户完成软件工程任务"的指令,以及其他关键的默认指令,比如:

  • How to scope changes;如何界定改动的范围;
  • When to add or omit code comments;何时添加或省略代码注释;
  • What to do about security concerns; and遇到安全问题该怎么办;以及
  • Verification habits like running tests before declaring work complete.诸如"宣布完成前先跑测试"这类验证习惯。

By default, a custom output style drops all of this and Claude Code becomes more of a general assistant than a software engineer assistant.

默认情况下,一个自定义 output style 会把这些全部丢弃,于是 Claude Code 更像一个通用助手,而非软件工程助手。

Tip · 提示

Before writing a custom output style, check the built-in styles. Proactive, Explanatory, and Learning cover the most common needs (autonomy, teaching mode, collaborative coding) without you having to maintain a style file.

在写自定义 output style 之前,先看看内置的几种。ProactiveExplanatoryLearning 已覆盖最常见的需求(自主执行、教学模式、协作式编码),你无需自己维护一个风格文件。

Appending the system prompt

追加系统提示 · 只做加法,只对本次调用生效

An alternative to modifying output styles is the append-system-prompt flag. Whereas modifying output style files can have large, unintended changes to Claude's behavior, the append flag is only additive to the original system prompt. It doesn't modify Claude's role; it just adds instructions to its default role.

修改 output style 之外的另一个选择,是 --append-system-prompt 参数。修改 output style 文件可能对 Claude 的行为造成大而意外的改动,而 append 参数只是在原始系统提示上做加法。它不修改 Claude 的角色,只是在其默认角色之上添加指令。

It is also passed at invocation time, and only applies to that invocation, rather than persisted as a file across sessions.

它也是在调用时传入,并且只对该次调用生效,而不是作为文件跨会话持久保存。

Appending the system prompt can have a higher context cost compared to other methods of passing instructions. It increases input tokens, though prompt caching reduces this cost after the first request in a session. Instructing Claude to use a more verbose or longer style also increases output tokens.

相比其他传递指令的方法,追加系统提示的上下文开销可能更高。它会增加输入 token,不过在会话第一次请求之后,prompt 缓存会降低这一成本。让 Claude 采用更啰嗦或更长的风格,也会增加输出 token。

Tip · 提示

Appending the system prompt is best for adding specific coding standards, output formatting, or domain-specific knowledge. Keep in mind that appending the system prompt has diminishing returns for adherence. Generally, the more instructions you provide using this method, the less strictly Claude will follow them, particularly if any contradict.

追加系统提示最适合用来添加特定的编码规范、输出格式或领域知识。但要记住,它在"遵循度"上有边际递减效应。一般来说,你用这种方法提供的指令越多,Claude 遵循得就越不严格——尤其当其中有相互矛盾之处时。

Chapter 02

When to use each method

如何取舍 · 五个"该换地方"的信号
CLAUDE.md 的误用信号 · 护栏要确定性 · 流程归 skill

If you find yourself doing one of the following, you may want to consider an alternative location for your instructions:

如果你发现自己在做下面某件事,或许该考虑把指令换个地方放:

"Every time X, always do Y" in CLAUDE.md. If the behavior should happen reliably, like running prettier after every edit or posting to Slack on completion, use a hook in settings.json instead. The model choosing to run a formatter is different from the formatter running automatically.

在 CLAUDE.md 里写"每次 X,总是做 Y"。如果这个行为应当可靠地发生——比如每次编辑后跑 prettier、或完成时发 Slack——那就改用 settings.json 里的 hook。"模型选择去跑一个格式化工具"和"格式化工具自动运行"是两码事。

"Never do this" in CLAUDE.md. When there's something that absolutely must not happen, an instruction is the wrong tool. Claude will follow the instruction most of the time, but when under pressure, in a long session or an ambiguous situation, or due to a prompt injection in a file accessed as part of the task, the model can fail to follow a prompted rule. A real guardrail needs to be deterministic, and the enforcement methods are hooks and permissions. A PreToolUse hook can inspect a call and exit with code 2 to block it. Managed settings go further: they are admin-deployed, cannot be overridden by a user's local config, and are the only way to enforce a deterministic, organization-wide guardrail.

在 CLAUDE.md 里写"绝不能做这个"。当有些事绝对不能发生时,"一条指令"就是错误的工具。Claude 大多数时候会遵循指令,但在压力之下、长会话中、模糊情境里,或因任务中访问的某个文件里存在 prompt 注入,模型可能就不遵循一条被 prompt 出来的规则了。真正的护栏必须是确定性的,而强制手段是 hookspermissions(权限)。一个 PreToolUse hook 可以检查一次调用,并以退出码 2 来阻断它。托管设置(Managed settings)更进一步:它们由管理员部署、无法被用户本地配置覆盖,是强制实施"确定性、组织级护栏"的唯一途径。

A 30-line procedure in CLAUDE.md. Procedures belong in skills. CLAUDE.md is for facts Claude should hold all the time: build commands, monorepo layout, team conventions. A deployment runbook or a security review checklist should live in .claude/skills/, where the body loads only when invoked.

在 CLAUDE.md 里写一段 30 行的流程。流程应属于 skill。CLAUDE.md 是给 Claude 一直持有的"事实":构建命令、monorepo 布局、团队约定。一份部署手册或安全审查清单应该住在 .claude/skills/ 里——那里的正文只在被调用时才加载。

An API-specific rule without paths. If a rule only applies to src/api/**, scoping it with paths: keeps it out of context during unrelated work. An unscoped rule is mechanically identical to putting the content in CLAUDE.md: always loaded, always costing tokens.

一条只针对 API、却不带 paths 的 rule。如果一条规则只适用于 src/api/**,用 paths: 限定它,能让它在无关工作期间不进入上下文。一条不限定作用域的 rule,机制上等同于把内容放进 CLAUDE.md:永远加载、永远费 token。

Writing personal preferences to a project-level CLAUDE.md file. All file-based methods have a user-level counterpart loaded for every Claude Code session regardless of which repo you're in. Use local files for personal preferences (always use semantic commit messages). Keep project-level files for preferences that are team-wide but specific to a given codebase.

把个人偏好写进项目级的 CLAUDE.md 文件。所有基于文件的方法都有一个用户级的对应版本——无论你在哪个仓库,它都会为每次 Claude Code 会话加载。个人偏好(比如"始终使用语义化提交信息")用本地文件;项目级文件则留给那些团队通用、但针对某个特定代码库的偏好。

Chapter 03

Getting started with Claude Code customization

上手 Claude Code 定制 · 从几项做起,再打包成插件
best practices · 打包成 plugin 共享

You can find more tips and patterns for getting the most out of Claude Code, from configuring your environment to scaling across parallel sessions, in our best practices for Claude Code documentation.

想把 Claude Code 用到极致——从配置环境到跨并行会话扩展——更多技巧和模式都在我们的 Claude Code 最佳实践文档里。

Once you have a few of these working, you can bundle many of them (skills, subagents, hooks, output styles) as a plugin to share a coherent setup across teammates or projects.

一旦你把其中几项跑通了,就可以把它们中的许多(skills、subagents、hooks、output styles)打包成一个 plugin(插件),在队友或项目之间共享一套连贯的配置。

This article was written by Michael Segner, member of Anthropic staff.
本文由 Anthropic 员工 Michael Segner 撰写。