Claude Code 架构速览

Claude Code Architecture at a Glance

12 章分析的核心要点,可视化呈现。515,029 行代码背后每一层设计决策。

Core insights from 12 chapters, visually presented. Every design decision behind 515,029 lines of code.

1,906
源文件
Source Files
515K
行代码
Lines
40+
工具
Tools
80+
命令
Commands
92%
缓存命中
Cache Hit
17,885
安全代码行
Security Lines

章节导航

Chapter Navigation

启动优化:<200ms 冷启动

Boot Optimization: <200ms Cold Start

4 阶段启动流水线,import 阶段就开始并行 I/O。

4-stage boot pipeline. Parallel I/O starts during import evaluation.

预启动
Pre-boot
import 期间偷跑 3 路预取
3 prefetches during imports
快速路由
Fast Dispatch
11 条快速路径分发
11 fast-path routes
配置加载
Config Load
设置 + 密钥链 + Feature Flag
Settings + Keychain + Flags
进入主循环
Enter Main Loop
TUI 渲染 + Agent 就绪
TUI render + Agent ready

核心技巧:在 ~135ms 的 import 求值窗口内,同步启动 MDM 注册表读取、密钥链 OAuth 查询、API Key 查询这三路 I/O。等 import 完成时,数据已经在内存中了。--version 在 <1ms 内返回——零 import 的编译时常量。

Core trick: during the ~135ms import evaluation window, three I/O streams launch in parallel — MDM registry read, Keychain OAuth lookup, API key lookup. By the time imports finish, data is already in memory. --version returns in <1ms — zero-import build-time constant.

反调试保护:Anti-debugging: 外部构建版本检测 debugger(execArgv + NODE_OPTIONS + inspector),发现即无声退出。Bun 和 Node.js 有不同的 argv 泄露行为,分别处理。 External builds detect debuggers (execArgv + NODE_OPTIONS + inspector) and silently exit. Bun and Node.js have different argv leaking behaviors — handled separately.
Full Detail: Ch00 入口与启动 →Full Detail: Ch00 Entry & Startup →

核心秘密:一个 While 循环驱动一切

The Core Secret: One While Loop Drives Everything

没有 DAG,没有规划器。模型自己决定下一步。

No DAGs, no planners. The model decides the next step.

用户输入User Input
调 APICall API
流式回复Stream Reply
执行工具Run Tools
继续?Continue?
↑ 是 → 回到"调 API"  |  否 → 返回结果 ↓↑ Yes → back to "Call API"  |  No → return result ↓

7 种继续原因

7 Continue Reasons

PTL 恢复、MOT 升级、Stop Hook、Token 预算、工具执行……每种有独立状态机

PTL recovery, MOT escalation, stop hooks, token budget, tool execution... each with its own state machine

流式工具执行

Streaming Execution

API 还在流式输出时,工具已经开始执行。只读工具并行,写入工具串行

Tools start executing while API is still streaming. Read-only tools run in parallel, write tools run serially

三层 AbortController

3-Layer AbortController

queryLoop → siblingAbort → toolAbort 层级取消,兄弟错误不影响父级

queryLoop → siblingAbort → toolAbort hierarchy. Sibling errors don't affect parent

错误先藏后决:Withhold-then-Decide: 可恢复错误(PTL/MOT)不立即暴露给消费者。先尝试恢复(最多 3 层递进),恢复失败才暴露。防止 SDK 消费者在恢复过程中提前终止会话。 Recoverable errors (PTL/MOT) are withheld from consumers. Recovery is attempted first (up to 3 levels), only exposed on failure. Prevents SDK consumers from terminating sessions during recovery.
Full Detail: Ch01 Agent Loop →Full Detail: Ch01 Agent Loop →

Prompt 经济学:92% 缓存命中率

Prompt Economics: 92% Cache Hit Rate

把 System Prompt 当缓存系统来设计,每次 API 调用省 90% 的钱。

Design System Prompt as a caching system — save 90% on every API call.

