← posts

什么是 Skill?30 分钟搞懂 AI 编程的「操作手册」 / What Is a Skill? A 30-Minute Primer for AI-Assisted Coding

AI codingClaude CodeSkillsHooksCI

中文在前,English follows below. / Chinese first, English after the divider.


中文版

什么是 Skill?

Skill 是一个文件夹,里面放着一份写给 AI 的”操作手册”。AI 遇到相关任务时会自动翻开它,按里面的做法干活。

没有 Skill 时,你每次都得在对话里重复交代:“我们用 pytest,测试放 tests/ 下,命名要 test_xxx……”。有了 Skill,写一次就够,以后 AI 自己会想起来用。

每个 Skill 的核心是一个 SKILL.md 文件,另可带上脚本、模板等支持文件。

三个核心机制

1. 谁来触发:AI 自己决定。 AI 根据你的请求和 Skill 的 description 判断要不要用它。在 Claude Code 里,Skill 目录名同时也是斜杠命令(.claude/skills/deploy-staging/SKILL.md 对应 /deploy-staging),所以你也可以手动调用。

2. 按需加载。 平时 AI 只看到每个 Skill 的名字和简介,判断相关时才读正文。所以长篇参考资料在用到之前几乎不占成本,这也是 description 成为整个 Skill 中最关键一行的原因。

3. 放在哪里。

动手写第一个

建立 .claude/skills/write-tests/SKILL.md:

---
name: write-tests
description: 为 Python 代码编写或补充单元测试时使用。当用户要求写测试、补测试覆盖率、或修改了函数需要验证时触发。
---

# 编写测试的规范

1. 使用 pytest,不用 unittest。
2. 测试文件放在 tests/ 下,命名为 test_<模块名>.py。
3. 每个函数至少覆盖:正常输入、边界值、异常输入。
4. 外部依赖(网络、数据库)一律用 pytest-mock 模拟。
5. 写完后运行 `pytest -q`,全部通过再交付。

然后对 Claude Code 说”帮我给 utils.py 写测试”,看它是否按规范来。

为什么 md 文件里有 YAML?

SKILL.md 由两部分拼成,读者不同:

YAML 有自己的规矩:开头结尾的 --- 缺一不可;缩进只能用空格,不能用 Tab;冒号后要有空格。YAML 一旦解析失败,整个 Skill 就不会加载。

Skill 没被用上,几乎都是两个原因:description 太模糊,或者 YAML 格式有错。

Skill、CLAUDE.md、Hook、CI 各管什么

手段相当于能保证吗
CLAUDE.md墙上的规章制度(放单条事实)靠 AI 自觉
Skill遇到某类工作时翻开的操作手册(放多步流程)靠 AI 自觉,但更容易想起来
Hook门禁闸机程序强制,AI 无选择权
CI云端的最后一道关卡不通过就不能合并

Hook 是 Claude Code 在固定节点(AI 调用工具前后、结束回答前等)自动执行的脚本,由程序执行,不经过 AI 判断。例如:每次 AI 改完文件自动跑格式化;在 AI 写入 .env 之前拦截。

CI 是代码托管平台上的”自动检查机器人”:每次 push 或提交 PR,在云端干净环境里跑你规定的检查。以 GitHub Actions 为例,建 .github/workflows/test.yml:

name: test
on: [push, pull_request]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install -r requirements.txt
      - run: pytest -q
      - run: ruff check .

再在仓库的分支保护规则里勾选”必须通过检查才能合并”,规则才真正有强制力。

判断标准: 如果某条规则”万一 AI 忘了”后果严重,就别只写成文字,要做成 Hook 或 CI。顺序是:Skill/CLAUDE.md 教 AI 怎么做 → Hook 防它当场出错 → CI 兜底。

命名规则与管理

(具体限制可能随版本调整,以官方文档为准。)

一个 Skill 只做一件事。 原因:混合职责会让 description 变模糊、触发不准;触发后整份正文都会读入上下文,无关内容会干扰注意力;改一处还可能误伤另一处。同一流程的细节(示例、检查清单、脚本)放进同一文件夹的支持文件,而不是拆成多个 Skill:

write-tests/
├── SKILL.md          ← 主流程,保持精简
├── examples.md       ← 需要时参考
└── scripts/
    └── check_coverage.py

个人电脑上的管理建议:

  1. 换项目还用得上的放个人层 ~/.claude/skills/;离开项目就没意义的放项目层。
  2. 把个人 Skill 放进自己的 git 仓库,再用符号链接指向 ~/.claude/skills,获得版本历史,换电脑 clone 即可恢复。
  3. 控制数量,让各 description 互不重叠,并写清什么场景触发。久不用的就删。
  4. 从你真正反复粘贴的 3 到 5 个流程开始,用顺了再加,并持续迭代。

