按需加載的領域知識
「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),熟悉的人一看就懂。