按需載入的領域知識
「Don't put everything in the system prompt. Load on demand.」
「全塞 system prompt」的坑
你有 20 個 skill,每個寫得挺詳細:pdf-processing(怎麼讀 PDF)、code-review(review 的 checklist)、git-workflow(常用 git 套路)…… 直覺做法:全部拼進 system prompt,讓模型隨時查閱。
結果:
- 每次呼叫都燒掉 15-30K 輸入 token(即使問題根本用不上任何 skill)。
- 模型注意力被稀釋——長 system prompt 裡提及的規則,服從度會下降。
- 改一個 skill,所有歷史對話的快取全作廢。
s05 的做法是把它拆成兩層。
兩層架構
Layer 1 · 便宜:system prompt 裡只放 skill 的名字和一句話描述(每個約 100 token)。20 個 skill = 2K token,可接受。
# 系統提示裡的 skill 清單
Skills available:
- pdf: Process PDF files. Extract text, tables, metadata.
- code-review: Systematic code review checklist.
- git-workflow: Common git branching and rebase patterns.
Layer 2 · 按需:模型要用某個 skill 時,調 load_skill(name="pdf"),完整 skill body(可能 5-10K token)透過 tool_result 塞進上下文。沒用到的 skill 一個 token 都不載入。
# tool_result 裡回傳完整 skill
<skill name="pdf">
Step 1: Use pdfplumber for extraction...
Step 2: Handle OCR fallback when needed...
Step 3: Structure output as Markdown table...
</skill>
對比 token 成本
真實場景測一下。假設你有 20 個 skill,每個 body 平均 3000 token。使用者問一個問題(比如「改一下登入介面的 bug」)——這個問題大概用不上任何 skill。
SKILL.md 的格式
skill 檔案用 YAML frontmatter + 正文:
--- name: pdf description: Process PDF files. Extract text, tables, metadata. tags: document,parsing --- Step 1: Use pdfplumber for extraction. Handle multi-column layouts... Step 2: For scanned PDFs, fall back to OCR via tesseract...
frontmatter 給 Layer 1 用(name/description/tags),正文給 Layer 2 用。這種寫法靈感來自靜態部落格(Jekyll、Hugo),熟悉的人一看就懂。