Learn Claude Code
s05

技能

规划与协调

Load on Demand

321 LOC5 个工具TypeScriptSkillLoader + two-layer injection
Inject knowledge via tool_result when needed, not upfront in the system prompt

s01 > s02 > s03 > s04 > [ s05 ] s06 | s07 > s08 > s09 > s10 > s11 > s12

"用到什么知识, 临时加载什么知识" -- 通过 tool_result 注入, 不塞 system prompt。

Harness 层: 按需知识 -- 模型开口要时才给的领域专长。

问题

你希望智能体遵循特定领域的工作流: git 约定、测试模式、代码审查清单。全塞进系统提示太浪费 -- 10 个技能, 每个 2000 token, 就是 20,000 token, 大部分跟当前任务毫无关系。

解决方案

System prompt (Layer 1 -- always present):
+--------------------------------------+
| You are a coding agent.              |
| Skills available:                    |
|   - git: Git workflow helpers        |  ~100 tokens/skill
|   - test: Testing best practices     |
+--------------------------------------+

When model calls load_skill("git"):
+--------------------------------------+
| tool_result (Layer 2 -- on demand):  |
| <skill name="git">                   |
|   Full git workflow instructions...  |  ~2000 tokens
|   Step 1: ...                        |
| </skill>                             |
+--------------------------------------+

第一层: 系统提示中放技能名称 (低成本)。第二层: tool_result 中按需放完整内容。

工作原理

  1. 每个技能是一个目录, 包含 SKILL.md 文件和 YAML frontmatter。
skills/
  pdf/
    SKILL.md       # ---\n name: pdf\n description: Process PDF files\n ---\n ...
  code-review/
    SKILL.md       # ---\n name: code-review\n description: Review code\n ---\n ...
  1. SkillLoader 递归扫描 SKILL.md 文件, 并用目录名作为兜底技能标识。
class SkillLoader {
  skills: Record<string, SkillRecord> = {};

  constructor(private skillsDir: string) {
    this.loadAll();
  }

  private loadAll() {
    for (const filePath of collectSkillFiles(this.skillsDir)) {
      const text = readFileSync(filePath, "utf8");
      const { meta, body } = parseFrontmatter(text);
      const fallbackName = filePath.replace(/\\/g, "/").split("/").slice(-2, -1)[0] ?? "unknown";
      const name = meta.name || fallbackName;
      this.skills[name] = { meta, body, path: filePath };
    }
  }

  getContent(name: string): string {
    const skill = this.skills[name];
    if (!skill) {
      return `Error: Unknown skill '${name}'.`;
    }
    return `<skill name="${name}">\n${skill.body}\n</skill>`;
  }
}
  1. 第一层写入系统提示。第二层不过是 dispatch map 中的又一个工具。
const SYSTEM = `You are a coding agent at ${WORKDIR}.
Use load_skill to access specialized knowledge before tackling unfamiliar topics.

Skills available:
${skillLoader.getDescriptions()}`;

const TOOL_HANDLERS = {
  // ...base tools...
  load_skill: (input) => skillLoader.getContent(String(input.name ?? "")),
};

模型知道有哪些技能 (便宜), 需要时再加载完整内容 (贵)。

相对 s04 的变更

组件之前 (s04)之后 (s05)
Tools5 (基础 + task)5 (基础 + load_skill)
系统提示静态字符串+ 技能描述列表
知识库skills/*/SKILL.md 文件
注入方式两层 (系统提示 + result)

试一试

cd learn-claude-code

试试这些 prompt:

cd agents-ts
npm install
npm run s05
  1. What skills are available?
  2. Load the agent-builder skill and follow its instructions
  3. I need to do a code review -- load the relevant skill first
  4. Build an MCP server using the mcp-builder skill