计费头
Billing
~30 tok
|
CLI 前缀
CLI Prefix
~25 tok
|
静态区 (Cached)
Static Zone (Cached)
身份 | 规则 | 工具 | 风格 — 全局缓存
Identity | Rules | Tools | Style — globally cached
|
动态区
Dynamic Zone
环境 | 记忆 | MCP
Env | Memory | MCP
2^4 位爆炸:2^4 Bit Explosion: 4 个条件 bit(AskUserQuestion/Agent/Explore/Skill 可用性)若放进静态区 = 16 种前缀 hash = 16 份缓存 = 命中率崩溃。解法:__DYNAMIC_BOUNDARY__ 分界线把它们推到动态区。 4 conditional bits (AskUserQuestion/Agent/Explore/Skill availability) in static zone = 16 prefix hashes = 16 cache entries = hit rate collapse. Fix: __DYNAMIC_BOUNDARY__ pushes them into the dynamic zone.
MCP Delta: MCP 服务器指令不可预测,每次变更会使整个缓存失效。解法:只附加变化部分到最新消息块(而非系统 prompt 中),缓存前缀不受影响。 MCP server instructions are unpredictable — each change would bust the entire cache. Fix: attach only deltas to the latest message block (not the system prompt), keeping the cached prefix intact.
Full Detail: Ch02 System Prompt →Full Detail: Ch02 System Prompt →

40+ 工具:AI 的手和脚

40+ Tools: AI's Hands and Feet

模型能"想",工具让它能"做"。每个工具默认不安全(fail-closed)。

The model can "think"; tools let it "do". Every tool defaults to unsafe (fail-closed).

📄

Read / Write / Edit

读写文件

Read and write files

Bash

18 文件 23 项安全检查

18 files, 23 security checks

🔍

Grep / Glob

搜索代码和文件

Search code and files

🤖

Agent

生成子 Agent 并行工作

Spawn sub-agents in parallel

🌐

WebSearch / Fetch

互联网搜索和抓取

Internet search and fetch

🔌

MCP Tools

无限外部工具接入

Unlimited external tools

输入验证Input Validation阻止 sleep 模式、解析命令结构Block sleep patterns, parse command structure
ASTtree-sitter 解析 + 4 种白名单结构 + 16 种危险类型tree-sitter parse + 4 allowlisted types + 16 dangerous types
权限链Permission Chain子命令拆分 → allow/deny 规则 → 路径边界校验Subcommand split → allow/deny rules → path boundary check
沙箱隔离Sandbox120-600s 超时 + 大输出持久化(>30K → 磁盘)120-600s timeout + large output persistence (>30K → disk)
延迟加载:Deferred loading: MCP 工具和非常用内置工具(NotebookEdit、WebFetch 等)延迟到 ToolSearch 请求时才加载 schema。核心工具(Bash/Read/Edit/Write/Grep/Glob/Agent)始终加载。ToolSearch 使用加权评分:完全匹配 +12,部分匹配 +6。 MCP tools and uncommon built-ins (NotebookEdit, WebFetch, etc.) defer schema loading until ToolSearch requests them. Core tools (Bash/Read/Edit/Write/Grep/Glob/Agent) always loaded. ToolSearch uses weighted scoring: exact match +12, partial +6.
Full Detail: Ch03 工具系统 →Full Detail: Ch03 Tool System →

90+ 斜杠命令:6 路合并管线

90+ Slash Commands: 6-Source Merge Pipeline

每条命令 metadata 启动时加载,实现懒加载到使用时。

Command metadata loads at startup, but implementations lazy-load on use.

内置技能
Built-in Skills
技能目录
Skill Dir
工作流
Workflows
插件
Plugins
合并
Merge
Array.find()

6 个来源按优先级合并——用户/插件命令覆盖内置命令。可用性检查不缓存(因为认证状态会在 /login 后变化)。最大的模块 /insights 有 113KB / 3,200 行,使用双层懒加载 shim。

6 sources merge by priority — user/plugin commands override built-ins. Availability checks are not memoized (auth state changes after /login). The largest module /insights (113KB / 3,200 lines) uses a double-layer lazy shim.

Prompt 命令注入:Prompt command injection: Prompt 模板中的 ```!```!`cmd` 语法在发送给模型前执行 shell 命令并替换为输出。安全层检查权限后才执行,且用函数替换(非字符串替换)防止 $$ / $& 腐蚀 shell 输出。 Prompt templates use ```!``` and !`cmd` syntax — shell commands execute and get replaced with output before sending to the model. Security layer checks permissions first, and uses function replacers (not string replacement) to prevent $$ / $& from corrupting shell output.
Full Detail: Ch04 命令系统 →Full Detail: Ch04 Command System →

Token 预算:三层递进压缩

Token Budget: 3-Tier Progressive Compression

从免费到昂贵,自动管理 200K-1M Token 上下文窗口。

From free to expensive, auto-managing the 200K-1M token context window.

微压缩
Micro
零 API 调用 | 清理旧结果
Zero API cost | clean old results
会话记忆
Session Memory
复用异步摘要 | ~10-40K tokens
Reuse async summaries | ~10-40K tok
全量压缩
Full Compact
完整 API 调用 | 9 段结构化摘要
Full API call | 9-section summary
熔断器
Breaker
3 次连续失败 → 停止
3 consecutive fails → stop

