Anthropic 官方提供七种定制 Claude Code 行为的方法(CLAUDE.md、Rules、Skills、Subagents、Hooks、Output Styles、System Prompt Appending),每种方法在加载时机、压缩行为和上下文成本上有所不同,需根据具体需求选择合适的方法。
适合的任务:需要系统化理解 Claude Code 定制机制的场景;需要决定在哪里放置项目规范、团队约定、自动化流程等
不适合的任务:需要深度技术实现细节的场景(本文更偏向决策框架)
适用的模型版本:Claude Opus 4.7(通过 Claude Code 使用)
适用的客户端、Agent 或 API:Claude Code CLI
推荐的推理档位和参数:N/A(本文讨论定制机制,不涉及具体任务)
| 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 | Build commands, directory layout, monorepo structure, coding conventions, team norms |
| CLAUDE.md (subdirectory) | On-demand, when Claude reads a file under that subdirectory | 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) |
| Skills | Name and description at session start; full body loads when the skill is invoked | Invoked skills re-injected up to a shared budget; oldest dropped first | Low. Full body loads only when invoked; subject to a shared token budget across invoked skills | Procedural workflows (deploy or release checklists) |
| Subagents | Name, description, and tool list at session start; body loads only when called via the Agent tool | 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 |
| 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 | Never compacted; applies only to that invocation | Moderate. Cached after first request in a session | Tone, response length, formatting preferences |
两种类型:
Always loaded: 根目录 CLAUDE.md,在会话开始时加载,跨长会话不会丢失或降级
On-demand: 子目录 CLAUDE.md,仅当 Claude 读取该子目录下的文件时加载
关键建议:
保持 CLAUDE.md 在 200 行以内
指定所有者,像代码一样审查变更
将团队特定约定推送到 path-scoped rules
将流程推送到 skills
在 monorepos 中,给每个团队的目录自己的子目录 CLAUDE.md
Monorepo 配置:
{
"claudeMdExcludes": ["teams/other-team/**"]
}---
paths:
- "src/api/**"
- "**/*.handler.ts"
---
All API handlers must validate input with Zod before processing.选择 CLAUDE.md (root) 当:
构建命令、目录布局、monorepo 结构
编码约定、团队规范
需要在整个会话中始终可用
选择 CLAUDE.md (subdirectory) 当:
约定仅适用于特定子目录
希望减少上下文成本
选择 Rules 当:
需要硬约束(如"所有 API 处理器必须用 Zod 验证输入")
需要 path-scoped 约束
选择 Skills 当:
需要可复用的流程(部署清单、发布检查)
希望按需加载以节省上下文
选择 Subagents 当:
需要并行运行工作
需要在隔离上下文中运行任务并仅返回摘要(深度搜索、日志分析、依赖审计)
选择 Hooks 当:
需要确定性自动化(运行 linter、阻止命令、备份聊天历史)
需要在生命周期事件上触发
选择 Output styles 当:
需要显著角色变更(代码助手到通用助手)
选择 Appending the system prompt 当:
需要设置语调、响应长度、格式偏好
评估当前项目:识别哪些规范需要始终可用(放入 CLAUDE.md root),哪些仅在特定场景下需要(放入子目录或 rules)
审查现有 CLAUDE.md:如果超过 200 行,考虑将团队约定推送到 rules,流程推送到 skills
为 Monorepo 配置:为每个团队创建子目录 CLAUDE.md,使用 claudeMdExcludes 排除不相关的团队文件
创建 Rules:为硬约束创建 path-scoped rules
创建 Skills:为可复用流程创建 skills
配置 Hooks:为自动化任务配置 hooks(如运行 linter、阻止危险命令)
测试压缩行为:在长会话中测试各种方法的压缩行为,确保关键规范不会丢失
文章明确列出七种方法及其加载时机、压缩行为、上下文成本和使用时机
强调 CLAUDE.md 应保持 200 行以内,并指定所有者
提供 Rules 的 YAML frontmatter 格式示例
详细说明每种方法的优缺点和适用场景
强调 Skills 使用共享 token 预算,最旧的最先丢弃
说明 Subagents 在主上下文中零成本,仅在调用时加载
本文提供决策框架,不提供具体实现细节
七种方法可以组合使用,需要根据具体项目需求权衡
CLAUDE.md 的 always-loaded 类型会在每次会话中消耗 token,即使不相关
Skills 的共享 token 预算可能导致最旧的 skills 被丢弃
Hooks 绕过压缩,但配置在主上下文之外
官方文章:"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."
"Keep CLAUDE.md under 200 lines, give it an owner, and review changes to it like code."
"Each method trades context cost against authority."
Claude Opus 4.7