Claude Code 源碼深度研究:為什麼它更像 Agent Operating System
核心結論
Claude Code 的強,不是來自某個「神秘 system prompt」,而是來自一個完整的軟體工程系統:
- Prompt 不是靜態文本,而是模組化 runtime assembly
- Tool 不是直接裸調,而是走 permission / hook / analytics / MCP-aware execution pipeline
- Agent 不是一個萬能 worker,而是多種 built-in / fork / subagent 的分工系統
- Skill 不是說明文檔,而是 prompt-native workflow package
- Plugin 不是外掛,而是 prompt + metadata + runtime constraint 的擴展機制
- MCP 不是單純工具橋,而是同時能注入工具與行為說明的 integration plane
一句話總結:這類成熟 Agent 產品的價值,不是一段 prompt,而是一整套把 prompt、tool、permission、agent、skill、plugin、hook、MCP、cache 和產品體驗統一起來的 Agent Operating System。
源碼結構全景
成熟 Coding Agent 至少包含這些重要模組:
src/entrypoints/:入口層(CLI、init、MCP、SDK)src/constants/:prompt、系統常量、風險提示src/tools/:工具定義與實作src/services/:運行時服務(tools、mcp、analytics)src/coordinator/:協調器模式src/memdir/:記憶 / memory promptsrc/plugins/+src/hooks/:插件與 Hook 生態src/tasks/:本地、遠端、異步 agent 任務
關鍵洞察:多個入口(CLI / SDK / MCP)共用同一個 agent runtime,這是平台化思維,不是單一 CLI 包裝器。
Prompt 架構:System Prompt Assembly
getSystemPrompt() 不是靜態字串,而是一個編排器,分為:
靜態前綴(適合 KV cache)
getSimpleIntroSection()getSimpleSystemSection()getSimpleDoingTasksSection()getActionsSection()getUsingYourToolsSection()getSimpleToneAndStyleSection()
動態後綴(按會話條件注入)
- session guidance
- memory
- env info
- language / output style
- MCP instructions
- token budget / brief / scratchpad
這個設計把 prompt 當作可編排的運行時資源管理,而不是「把能想到的都寫進去」。
做任務的哲學(穩定性來源)
System prompt 明確規定:
- 不要亂加功能
- 不要過度抽象
- 不要亂重構
- 先讀程式碼再改
- 不要輕易創建新檔案
- 結果要誠實匯報
這些規定讓它把「好習慣」制度化,而不是靠模型即興發揮。
Built-in Agents 分工
內建 agents 至少包括:
- General Purpose Agent
- Explore Agent
- Plan Agent
- Verification Agent(主動去驗證、打破實現,要求 build + tests + type-check + 真實命令輸出)
不是萬能 worker,而是通過角色分工提高穩定性。
Tools 執行鏈(不是裸調)
schema 校驗
→ validateInput
→ pre-tool hooks
→ permission decision
→ tool call
→ telemetry
→ post-tool hooks
→ failure hooks
這也是它比很多「會調工具的 Agent」更穩定的重要原因。
Skills / Plugins / Hooks / MCP 的本質
| 元件 | 本質 |
|---|---|
| Skill | 可復用的 workflow package,prompt-native |
| Plugin | prompt + metadata + runtime constraints 組合包 |
| Hook | 運行時治理層,可改輸入、給權限建議、阻止執行 |
| MCP | 工具橋 + 行為說明注入的 integration plane |
為什麼這麼強
核心原因是把這些東西系統化:
- Prompt architecture
- Tool runtime governance
- Permission model
- Agent specialization
- Skill workflow packaging
- Plugin / MCP extensibility
- Context hygiene
- Async/background lifecycle
真正厲害的不是某一句 prompt,而是整個 operating model。
延伸資源
- GitHub 報告:tvytlx/ai-agent-deep-dive
- PDF 完整報告:ai-agent-deep-dive-report.pdf