Claude Code系列文5 – 建立自己的Claude Code SKILL優化工作流程

目錄

本篇文章的重點

  1. 用SKILL優化重複的工作流程
  2. 如何建立第一個SKILL
  3. 瞭解SKILL的完整結構與傳遞參數的方式

為什麼要用SKILLs

你一定有過那種經驗。

打開Claude Code,輸入:「幫我Review這次改動,特別注意安全性與效能問題」,AI給出的回饋很精準,找到了可怕的錯誤並修正。

隔天,你再次打開Claude Code,再次輸入:「幫我Review這次改動,特別注意安全性與效能問題」,一模一樣的提示詞。

第三天、第四天…不斷重複。

這就是重複工作流程的問題,不是AI不夠聰明,而是你的工作流程可以更優化。

SKILL就是把你正在執行的工作流程,變成可重複利用的內建知識模組供Claude Code使用。

建立第一個SKILL

SKILL講白了就是一個檔案結構,裡面包含以下這些項目:

Level 1: Metadata

一但SKILL建立,Claude Code永遠會讀取metedata,以便判斷是否需要用到SKILL,如果需要再進一步執行,才會進到instructions,不用在一開始就讀取所有內容,減少token浪費。

提供PDF檔案處理的SKILL範例:

---
name: pdf-processing
description: Extract text and tables from PDF files, fill forms, merge documents. Use when working with PDF files or when the user mentions PDFs, forms, or document extraction.
---

Level 2: Instructions

當Claude Code判斷SKILL需要執行的時候,就會讀取instructions的內容。

同樣的PDF檔案處理的範例:

# PDF Processing

## Quick start

Use pdfplumber to extract text from PDFs:

python
import pdfplumber

with pdfplumber.open("document.pdf") as pdf:
    text = pdf.pages[0].extract_text()

For advanced form filling, see [FORMS.md](FORMS.md).

Resources and code

claude code用skills執行程式碼的時候,只會消耗程式碼輸出結果的內容token,會比透過claude code自行產生程式碼執行來得更有效率。

額外附加檔案沒有任何的限制,可以放完整的API文件、程式碼、很大的資料、複雜的範例以及任何的參考資料,這些內容在實際取用之前都不會有任何的token消耗

SKILL完整資料夾目錄

完整的內容如下,一般實際在使用時,最簡便的方式只會使用到SKILL.md這份主要檔案,就能夠順利執行SKILL了。

pdf-skill/
├── SKILL.md (main instructions)
├── FORMS.md (form-filling guide)
├── REFERENCE.md (detailed API reference)
└── scripts/
    └── fill_form.py (utility script)

完整SKILL.md範例

---
name: pdf-processing
description: Extract text and tables from PDF files, fill forms, merge documents. Use when working with PDF files or when the user mentions PDFs, forms, or document extraction.
---

# PDF Processing

## Quick start

Use pdfplumber to extract text from PDFs:

python
import pdfplumber

with pdfplumber.open("document.pdf") as pdf:
    text = pdf.pages[0].extract_text()

For advanced form filling, see [FORMS.md](FORMS.md).

傳遞參數

然而,在實際使用上會遇到需要傳遞參數的情況,例如以下範例是想要修正某個Github上面的issue:

---
name: fix-issue
description: Fix a GitHub issue
disable-model-invocation: true
---

Fix GitHub issue $ARGUMENTS following our coding standards.

1. Read the issue description
2. Understand the requirements
3. Implement the fix
4. Write tests
5. Create a commit

透過指令/fix-issue 123,Claude Code就會收到「Fix GitHub issue 123 following our coding standards…」這樣子的提示詞,然後開始執行任務。

發佈留言