s05
技能
规划与协调Load on Demand
102 LOC5 个工具SkillLoader + 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 中按需放完整内容。
工作原理
- 每个技能是一个目录, 包含
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 ...
- SkillLoader 递归扫描
SKILL.md文件, 用目录名作为技能标识。
class SkillLoader {
constructor(skillsDir: string) {
this.skills = {};
for (const filePath of globSync("**/SKILL.md", { cwd: skillsDir }).sort()) {
const fullPath = join(skillsDir, filePath);
const text = readFileSync(fullPath, "utf8");
const { meta, body } = this.parseFrontmatter(text);
const name = meta.name || basename(dirname(fullPath));
this.skills[name] = { meta, body };
}
}
getDescriptions(): string {
const lines = [];
for (const [name, skill] of Object.entries(this.skills)) {
lines.push(` - ${name}: ${skill.meta.description || ""}`);
}
return lines.join("\n");
}
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>`;
}
}
- 第一层写入系统提示。第二层不过是 dispatch map 中的又一个工具。
const SYSTEM = `You are a coding agent at ${WORKDIR}.
Skills available:
${SKILL_LOADER.getDescriptions()}`;
const TOOL_HANDLERS = {
// ...base tools...
load_skill: (input) => SKILL_LOADER.getContent(input.name),
};
模型知道有哪些技能 (便宜), 需要时再加载完整内容 (贵)。
相对 s04 的变更
| 组件 | 之前 (s04) | 之后 (s05) |
|---|---|---|
| Tools | 5 (基础 + task) | 5 (基础 + load_skill) |
| 系统提示 | 静态字符串 | + 技能描述列表 |
| 知识库 | 无 | skills/*/SKILL.md 文件 |
| 注入方式 | 无 | 两层 (系统提示 + result) |
试一试
cd learn-claude-code-typescript
npx tsx agents/s05_skill_loading.ts
试试这些 prompt (英文 prompt 对 LLM 效果更好, 也可以用中文):
What skills are available?Load the agent-builder skill and follow its instructionsI need to do a code review -- load the relevant skill firstBuild an MCP server using the mcp-builder skill