claude code实战四
1.Skills 的基础结构
Skills 是一种可被语义触发的能力包,它包含领域知识、执行步骤、输出规范与约束条件,并在需要时渐进式加载到主 Agent 的认知空间中。
Tools 是行动原语。它回答的是能做什么。读文件、改代码、执行 Bash 命令……这些是操作层面的能力,类似人的双手。 SubAgents 是执行分工。它回答的是谁来做。当任务复杂到需要独立上下文时,子代理承担专职职责,类似团队中的同事。 Hooks 是流程规则。它回答的是什么时候检查。它们在关键节点自动触发质量校验或合规约束,类似企业中的质检流程。 而 Skills 回答的,是另外一个非常关键的问题:“怎么做,以及何时做”,它不是工具,也不是分工机制。它是一种可操作知识结构。
而一个 Skill,则是一段具备语义入口的标准操作程序。它通过 description 告诉模型:在什么情况下应该加载这项能力。它在正文中定义执行步骤,将抽象原则转化为可执行流程。它通过模板约束输出格式,确保结果标准化。它可以限制可调用工具的范围,防止越权操作。它甚至可以通过 hooks 在完成后自动执行验证逻辑。 当我们把文档封装为 Skill,它就不再是参考资料,而成为一种可被调用的行为模式。从工程视角看,这是对上下文资源的优化;但从系统设计视角看,这是一种更深层的变化。
claude code中,同一个 Skill 既可以作为斜杠命令使用,也可以让 Claude根据skill的description自动判断何时需要。
可以采用三种方式来控制 Claude 对 Skills 的访问。 全局禁用:在 /permissions 中 deny Skill 工具 精确控制:Skill(commit) 精确匹配,Skill(deploy *) 前缀匹配 逐个控制:给 Skill 加 disable-model-invocation: true frontmatter,设有 disable-model-invocation: true 的 Skill,其 description 不会加载到上下文——Claude 完全看不到它,只有用户 /name 才能触发。
好的 Skill 设计遵循“导航页 + 详情页”模式:
SKILL.md ← 导航页:概述 + 引用(< 500 行) ├── reference.md ← 详情页:详细 API 文档 ├── examples.md ← 详情页:使用示例 └── scripts/validate.sh ← 工具:可执行脚本
注意:SKILL.md 应该被控制在 500 行以内。如果过于复杂,应该将详细参考资料移到独立文件,并在 SKILL.md 中进行引用(也就是我们所常说的渐进式加载)。
Skills 的存放位置决定了谁能使用它,以及优先级顺序。
在 Claude Code 中,每个 Skill 独占一个目录。其标准的目录和文件结构如下:.claude/skills//SKILL.md。
从工程角度,Skill 内容分为两类,参考型和任务型。参考型 Skill 影响“怎么做”,任务型 Skill 决定“做什么”。前者是语义环境,后者是具体行动。
一个Skill的内容示例如下:
.claude/skills/api-conventions/ # skill 目录,名称即 skill 名
└── SKILL.md # 主文件(必需)
---
name: api-conventions
description: API design patterns and conventions for this project. Covers RESTful URL naming, response format standards, error handling, and authentication requirements. Use when writing or reviewing API endpoints, designing new APIs, or making decisions about request/response formats.
allowed-tools:
- Read
- Grep
- Glob
---
# API Design Conventions
These are the API design standards for our project. Apply these conventions whenever working with API endpoints.
## URL Naming
- Use plural nouns for resources: `/users`, `/orders`, `/products`
- Use kebab-case for multi-word resources: `/order-items`, `/user-profiles`
- Nested resources for belongsTo relationships: `/users/{id}/orders`
- Maximum two levels of nesting; beyond that, use query parameters
- Use query parameters for filtering: `/orders?status=active&limit=20`
## Response Format
All API responses must follow this structure:
{
"data": {}, // 成功时返回的数据
"error": null, // 错误时返回错误对象 { code, message, details }
"meta": { // 分页和元信息
"page": 1,
"limit": 20,
"total": 100
}
}
## HTTP Status Codes
- 200: 成功返回数据
- 201: 成功创建资源
- 400: 请求参数错误
- 401: 未认证
- 403: 无权限
- 404: 资源不存在
- 422: 业务逻辑错误
- 500: 服务器内部错误
## Authentication
- All endpoints require Bearer token unless explicitly marked as public
- Public endpoints must be documented with `@public` annotation
- Token format: `Authorization: Bearer <jwt-token>`
## Versioning
- API version in URL path: `/api/v1/users`
- Breaking changes require new version
一般一个skill.md有三个部分: YAML frontmatter,是通过---包裹的元数据。 Markdown 正文,是技能的具体说明。辅助文件:.claude/skills//SKILL.md——每个 Skill 在自己的目录中,可以包含辅助文件(此处只有主文件,下一讲中的示例我们将看到辅助文件)。
Claude Code 官方支持的完整 frontmatter 字段如下:
---
name: my-skill-name # 可选:Skill 标识符(省略则用目录名)
description: What this does # 推荐:触发器(最重要!)
argument-hint: "[issue-number]" # 可选:自动补全时的参数提示
disable-model-invocation: true # 可选:禁止 Claude 自动触发
user-invocable: false # 可选:对用户隐藏 /skill-name
allowed-tools: # 可选:限制可用工具
- Read
- Grep
- Glob
model: sonnet # 可选:指定执行模型
context: fork # 可选:在子代理中隔离执行
agent: Explore # 可选:context: fork 时的代理类型
hooks: # 可选:作用域为此 Skill 的 Hooks
PreToolUse:
- matcher: Write
hooks:
- type: command
command: "echo 'Write called in skill'"
---
早期,斜杠命令 /Comands 和 Skills 是两个独立组件。但在新版 Claude Code 中,Commands 已合并到 Skills,成为 Skills 的子集。 因此,在 .claude/commands/review.md 和 .claude/skills/review/SKILL.md 两个不同目录的文件,都会创建 /review。Skills 目录的额外优势是支持辅助文件目录(模板、示例、脚本等)。如果同名 Skill 和 Command 共存,Skill 优先。
通过 ARGUMENTS 给 Skill 传参 当你通过 /skill-name args 调用 Skill 时,args 会通过 $ARGUMENTS 注入到 Skill 内容中。
Skill 支持两种参数传递方式。 单参数——$ARGUMENTS 接收所有参数。 --- description: Quick git commit argument-hint: [commit message] disable-model-invocation: true --- Create a git commit with message: $ARGUMENTS 多参数—— $1,$2 接收位置参数:
--- description: Create a pull request argument-hint: [title] [description] disable-model-invocation: true --- Title: $1 Description: $2
更多推荐




所有评论(0)