Skills 是 Claude Code 中一種可重複使用的、基於 Prompt 的能力擴展機制。
它們以 SKILL.md 格式定義,讓 Claude 在需要時自動發現並使用特定的工作流程。
Skills 與 Custom Commands 的區別
這是最常見的混淆點。兩者的核心區別在於觸發方式:
- Custom Commands(
.claude/commands/):由使用者透過斜線命令手動觸發,例如/project:deploy。適合「我想要做 X」的場景 - Skills(
.claude/skills/):由 AI 自動發現並在需要時觸發。適合「當 Claude 遇到 Y 情境時,應該用 Z 方法」的場景
決策樹:
- 使用者需要明確觸發?→ Custom Command
- AI 應該自動識別何時使用?→ Skill
- 需要標準化的工作流程?→ 兩者都可以,但 Skill 更靈活
創建自定義 Skill
在專案的 .claude/skills/ 目錄下建立 Markdown 檔案:
# .claude/skills/deploy.md
---
name: deployment-skill
description: Handle deployment to production and staging environments
when: User asks about deploying, releasing, or pushing to production
---
## Deployment Workflow
1. Run all tests first: `npm run test`
2. Build the project: `npm run build`
3. Check for TypeScript errors: `npx tsc --noEmit`
4. If deploying to staging:
- Run: `npm run deploy:staging`
5. If deploying to production:
- Confirm with user first
- Run: `npm run deploy:prod`
- Create a git tag for the release
frontmatter 中的 when 欄位告訴 Claude 何時該啟用這個 Skill。
當使用者的請求匹配到這個描述時,Claude 會自動載入該 Skill 的指令。
內建 Skills
Claude Code 預裝了多個實用的內建 Skill:
- /commit:智慧提交——分析變更、生成有意義的 commit message、遵循專案規範
- /review-pr:審查 Pull Request——分析代碼變更、提出改善建議
- /simplify:簡化代碼——檢查重複、品質和效率問題
- /claude-api:幫助使用 Anthropic SDK 構建應用
Skills 的通用格式
Skills 採用通用的 Markdown 格式,這意味著同一個 Skill 可以在不同的 AI 工具中使用。 目前支援 Skills 格式的工具包括:
- Claude Code
- Cursor
- Gemini CLI
這種跨平台相容性意味著你為 Claude Code 編寫的 Skill,團隊中使用其他工具的成員也能受益。
範例:資料庫遷移 Skill
# .claude/skills/db-migration.md
---
name: database-migration
description: Create and run database migrations safely
when: User needs to modify database schema or create migrations
---
## Database Migration Workflow
### Creating a Migration
1. Generate migration file: `npx prisma migrate dev --name descriptive_name`
2. Review the generated SQL in `prisma/migrations/`
3. Ensure migration is reversible
### Safety Checks
- NEVER drop columns in production without a multi-step migration
- Always add new columns as nullable first
- Test migration on a copy of production data first
最佳實踐
- 每個 Skill 聚焦一個明確的任務領域
- 在
when欄位中使用具體的觸發描述 - 包含具體的命令和步驟,不要只寫抽象原則
- 將 Skills 提交到版本控制中,讓團隊共享