Claude Code 技能(skill)是一个包含 SKILL.md 文件的文件夹。该文件包含 Claude Code 针对特定任务(例如编写提交信息、审查拉取请求或遵循团队编码规范)所遵循的指令。本指南介绍了文件格式、技能存放位置、安装与共享方式,以及当某条规则必须始终遵守时,如何通过钩子(hook)来支持技能。
详细信息来自 2026-11-11 阅读的 Claude Code 文档中的三个页面:Skills、Hooks reference 和 Install and manage plugins。
技能与自定义命令
自定义命令已合并到技能中。位于 .claude/commands/deploy.md 的文件和位于 .claude/skills/deploy/SKILL.md 的技能都会创建 /deploy 命令,且工作方式相同。现有的命令文件将继续有效。技能增加了一个用于存放支持文件的文件夹、用于控制调用权限的元数据(frontmatter),以及在任务匹配时由 Claude 自动加载的功能。请使用技能来处理新工作。如果技能和命令文件同名,则优先运行技能。
Claude Code 会将每个技能的名称和描述保留在上下文中,以便 Claude 了解有哪些技能可用。只有在调用技能时,才会加载全文。你可以通过输入 /skill-name 来调用技能。当你的请求与技能描述匹配时,Claude 也会主动调用它。
技能存放位置
| 位置 | 路径 | 可用范围 |
|---|---|---|
| 个人 | ~/.claude/skills/<skill-name>/SKILL.md |
本机上的所有项目 |
| 项目 | .claude/skills/<skill-name>/SKILL.md |
此仓库中的会话。提交后可与团队共享 |
| 嵌套项目 | <subdir>/.claude/skills/<skill-name>/SKILL.md |
该子目录及以下目录中的会话,适用于 monorepo |
| 插件 | <plugin>/skills/<skill-name>/SKILL.md |
启用插件的任何地方,调用方式为 /plugin-name:skill-name |
| 企业 | 管理设置目录中的 .claude/skills/<skill-name>/SKILL.md |
组织部署该配置的机器上的所有用户 |
| 遗留命令 | .claude/commands/<name>.md |
仍然有效并创建相同的 /name |
如果个人技能和项目技能重名,则运行个人技能。企业技能优先级高于个人技能。插件技能永远不会冲突,因为它们带有插件名称作为前缀。
编写你的第一个技能
以下示例是一个用于提交信息的项目技能。创建 .claude/skills/commit/SKILL.md:
---
name: commit
description: Write a conventional commit message for the staged changes and commit them.
disable-model-invocation: true
allowed-tools: Bash(git commit *) Bash(git status *)
---
## Staged changes
!`git diff --staged`
## Steps
1. Read the staged changes above. If nothing is staged, stop and say so.
2. Pick a type: feat, fix, refactor, docs, test or chore.
3. Write the message as `type(scope): description`.
- The scope is the package or module name.
- The description is in imperative mood, lowercase, with no period.
- The body explains why the change was made, not what changed.
4. If the changes touch more than one scope, say so and suggest separate commits.
5. Run `git commit -m "<message>"`.
## Rules
- Never use `--no-verify`.
- Never amend a commit that was already pushed.
- If a check fails, fix the cause and commit again.
## Examples
- `feat(billing): add stripe webhook handler`
- `fix(auth): handle expired refresh tokens`
- `refactor(api): extract rate limiter to shared package`
输入 /commit,Claude Code 就会运行该工作流。以 ! 开头的行会在 Claude 读取技能之前运行 git diff --staged,因此暂存区的差异内容已经包含在提示词中。
元数据位于两个 --- 行之间,且起始行必须是文件的第一行。上述示例中使用了这些字段,还有一些你经常会用到的字段:
| 字段 | 作用 |
|---|---|
name |
/ 菜单中的命令名称。默认为文件夹名称。 |
description |
技能的功能及使用场景。Claude 用它来决定何时加载技能。将主要用例放在最前面。 |
disable-model-invocation |
设为 true 时,仅限你手动调用该技能。用于具有副作用的操作,如提交和部署。 |
user-invocable |
设为 false 时,仅 Claude 可以调用该技能。用于后台知识。 |
allowed-tools |
Claude 在调用技能的轮次中无需权限提示即可使用的工具。它不会限制其他工具。 |
paths |
限制自动加载范围的 Glob 模式,仅匹配特定文件时加载。 |
hooks |
调用技能时 Claude Code 注册的钩子。请参阅下方的钩子部分。 |
所有字段均为可选,文档建议必须包含 description。Claude Code 会忽略无法识别的字段且不报错,因此请检查拼写。运行 claude plugin validate .claude/skills 可以查找元数据解析失败的 SKILL.md。完整列表请参考 frontmatter reference。
技能设计模式
文档建议将 SKILL.md 控制在 500 行以内,并将细节移至单独的文件中。以下三种模式涵盖了大多数技能。
检查清单模式
适用于包含多个验证步骤的任务。
# Pre-Deploy Checklist
Before deploying, verify each item:
- [ ] `pnpm typecheck` passes
- [ ] `pnpm test` passes
- [ ] No console.log statements in production code
- [ ] Environment variables are documented in .env.example
- [ ] Database migrations are reversible
If any check fails, stop and report the issue. Do not deploy.
决策树模式
适用于处理方法取决于具体原因的情况。
# Bug Fix Workflow
1. Reproduce the bug. Find or write a failing test.
2. Identify the root cause:
- Type error: fix the type definition at the source.
- Race condition: add locking or sequencing.
- Missing validation: add schema validation at the boundary.
- Logic error: fix it and add a regression test.
3. Check that the fix does not break existing tests.
模板模式
适用于获取一致的输出。以下路径仅为示例,请使用你自己的路径。
# New API Endpoint
## File structure
- Route handler: `src/routes/{resource}/{action}.ts`
- Schema: `src/schemas/{resource}.ts`
- Test: `src/routes/{resource}/__tests__/{action}.test.ts`
## Required elements
- A request validation schema
- Authentication middleware
- Structured error responses
安装与共享技能
有三种方式将技能放入机器中:
- 个人: 将技能文件夹复制到
~/.claude/skills/。 - 项目: 将文件夹提交到仓库中的
.claude/skills/。团队成员拉取代码后即可获得。 - 插件: 安装一个在其
skills/文件夹中包含技能的插件。
要安装插件,请在 Claude Code 中运行 /plugin install。此示例安装了 Anthropic 官方市场中的 commit-commands:
/plugin install commit-commands@claude-plugins-official
该命令会打开插件详情,以便你查看其添加的内容并选择作用域:用户、项目或本地。完成后,插件的技能会出现在 / 菜单中,显示为 /<plugin>:<skill>,此处为 /commit-commands:commit。由于带有前缀,它不会覆盖你自己的 /commit 技能。Anthropic 官方市场会在你首次启动交互式会话时添加。其他市场需要额外一步,使用 GitHub 的 owner/repo 或 git URL:
/plugin marketplace add your-org/plugins
/plugin install deploy-helper@your-org
@ 后面的名称是市场注册的名称。在 Shell 中,claude plugin install <plugin>@<marketplace> --scope project 可以在不启动会话的情况下执行相同操作,而 claude plugin list 则显示已安装的插件。请阅读 plugin install guide 以了解作用域、更新和移除方法。
请像对待代码一样对待第三方技能和插件。插件以你的用户权限运行,而技能可以通过 allowed-tools 预先批准工具。安装前请务必阅读它们。若要在整个组织内共享技能,请通过管理设置进行部署。
使用钩子强制执行技能
技能只是指导,Claude 可能会跳过某个步骤。如果某条规则必须始终遵守,请将其放入钩子中。无论 Claude 是否遵循技能,Claude Code 都会在每次触发事件时运行钩子。
钩子绑定到 Claude Code 事件,而不是 git 事件。Claude Code 没有 pre-commit 或 post-commit 事件。要对提交操作采取行动,请挂载 Bash 工具并过滤命令。以下是你最常使用的事件:
| 事件 | 触发时机 |
|---|---|
SessionStart |
会话开始或恢复时 |
UserPromptSubmit |
你提交提示词时,在 Claude 处理之前 |
PreToolUse |
工具调用运行之前。它可以阻止调用 |
PostToolUse |
工具调用成功之后 |
Stop |
Claude 完成响应时 |
SessionEnd |
会话结束时 |
hooks reference 列出了所有事件。Git 自带的 pre-commit 钩子是另一回事。正如 githooks 手册 所述,Git 在 Claude 运行 git commit 时执行它,而 --no-verify 会跳过它。
在每次提交前运行检查
此钩子在 Claude 发出的任何 git commit 之前运行,如果检查失败则阻止提交。将其添加到 .claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"if": "Bash(git commit *)",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/check-before-commit.sh"
}
]
}
]
}
}
将脚本保存为 .claude/hooks/check-before-commit.sh 并运行 chmod +x。将这两个命令替换为你自己的检查逻辑。
#!/bin/bash
# Runs before "git commit". Exit code 2 blocks the commit
# and sends stderr to Claude as feedback.
if ! pnpm typecheck >&2; then
echo "Blocked: typecheck failed. Fix the errors, then commit again." >&2
exit 2
fi
if ! pnpm lint >&2; then
echo "Blocked: lint failed. Fix the errors, then commit again." >&2
exit 2
fi
exit 0
有三个细节很重要。matcher 选择 Bash 工具。if 字段使用权限规则语法,因此脚本仅针对 git commit 命令运行。退出代码 2 会阻止工具调用。退出代码 0 允许其继续执行正常的权限流程,任何其他非零代码都不会阻止调用。if 过滤器是尽力而为的。对于绝不能违背的规则,请同时使用权限规则或 CI 检查。
在每次编辑后格式化文件
带有 Edit|Write 匹配器的 PostToolUse 钩子会在 Claude 修改文件后运行。这个来自钩子指南的示例会在编辑后的文件上运行 Prettier:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
]
}
}
此示例需要安装 jq。
将钩子与技能保持在一起
技能可以在其元数据中携带自己的钩子,格式与设置中相同。Claude Code 在调用技能时会注册它们,并在会话剩余时间内保留它们:
---
name: secure-operations
description: Perform operations with security checks
hooks:
PreToolUse:
- matcher: "Bash"
hooks:
- type: command
command: "./scripts/security-check.sh"
---
如果你的组织管理设置在 strictPluginOnlyCustomization 下列出了 hooks,则插件之外的技能将丢失其元数据中的钩子。请将此类技能作为插件发布。
一个优秀的团队配置包含三个部分:告诉 Claude 工作流的技能、强制执行必须遵守规则的钩子,以及向人员解释两者的仓库文档。没有钩子的技能只是建议。钩子强制执行规则,而技能解释围绕该规则的工作流。
支持文件
技能是一个文件夹,因此它可以包含多个文件。保持 SKILL.md 简洁并从其中链接其他文件,这样 Claude 仅在需要时才会加载它们:
my-skill/
├── SKILL.md (required: overview and navigation)
├── reference.md (detailed notes, loaded when needed)
├── examples.md (examples, loaded when needed)
└── scripts/
└── helper.py (a script that runs, not loaded as text)
这适用于部署工作流、模式迁移检查清单、多步骤代码审查和特定于产品的手册。命令文件是单个 Markdown 文件,因此没有文件夹来存放这些内容。
有效技能的设计技巧
明确路径和名称。说“创建一个组件”很模糊。说“在
src/components/ui/中创建一个 PascalCase 命名的组件”是 Claude 可以遵循的。包含正确输出的示例。
说明不要做什么。“永远不要使用
any类型”比“使用正确的类型”更容易遵循。每个技能只处理一个工作流。五个简短的技能比一个涵盖所有内容的冗长技能效果更好。
为匹配编写描述。将主要用例放在最前面,并使用人们在请求中常用的词汇。描述和
when_to_use文本在技能列表中合计会被截断为 1,536 个字符。决定谁来调用技能。对于任何具有副作用的操作,请设置
disable-model-invocation: true,这样 Claude 就不会因为代码看起来准备好了而自动运行部署。当你的规范发生变化时更新技能。过时的技能会传授旧的模式。
如果你只写一个技能,请为那个出错代价最大的重复步骤编写它。
TokenLab 为你提供来自多个提供商的聊天模型 API 密钥。要在其上运行 Claude Code,请遵循 Claude Code 集成指南。要比较编码工作的模型,请阅读 TokenLab 的最佳编码 AI 模型指南。