200K 窗口

200K Window

83.5% (167K) 触发自动压缩。有效窗口 = 总窗口 - min(maxOutput, 20K)

83.5% (167K) triggers auto-compaction. Effective = total - min(maxOutput, 20K)

1M 窗口

1M Window

96.7% (967K) 才触发。更大的窗口意味着更晚的压缩、更低的信息损失

96.7% (967K) to trigger. Larger window = later compression = less info loss

精确 + 估算混合计数:Precise + Estimation hybrid: 找到最近的 API 响应用量数据(精确),然后对更新的消息用 length/4 估算。并行工具调用需要回溯查找共享 message.id 以包含交错的 tool_results。 Find the latest API usage data (precise), then estimate newer messages at length/4. Parallel tool calls require backtracking to find shared message.id to include interleaved tool_results.
Full Detail: Ch05 上下文管理 →Full Detail: Ch05 Context Management →

安全:17,885 行代码 + 23 道检查

Security: 17,885 Lines + 23 Validators

Fail-Closed 哲学:无法证明安全的一律拦截。

Fail-Closed philosophy: anything not provably safe gets blocked.

命令输入
Command
tree-sitter
AST 白名单解析
AST allowlist parse
23 验证器
23 Validators
注入 | IFS | 编码 | 括号
Injection | IFS | Encoding | Braces
权限规则
Permission Rules
沙箱执行
Sandbox Run

双引擎解析

Dual-Engine Parse

tree-sitter(主引擎,AST 白名单 4 种结构 + 16 种危险类型)+ shell-quote(备用引擎)。"太复杂"时降级到 shell-quote

tree-sitter (primary, 4 structural + 16 dangerous types) + shell-quote (fallback). "Too complex" downgrades to shell-quote

HackerOne

5 个真实渗透测试发现被修补:IFS 注入、CR 注入、反斜杠双重解析、括号展开混淆、换行符隐藏攻击

5 real pentest findings patched: IFS injection, CR injection, backslash double-parse, brace expansion, quoted newline attacks

沙箱白名单

Sandbox Allowlist

可写:cwd + Claude 临时目录 + --add-dir 路径。永远禁止:settings.json(防止沙箱逃逸)

Write: cwd + Claude temp + --add-dir paths. Always denied: settings.json (prevents sandbox escape)

27 种 Hook 事件:27 Hook Event Types: PermissionRequest Hook 可输出 allow/deny 覆盖权限检查。工具 Hook 10 分钟超时,会话结束 Hook 仅 1.5 秒。策略可强制"仅托管 Hook"或禁用所有。 PermissionRequest hooks can output allow/deny to override permission checks. Tool hooks get 10-minute timeout, session end hooks only 1.5s. Policy can enforce "managed hooks only" or disable all.
Full Detail: Ch06 权限与安全 →Full Detail: Ch06 Permission & Security →

多 Agent 协作:6 种模式

Multi-Agent: 6 Operating Modes

从简单的子 Agent 到完整的团队协作,按需选择隔离级别。

From simple sub-agents to full team collaboration — choose isolation level as needed.

Teammate最高优先级 — tmux/iTerm2/进程内多 Agent 并行Highest priority — tmux/iTerm2/in-process parallel agents
Fork继承父对话上下文 + 字节级 prompt 缓存共享Inherits parent context + byte-level prompt cache sharing
Worktree独立 Git 分支 — 5 点冲突预防(分支/目录/标志/展平/根查找)Isolated Git branch — 5-point conflict prevention (branch/dir/flag/flatten/root)
Coordinator"永远不要委托理解" — ~370 行系统 prompt 强制综合而非转发"Never delegate understanding" — ~370-line prompt enforces synthesis over forwarding
Async / Sync后台异步或阻塞同步 — 默认同步,run_in_background=true 切异步Background async or blocking sync — default sync, run_in_background=true for async
Fork Agent: 最精妙的设计——复用父 Agent 渲染后的系统 prompt 字节(而非重新编译),使用 useExactTools: true 确保工具池完全一致。父子仅在最后一条 <fork-directive> 处分叉,前缀缓存完全共享。通过 isInForkChild() 禁止嵌套 Fork。 Most elegant design — reuses parent's rendered system prompt bytes (not recompiled), with useExactTools: true for identical tool pools. Parent and child diverge only at the final <fork-directive>, sharing all prefix cache. isInForkChild() prevents nested forks.
团队通信:Team Communication: 基于文件系统的邮箱 .claude/teams/{team}/mailbox/{name}/。4 种寻址:名字、* 广播、UDS 跨会话、Bridge 跨机器。不主动检查收件箱——消息自动投递。 Filesystem-based mailbox at .claude/teams/{team}/mailbox/{name}/. 4 addressing modes: name, * broadcast, UDS cross-session, Bridge cross-machine. No active polling — messages delivered automatically.
Full Detail: Ch07 多 Agent 协作 →Full Detail: Ch07 Multi-Agent →

