Command · Agent · Skill 三层协作模式

来源:CCBP(Claude Code Best Practice)仓库 · orchestration-workflow/orchestration-workflow.md


为什么要三层配合?

你可能已经知道:

  • Skill 是一份指令手册,告诉 AI “怎么做这件事”
  • Agent 是一个专注的执行者,接到任务就干,干完就报告
  • Command 是一个斜杠命令,用户输入 /xxx 就触发

单独用任何一个都能做事。但真实世界里的任务往往是这样的:

用户问天气 → 先问他要摄氏还是华氏 → 去 API 拉数据 → 把结果画成一张卡片 → 告诉用户

这一条链里,有用户交互、有外部调用、有文件创建,还要按顺序执行。靠一个 Skill 或一个 Agent 单打独斗,要么职责太混,要么无法协调。

三层的分工思路很清晰:

谁干做什么
Command入口 + 指挥问用户、调 Agent、调 Skill、汇报结果
Agent专注执行者带着自己的 Skill 去完成一件事,返回结果
Skill工具箱给 Agent 或 Command 提供”怎么做”的知识

天气查询系统:完整示例

CCBP 仓库用一个天气查询系统演示了这个模式。涉及 4 个文件:

  • weather-orchestrator.md(Command)
  • weather-agent.md(Agent)
  • weather-fetcher/SKILL.md(Agent 的 Skill,预加载)
  • weather-svg-creator/SKILL.md(Command 直接调用的 Skill)

Command:/weather-orchestrator