一句话行动建议

回想最近用 AI 编程时,哪段话你粘贴了三次以上,把它变成你的第一个 Skill。

参考:Claude Code Skills 官方文档(中文)


English Version

What is a Skill?

A Skill is a folder containing an operating manual written for your AI. When a relevant task comes up, the AI opens the manual on its own and follows it.

Without Skills, you keep re-explaining in chat: “We use pytest, tests go in tests/, name them test_xxx…”. With a Skill, you write it once and the AI recalls it when needed.

The heart of a Skill is a SKILL.md file, optionally accompanied by scripts, templates and other supporting files.

Three core mechanisms

1. Who triggers it: the AI decides. The AI judges from your request and the Skill’s description whether to use it. In Claude Code, the directory name doubles as a slash command (.claude/skills/deploy-staging/SKILL.md becomes /deploy-staging), so you can invoke it manually too.

2. Loaded on demand. Normally the AI only sees each Skill’s name and short description; the body is read only when it looks relevant. Long reference material therefore costs almost nothing until needed, which is why description is the most important line in the whole Skill.

3. Where Skills live.

Write your first one

Create .claude/skills/write-tests/SKILL.md:

---
name: write-tests
description: Use when writing or adding unit tests for Python code. Triggers when the user asks for tests, more coverage, or has changed a function that needs verification.
---

# Test-writing conventions

1. Use pytest, not unittest.
2. Put tests under tests/, named test_<module>.py.
3. Cover at least: normal input, boundary values, invalid input.
4. Mock external dependencies (network, database) with pytest-mock.
5. Run `pytest -q` and deliver only when everything passes.

Then tell Claude Code “write tests for utils.py” and see whether it follows your conventions.

Why is there YAML inside a Markdown file?

SKILL.md is two formats glued together, for two different readers:

YAML has its own rules: both --- lines are required; indent with spaces only, never tabs; put a space after each colon. If the YAML fails to parse, the Skill simply won’t load.

When a Skill isn’t being used, it’s almost always one of two things: a vague description, or broken YAML.

Skill vs CLAUDE.md vs Hook vs CI

ToolAnalogyGuaranteed?
CLAUDE.mdRules posted on the wall (single facts)Relies on the AI
SkillManual opened for a type of job (multi-step procedures)Relies on the AI, but recalled more reliably
HookA turnstileEnforced by the program; the AI has no say
CIThe last gate in the cloudCan’t merge unless it passes

A Hook is a script that Claude Code itself runs at fixed points (before or after a tool call, before the AI finishes its answer, and so on), with no AI judgment involved. For example: auto-format after every file edit, or block any write to .env.

CI is an automated checker on your code host: every push or pull request triggers your chosen checks in a clean cloud environment. With GitHub Actions, create .github/workflows/test.yml:

name: test
on: [push, pull_request]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install -r requirements.txt
      - run: pytest -q
      - run: ruff check .

Then turn on “require status checks to pass before merging” in branch protection rules, so the rule actually has teeth.

Rule of thumb: if forgetting a rule would hurt, don’t leave it as prose; make it a Hook or CI check. The order: Skill/CLAUDE.md teach the AI what to do → Hooks stop mistakes on the spot → CI is the safety net.

Naming rules and housekeeping

(Exact limits may change between versions; check the official docs.)

One Skill, one job. Mixed responsibilities blur the description and make triggering unreliable; the whole body is loaded once triggered, so unrelated content competes for attention; and an edit in one place can break another. Details of a single procedure (examples, checklists, scripts) belong in supporting files in the same folder, not in separate Skills:

write-tests/
├── SKILL.md          ← main procedure, kept lean
├── examples.md       ← consulted when needed
└── scripts/
    └── check_coverage.py

Managing Skills on your own machine:

  1. If it’s useful in any project, keep it in ~/.claude/skills/; if it only makes sense inside one project, keep it in that project’s .claude/skills/.
  2. Keep your personal Skills in your own git repo and symlink it to ~/.claude/skills, so you get history and can restore everything with one git clone on a new machine.
  3. Keep the count down, keep descriptions from overlapping, and say when to trigger. Delete what you haven’t used in a month.
  4. Start with the 3 to 5 procedures you genuinely paste most often, then grow and iterate.

One action item

Think of the instructions you’ve pasted into AI chats three or more times. Turn one of them into your first Skill.

Reference: Claude Code Skills documentation