MCP 协议:8 种传输 + 4 个 API 后端

MCP Protocol: 8 Transports + 4 API Backends

统一协议层让 Claude Code 接入任意外部工具和云服务。

A unified protocol layer connects Claude Code to any external tool or cloud service.

stdio

本地进程

Local process

SSE

服务器推送

Server events

HTTP

请求响应

Request-response

WebSocket

双向通道

Bidirectional

Pending
Connected
/
Failed
/
Needs Auth
/
Disabled

连接批处理:本地服务器 3 路并发,远程服务器 20 路并发(6.7x 差异,优化网络 vs 子进程开销)。指数退避 1s → 30s。401 自动刷新 Token,403 + WWW-Authenticate 触发 Step-up 认证。

Connection batching: local servers 3-way concurrency, remote 20-way (6.7x difference, optimizing network vs. subprocess overhead). Exponential backoff 1s → 30s. 401 auto-refreshes tokens, 403 + WWW-Authenticate triggers step-up auth.

4 个 API 后端

4 API Backends

getAnthropicClient() 根据环境变量透明切换:Anthropic 直连、AWS Bedrock、Azure Foundry、Google Vertex AI

getAnthropicClient() transparently switches by env vars: Anthropic direct, AWS Bedrock, Azure Foundry, Google Vertex AI

XAA OAuth

跨应用访问:一次登录 IdP → RFC 8693 Token 交换 → 自动认证 N 个 MCP 服务器,无需重复弹窗

Cross-App Access: one IdP login → RFC 8693 Token Exchange → auto-auth N MCP servers, no repeated popups

Full Detail: Ch08 MCP 与服务层 →Full Detail: Ch08 MCP & Services →

终端里的 React 应用:389 个组件

React App in the Terminal: 389 Components

不是 curses,是一个真正的 React 渲染引擎 + 双缓冲 + 对象池。

Not curses — a real React rendering engine + double buffering + object pools.

React + Ink389 组件 + React Compiler 自动 memoization389 components + React Compiler auto-memoization
Reconciler自定义协调器 → DOM 树 → Yoga 布局(Flexbox)Custom reconciler → DOM tree → Yoga layout (Flexbox)
双缓冲Double Buffer前帧/后帧逐 cell diff,Packed Int 优化(charId + styleId 合并)Front/back frame cell-by-cell diff, packed int (charId + styleId merged)
对象池Object PoolsCharPool(ASCII O(1))+ StylePool(Bit 0 跳过空格)+ HyperlinkPoolCharPool (ASCII O(1)) + StylePool (Bit 0 space skip) + HyperlinkPool
ANSI → stdoutDECSTBM 硬件滚动 + OSC 52 剪贴板协议DECSTBM hardware scrolling + OSC 52 clipboard protocol
REPL.tsx — "上帝组件",5,005 行、280+ 导入。终端没有路由,REPL 就是唯一的"页面"。这是有意为之——React Compiler 用 _c() 缓存数组对整个函数体做细粒度自动 memoization,即使规模巨大也不会性能崩溃。 "God Component" — 5,005 lines, 280+ imports. Terminals have no routing, so REPL is the sole "page." This is intentional — React Compiler transforms the entire function body with _c() cache arrays for fine-grained auto-memoization, preventing performance collapse despite massive scale.
Full Detail: Ch09 UI 组件系统 →Full Detail: Ch09 UI Components →

88 个 Feature Flag:隐藏的未来

88 Feature Flags: The Hidden Future

三层 Flag 架构控制从实验功能到产品形态的一切。

3-tier flag architecture controls everything from experiments to product evolution.

构建时
Build-time
feature('FLAG')
Bun DCE
运行时
Runtime
GrowthBook
tengu_* namespace
环境变量
Env Vars
USER_TYPE
CLAUDE_CODE_*

KAIROS

助理模式:7 个子 Flag 联动。SleepTool 无限等待不阻塞、Dreaming 4 阶段记忆整理(定向→收集→合并→修剪)、三重门控防并发

Assistant mode: 7 interlocking sub-flags. SleepTool waits indefinitely without blocking, Dreaming 4-phase memory consolidation (Orient→Gather→Consolidate→Prune), triple-gated concurrency prevention