原文精华(.claude/commands/weather-orchestrator.md

---
description: Fetch weather data for Dubai and create an SVG weather card
model: haiku
---

## Workflow

### Step 1: Ask User Preference
Use the AskUserQuestion tool to ask the user whether they want the temperature
in Celsius or Fahrenheit.

### Step 2: Fetch Weather Data
Use the Task tool to invoke the weather agent:
- subagent_type: weather-agent
- prompt: Fetch the current temperature for Dubai, UAE in [unit requested by user].

### Step 3: Create SVG Weather Card
Use the Skill tool to invoke the weather-svg-creator skill:
- skill: weather-svg-creator

## Critical Requirements
1. Use Task Tool for Agent: DO NOT use bash commands to invoke agents.
   You must use the Task tool.
2. Use Skill Tool for SVG Creator: Invoke the SVG creator via the Skill tool,
   not the Task tool.

Command 做三件事:问用户、用 Task 工具调 Agent、用 Skill 工具调 SVG Skill。它本身不碰数据,只负责协调和呈现。

Agent:weather-agent

原文精华(.claude/agents/weather-agent.md

---
name: weather-agent
model: sonnet
color: green
maxTurns: 5
skills:
  - weather-fetcher
---
 
# Weather Agent
 
## Your Task
1. Fetch: Follow the weather-fetcher skill instructions to fetch the current temperature
2. Report: Return the temperature value and unit to the caller
 
## Critical Requirements
1. Use Your Skill: The skill content is preloaded - follow those instructions
2. Return Data: Your job is to fetch and return the temperature - not to write
   files or create outputs

Agent 的 frontmatter 里写了 skills: - weather-fetcher,意思是启动时这份 Skill 的内容就被注入进去了,Agent 直接按 Skill 的说明行动,不需要再调用什么工具加载它。

Skill(Agent 预加载版):weather-fetcher

原文精华(.claude/skills/weather-fetcher/SKILL.md

---
name: weather-fetcher
user-invocable: false
---
 
## Instructions
 
1. Fetch Weather Data: Use the WebFetch tool to get current weather data
   from the Open-Meteo API.
   For Celsius:
   URL: https://api.open-meteo.com/v1/forecast?latitude=25.2048&longitude=55.2708
        &current=temperature_2m&temperature_unit=celsius
 
2. Extract Temperature: From the JSON response, extract current.temperature_2m
 
3. Return Result: Return the temperature value and unit clearly.
 
Notes:
- Only fetch the temperature, do not perform any transformations or write any files
- user-invocable: false (不能被用户直接调用,只能被 Agent 预加载)

注意 user-invocable: false——这个 Skill 不是给用户直接喊的,它是 Agent 的”内功”,启动时自动注入,不会出现在 /skill 列表里。

Skill(Command 直接调用版):weather-svg-creator

原文精华(.claude/skills/weather-svg-creator/SKILL.md

---
name: weather-svg-creator
description: Creates an SVG weather card showing the current temperature for Dubai.
---
 
## Task
You will receive a temperature value and unit from the calling context.
Create an SVG weather card and write both the SVG and a markdown summary.
 
## Instructions
1. Create SVG — Use the SVG template, replacing placeholders with actual values
2. Write SVG file — Write to orchestration-workflow/weather.svg
3. Write summary — Write to orchestration-workflow/output.md
 
## Rules
- Use the exact temperature value and unit provided — do not re-fetch or modify

这个 Skill 不在任何 Agent 里,而是由 Command 用 Skill 工具直接调用。它拿到的温度数据来自对话上下文(Agent 上一步已经返回了),自己专心负责”画卡片”。


一次完整调用的时序

用户输入 /weather-orchestrator
    │
    ▼
Command(haiku 模型)启动
    │
    ├─ [Step 1] AskUserQuestion → 用户回答:"摄氏"
    │
    ├─ [Step 2] Task 工具 → 调用 weather-agent
    │               │
    │               └─ weather-agent 启动(sonnet 模型)
    │                       │  已预加载 weather-fetcher Skill
    │                       │
    │                       ├─ 按 Skill 指引 WebFetch Open-Meteo API
    │                       ├─ 提取 current.temperature_2m = 36
    │                       └─ 返回给 Command:"36°C"
    │
    ├─ [Step 3] Skill 工具 → 调用 weather-svg-creator
    │               │  (从上下文中读取 36°C)
    │               │
    │               ├─ 生成 weather.svg
    │               └─ 写入 output.md
    │
    └─ 汇报用户:摄氏 / 36°C / SVG 在 weather.svg

两种 Skill 用法的区别一目了然:

  • weather-fetcher → 注入 Agent(skills: 列表)· Agent 启动时就有,不需要额外调用
  • weather-svg-creator → Command 用 Skill 工具直接调用 · 独立运行在 Command 的上下文里

踩坑点:subagent 之间不能用 bash 互调

CCBP 原文明确强调:

## Critical Requirements
1. Use Task Tool for Agent: DO NOT use bash commands to invoke agents.
   You must use the Task tool.

这一条是新手最容易踩的坑。很多人第一反应是”我直接在 Command 里写一行 bash 调用另一个 Agent 的脚本不就行了?” 不行,原因有两个:

  1. Agent 不是脚本。Agent 的身份、工具权限、预加载 Skill,都是 Claude Code 框架在启动时注入的,绕过 Task 工具就绕过了整个注入机制,Agent 会变成一个什么都不懂的空壳。
  2. 返回值没法传。bash 调用的输出是字符串,而 Task 工具的调用会把 Agent 的整个对话上下文结果结构化地返回给调用方,数据流才能接续。

同理,Skill 要用 Skill 工具调,不能用 bash 执行 Skill 的 md 文件。


你可以怎么用

如果你是第一次自己搭三层系统,建议这个顺序:

第一步:先想清楚有几件”专业的事” 天气系统有两件:拉数据、画图。每件事对应一个 Skill 或一个 Agent + Skill 组合。

第二步:决定哪些 Skill 预加载、哪些直接调用

  • 如果 Skill 是某个 Agent 的”专业知识”(比如 weather-fetcher 是 weather-agent 的领域技能)→ 写进 Agent 的 skills: 列表,预加载
  • 如果 Skill 是独立的输出工具(比如画图、写报告)→ 放在 Command 里用 Skill 工具调用

第三步:用 Command 把流程串起来 Command 只负责:问用户、调 Agent、调 Skill、报结果。它自己不做业务逻辑,只做协调。

建议起点(小于 3 步的任务不需要三层)

  • 1 件事 → 1 个 Skill 就够
  • 2 件事但有先后顺序 + 一件需要外部 API → 考虑 Command + Agent + Skill
  • 有用户交互 + 多步骤 → 三层架构值得投入

关联页面

  • 5-术语们/02-agent.md — Agent 是什么
  • 5-术语们/03-skill.md — Skill 是什么
  • 5-术语们/04-subagent.md — Subagent 与 Agent 的关系
  • 5-术语们/05-workflow.md — Workflow 编排基础