自定義 Agents 與 Skills
建構你自己的 .md 擴展用於專門工作流。
你將學到什麼
Claude Code 不是一個固定的產品。它是一個可擴展的平台。只要將 Markdown 檔案放入特定目錄,你就能新增全新的功能 — 斜線命令、專門的審查者、自動化工作流,以及特定領域的助手。
完成後,你將了解:
- Skills 和 Agents 之間的差異
- 如何撰寫 skill(你或 Claude 都能觸發的工作流)
- 如何撰寫 agent(Claude 委派工作的子代理)
- 兩者的設計模式
- 如何限制工具以確保安全
- 從觸發到執行的生命週期
問題是什麼
每個團隊都有工作流程。部署程序、程式碼審查清單、測試協議、新人導入指南。這些知識通常存在於兩個地方:沒人閱讀的文件,和某個人的腦中。
「我們怎麼部署?」 → 問 Sarah
「我們的 PR 清單是什麼?」 → 在 wiki 裡... 某個地方
「資料庫遷移怎麼做?」 → Dave 寫了一個腳本,去他的家目錄找找
如果這些知識是 AI 可執行的指令呢?不只是文件 — 而是 AI 每次都會按步驟遵循的指令。這就是自定義 agents 和 skills 做的事。
如何運作
Skills 與 Agents
┌──────────────────────────────────────────────────────┐
│ Skills Agents(子代理) │
│ │
│ 由你觸發(/deploy),或在 由 Claude 透過 Agent │
│ description 相符時由 Claude 工具委派,或由你指定 │
│ 載入 (@-mention) │
│ │
│ 在主對話中執行 在自己的上下文視窗中 │
│ (注入指令) 執行,只回傳摘要 │
│ │
│ 位置: 位置: │
│ .claude/skills/<name>/SKILL.md .claude/agents/ │
│ <name>.md │
└──────────────────────────────────────────────────────┘
| 面向 | Skill | Agent |
|---|---|---|
| 觸發方式 | 你(/skill-name),或 Claude 在其 description 相符時 | Claude 透過 Agent 工具;你也可以點名或 @-mention |
| 上下文 | 載入主對話(或以 context: fork 在分支子代理中執行) | 自己的上下文視窗;只回傳結果 |
| 用途 | 逐步工作流、範本、參考知識 | 專門角色(審查者、探索者) |
| 工具控制 | allowed-tools 為觸發的那一輪預先核准工具 | tools 允許清單或 disallowedTools 拒絕清單 |
| 位置 | .claude/skills/<name>/SKILL.md | .claude/agents/<name>.md |
| 範圍 | 廣泛(完整工作流) | 聚焦(單一職責) |
兩者也都能放在使用者層級(~/.claude/skills/、~/.claude/agents/)以及 plugins 中。舊式的 .claude/commands/ 自訂命令仍然可用,並產生相同的 /name;skills 則多了可放輔助檔案的資料夾,以及控制誰能觸發的能力。
Skill 的結構
一個 skill 就是一個 SKILL.md 檔案:開頭是告訴 Claude Code 如何處理它的 YAML frontmatter,接著是 Markdown 指令。把下面這個存為 .claude/skills/deploy/SKILL.md --- 資料夾名稱就是 /deploy 命令:
---
name: deploy
description: Deploy the application to production with safety checks
disable-model-invocation: true
---
# Deploy
1. Run `git status` to check for uncommitted changes
2. Run the full test suite with `npm test`
3. If tests fail, stop and report the failures
4. Run `npm run build` and verify no errors
5. Show the user: test results, build size, files changed
6. Ask: "Ready to deploy? [yes/no]"
7. Only if confirmed: run `git push origin main`
8. Report deployment status
## Rules
- NEVER push without user confirmation
- If any step fails, stop and report
開頭的 --- 必須是檔案的第一行,否則 Claude Code 會把整個檔案當成指令。description 是 Claude 判斷何時適用這個 skill 的依據,所以請把關鍵用途放在最前面。disable-model-invocation: true 讓 Claude 不會自行啟動有副作用的工作流:你用 /deploy 執行它,Claude 不會因為覺得程式碼看起來準備好了就決定部署。
Agent 的結構
一個 agent 定義了 Claude 可以委派工作的專門角色(子代理)。把下面這個存為 .claude/agents/security-reviewer.md:
---
name: security-reviewer
description: Reviews code changes for security vulnerabilities. Use after changes to authentication, input handling, or data access.
tools: Read, Glob, Grep
model: sonnet
---
You are a security-focused code reviewer. Be thorough
but avoid false positives.
## Checklist
- SQL injection: raw queries with user input
- XSS: unescaped output in templates
- Auth bypass: missing middleware on protected routes
- Secrets: hardcoded API keys, passwords, tokens
- Path traversal: unsanitized file paths
- SSRF: user-controlled URLs in server requests
## Output Format
For each finding: file, line, severity, issue, fix.
If clean: "No security issues found." and list what you checked.
只有 name 與 description 是必填。Markdown 本文會成為子代理的系統提示。真正限制工具的是 tools 欄位:這個審查者只拿得到 Read、Glob 與 Grep。model 選擇它運行的模型(sonnet、opus、haiku、fable、完整模型 ID,或 inherit)。
當 Claude 委派工作時,呼叫看起來像這樣:
{
"type": "tool_use",
"name": "Agent",
"input": {
"subagent_type": "security-reviewer",
"description": "Security review of src/api",
"prompt": "Review the changes in src/api/ for security issues"
}
}
你也可以點名要求某個 agent(「用 security-reviewer agent 檢查這些變更」)、用 @-mention 確保它一定會執行,或用 claude --agent security-reviewer 讓整個會話以該 agent 身分運行。
Skill 生命週期
┌──────────────────────────────────────────────────────┐
│ 1. 會話啟動:Claude Code 探索 skills │
│ (~/.claude/skills、.claude/skills、plugins) │
│ ▼ │
│ 2. 每個 skill 的 description 會列給 Claude 參考 │
│ (disable-model-invocation: true 的除外) │
│ ▼ │
│ 3. 你輸入 /deploy,或 Claude 判斷某個 skill 適用 │
│ ▼ │
│ 4. 渲染後的 SKILL.md 以一則訊息進入對話, │
│ 並在後續回合中持續保留 │
│ ▼ │
│ 5. Claude 以一般工具逐步執行 │
│ (allowed-tools 為那一輪預先核准部分工具) │
│ ▼ │
│ 6. 工作流完成(或你中斷) │
└──────────────────────────────────────────────────────┘
Skill 設計模式
模式 1:逐步工作流
最常見的模式。一系列編號的操作:
## Steps
1. Check preconditions
2. Gather information
3. Present plan to user
4. Execute with safety checks
5. Verify results
6. Report summary
模式 2:決策樹
用於根據條件分支的工作流:
## Process
Check the project type:
- If `package.json` exists → Node.js project
- If `tsconfig.json` exists → TypeScript
- Use `npx tsc --noEmit` for type checking
- Else → JavaScript
- Use `node --check` for syntax validation
- If `requirements.txt` exists → Python project
- Use `python -m py_compile` for syntax checking
- If `Cargo.toml` exists → Rust project
- Use `cargo check` for compilation checking
模式 3:基於範本的輸出
用於生成結構化內容的 skill:
## Output Template
Generate a changelog entry using this format:
### [version] - YYYY-MM-DD
#### Added
- New features
#### Changed
- Changes to existing features
#### Fixed
- Bug fixes
Fill each section by analyzing git commits since the last tag.
Agent 設計模式
模式:專門審查者
---
name: performance-reviewer
description: Reviews code for performance problems. Use when a change touches database access, list endpoints, or hot loops.
tools: Read, Glob, Grep
---
Analyze code for performance issues.
## Check For
- N+1 queries in database access patterns
- Missing pagination on list endpoints
- Unbounded array operations (map/filter on large datasets)
- Missing indexes suggested by query patterns
- Synchronous operations that should be async
模式:帶結構化輸出的探索者
---
name: architecture-explorer
description: Maps the architecture of an unfamiliar codebase. Use before planning changes in code you haven't read.
tools: Read, Glob, Grep
model: haiku
---
Map the architecture of this codebase.
## Output Format
- Entry points (main files, index files)
- Dependency graph (what imports what)
- Data flow (where data enters, transforms, exits)
- External dependencies (APIs, databases, services)
探索者使用 Haiku,因為描繪程式碼庫是大量閱讀,而不是深度推理(第 21 堂課)。
工具限制
限制 agent 的工具遵循最小權限原則。審查者不應該編輯程式碼。探索者不應該執行命令。請把限制寫在 frontmatter,而不是本文 --- 本文裡寫一行「Tools: Read only」只是一條指令,而 tools 欄位會移除其他所有工具:
---
name: security-reviewer
description: Reviews code changes for security vulnerabilities
tools: Read, Glob, Grep # allowlist: only these tools
# disallowedTools: Write, Edit # or: inherit everything except these
---
有了這份允許清單,審查者就無法編輯檔案、執行命令,也無法再產生自己的子代理(否則子代理預設最多可巢狀三層)。在 macOS、Linux 與 WSL 上,只列出 Glob 與 Grep 而不列 Bash,也會讓這個子代理取得專用的搜尋工具。對 skills 而言,allowed-tools 只會為觸發該 skill 的那一輪預先核准工具,不會拿走任何工具。若要封鎖工具,請使用權限 deny 規則,或在 skill 啟用期間使用 disallowed-tools。
關鍵洞見
自定義 agents 和 skills 是你編碼團隊工作流知識的方式。 它們將部落知識轉化為可重複、一致的 AI 行為。
平庸的 Claude Code 設定和強大的之間的差異,不在於模型 — 而在於 skills 和 agents 的品質。一個寫得好的部署 skill 意味著每次部署都遵循相同的安全檢查。一個寫得好的安全審查者意味著每個 PR 都得到相同的審視。
這是「基礎設施即程式碼」原則應用到 AI 工作流上:工作流即 Markdown。 無需編譯、無需框架、無需部署。放入一個檔案,獲得一個能力。
想想你的團隊重複做的事情:
- 帶有特定清單的程式碼審查
- 帶有安全閘門的部署程序
- 資料遷移協議
- 讓新團隊成員熟悉程式碼庫
每一項都可以成為一個 skill 或 agent,每次都以相同的方式運行,無論是誰觸發的。
實作範例
建構一個自定義的程式碼審查 skill 和一個安全審查者 agent。
審查 Skill
建立 .claude/skills/team-review/SKILL.md:
---
description: Review the current branch against main with the team checklist and a security pass. Use when asked to review a branch or prepare a PR.
---
# Team Review
Review the current branch's changes against the base branch.
## Steps
1. Run `git diff main...HEAD --stat` to see changed files
2. For each changed file:
- Read the full diff with `git diff main...HEAD -- <file>`
- Check for:
- Missing error handling
- Hardcoded values that should be config
- Functions longer than 50 lines
- Missing type annotations
- Console.log or debug statements left in
3. Use the security-reviewer agent on the changed files
4. Compile all findings into a report:
## Report Format
### Summary
[1-2 sentence overview]
### Findings
| File | Line | Severity | Issue |
|------|------|----------|-------|
### Security Review
[Output from security-reviewer agent]
### Recommendation
[Approve / Request Changes / Needs Discussion]
為什麼不叫 review?Claude Code 已經把 /review 用作內建 /code-review skill 的別名,所以團隊的 skill 請取不同的名字。這個 skill 沒有設定 disable-model-invocation:審查只會讀取程式碼,所以當你請 Claude 審查分支時讓它自行載入是安全的。
安全審查者 Agent
建立 .claude/agents/security-reviewer.md:
---
name: security-reviewer
description: Reviews code changes for security vulnerabilities. Use after changes to authentication, input handling, or data access.
tools: Read, Glob, Grep
---
You review code for security vulnerabilities. Be thorough
but avoid false positives. Only flag issues you are
confident about.
## Checklist
- [ ] No SQL injection (parameterized queries)
- [ ] No XSS (output escaping)
- [ ] No hardcoded secrets
- [ ] Auth middleware on protected routes
- [ ] Input validation on user-facing endpoints
- [ ] No path traversal in file operations
## Output
List findings with file, line, severity, and fix.
If clean, state "No security issues found."
現在輸入 /team-review:Claude 會遵循這個 skill,把安全檢查委派給 security-reviewer 子代理,並產生結構化的報告。無論是誰執行,每次審查都是一致的。
前後對比
| 沒有擴展 | 有自定義 Agents 與 Skills |
|---|---|
| 工作流存在於文件中 | 工作流是 AI 可執行的 Markdown |
| 一致性取決於誰在運行 | 每次都是相同的步驟 |
| 新團隊成員需要培訓 | Skills 編碼了培訓內容 |
| 審查因審查者而異 | Agents 應用相同的清單 |
| 部落知識在人的腦中 | 知識編碼在檔案中,受版本控制 |
| 新增能力需要寫程式碼 | 新增能力只需要一個 Markdown 檔案 |
下一堂課
你已經擁有用自己的工作流擴展 Claude Code 的工具。第 24 堂課將所有內容整合在一起,涵蓋生產環境模式 — 如何從個人生產力工具轉變為團隊級平台,包含可觀測性、CI/CD 整合、成本管理,以及擴展 AI 輔助開發的成熟度模型。