Buddy

电子宠物!18 种物种、5 级稀有度(60% 普通 → 1% 传说)、确定性 Hash 生成。一个物种名用 String.fromCharCode() 编码以规避 canary 检测

Virtual pet! 18 species, 5 rarity tiers (60% common → 1% legendary), deterministic hash generation. One species name encoded with String.fromCharCode() to evade canary detection

Undercover

卧底模式:提交到非内部仓库时自动激活,隐藏模型代号/项目名/AI 身份。无法关闭——安全优先于透明

Stealth mode: auto-activates on non-internal repos, hides model codenames/project names/AI identity. Cannot be disabled — security over transparency

内部 vs 外部

Internal vs External

内部版更啰嗦、有主见、挑战假设。tengu_ 前缀证实 "Tengu" 是 Claude Code 内部代号。3 级 GrowthBook SDK Key

Internal version: verbose, opinionated, challenges assumptions. tengu_ prefix confirms "Tengu" is Claude Code's internal codename. 3-tier GrowthBook SDK keys

Full Detail: Ch10 Feature Flags →Full Detail: Ch10 Feature Flags →

基础设施:35 行 Store + 7 种 Task

Infrastructure: 35-Line Store + 7 Task Types

最小化核心 + 集中式副作用 = 可测试的基础层。

Minimal core + centralized side effects = testable foundation layer.

local_bash

前缀 b

Prefix b

local_agent

前缀 a

Prefix a

remote_agent

前缀 r

Prefix r

teammate

前缀 t

Prefix t

workflow

前缀 w

Prefix w

monitor_mcp

前缀 m

Prefix m

dream

前缀 d

Prefix d

Task ID = 前缀 + 8 位 base36 随机字符(2.8 万亿种组合,防 symlink 碰撞攻击)。

Task ID = prefix + 8 base36 random chars (2.8 trillion combinations, preventing symlink collision attacks).

35 行 Store

35-Line Store

比 Redux 更极简:setState(updater) + Object.is 引用比较 + TypeScript DeepImmutable<T> 编译时不可变约束。570 行 AppState 含 100+ 字段跨 13 个功能域

More minimal than Redux: setState(updater) + Object.is reference comparison + TypeScript DeepImmutable<T> compile-time immutability. 570-line AppState with 100+ fields across 13 domains

集中副作用

Centralized Side Effects

onChangeAppState:一个函数处理 7 种副作用(权限同步、模型持久化、详细模式、认证缓存清理……),替代 8+ 个分散的通知路径

onChangeAppState: one function handles 7 side effects (permission sync, model persistence, verbose toggle, auth cache cleanup...) replacing 8+ scattered paths

Vim Mode: 完整的两层状态机(INSERT/NORMAL)+ 11 种 CommandState 子状态。纯函数架构——motions.ts(输入: 键+光标+计数, 输出: Cursor)、operators.ts、textObjects.ts、transitions.ts。每层可独立测试。Dot-repeat 支持 10 种操作类型。 Full two-level state machine (INSERT/NORMAL) + 11 CommandState substates. Pure function architecture — motions.ts (input: key+cursor+count, output: Cursor), operators.ts, textObjects.ts, transitions.ts. Every layer testable in isolation. Dot-repeat supports 10 operation types.
Full Detail: Ch11 基础设施 →Full Detail: Ch11 Infrastructure →

总结:Claude Code 的设计哲学

Summary: Claude Code's Design Philosophy

简单核心 + 复杂工程

Simple Core + Complex Engineering

核心是一个 while 循环,但围绕它有 515K 行优化代码

The core is a while loop, but 515K lines of optimization surround it

模型决策,系统执行

Model Decides, System Executes

不预设流程,让 AI 自己判断下一步

No preset workflows — let AI decide the next step

安全优先 (Fail-Closed)

Security First (Fail-Closed)

17,885 行安全代码,不确定就拦截

17,885 lines of security code — when in doubt, block it

成本敏感

Cost Sensitive

92% 缓存命中、三层压缩、动态区隔离防缓存击穿

92% cache hit, 3-tier compression, dynamic zone isolation prevents cache busting

并行化一切

Parallelize Everything

Import 期间预取、流式工具执行、Fork Agent 缓存共享

Prefetch during imports, streaming tool execution, Fork Agent cache sharing

分层预算控制

Tiered Budget Control

每工具限额 + 系统级上限 + 聚合预算,层层兜底

Per-tool limit + system cap + aggregate budget — defense in depth

阅读完整 12 章分析 → Read Full 12-Chapter Analysis →