Anthropic Claude Code 架构详解:六层系统与多智能体协作机制
Claude Code’s architecture, explained visually:
Agent 开发者必读,清晰拆解了 Claude Code 的多智能体协作与上下文管理机制,对理解 Agent 工程实践极具参考价值。
Claude Code’s architecture, explained visually:
Claude Code 的架构,图解说明:
(bookmark this)
(收藏此页)
Claude Code is a lot more than a CLI that invokes the Claude models.
Claude Code 远不止是一个调用 Claude 模型的 CLI。
The actual system has six layers, and the model is just one node inside the loop.
实际系统包含六个层级,而模型只是循环中的一个节点。
The diagram below explains every component:
下图解释了每个组件:
1) Input layer handles session management, permission gating, and YAML-based trust tiers before anything reaches the model.
1) 输入层在处理任何内容到达模型之前,负责会话管理、权限门控以及基于 YAML 的信任层级。
2) Knowledge layer holds the skill registry, context compressor, task graph, and cross-session memory store. This is where harness intelligence lives outside the weights.
2) 知识层包含技能注册表、上下文压缩器、任务图和跨会话内存存储。这是“驾驭智能”存在于权重之外的地方。
The context compressor is a 5-layer cascade that kicks in when the context window hits roughly 95% capacity. It doesn’t summarize your conversation the way ChatGPT does. Instead, it runs structured extraction on file paths, code snippets, and error histories while pruning redundant tool outputs. The goal is to keep the context usable, not just smaller.
上下文压缩器是一个 5 层级联结构,当上下文窗口达到约 95% 容量时触发。它不像 ChatGPT 那样对你的对话进行总结。相反,它对文件路径、代码片段和错误历史运行结构化提取,同时剪枝冗余的工具输出。其目标是保持上下文的可用性,而不仅仅是缩小体积。
3) Execution layer runs tool dispatch through a typed registry with one handler per tool, like bash, read, write, grep, glob, and revert.
3) 执行层通过类型化注册表运行工具分发,每个工具对应一个处理程序,例如 bash、read、write、grep、glob 和 revert。
The streaming runtime handles parallel execution, and the prompt cache reuses stable prefixes at roughly 10% of the original cost.
流式运行时处理并行执行,提示缓存以原始成本的大约 10% 重用稳定的前缀。
4) Integration layer connects the MCP runtime to external servers (filesystem, git, custom). Tools register inward, and memory writes outward to a markdown file (agent_memory.md) that persists across sessions.
4) 集成层将 MCP 运行时连接到外部服务器(文件系统、git、自定义)。工具向内注册,内存向外写入 markdown 文件(agent_memory.md),该文件在会话之间持久存在。
5) Multi-agent layer is the most underappreciated piece, and it works very differently from what most people assume.
5) 多智能体层是最被低估的部分,其工作方式与大多数人的假设截然不同。
Claude Code supports two levels of parallelism:
Claude Code 支持两个级别的并行性:
- Subagents are lightweight workers that run inside your session. They get their own context window, do a focused task (search the codebase, explore a file tree), and return results to the parent. They can’t talk to each other, and they can’t spawn their own subagents. It’s a strict parent-child hierarchy.
- Agent teams go further. One session acts as a team lead, and it spawns independent teammates, each running as a full Claude Code instance with its own context window. The team lead breaks a task into subtasks, assigns them, and monitors progress.
- 子代理是轻量级工作进程,在你的会话内运行。它们拥有自己的上下文窗口,执行聚焦的任务(搜索代码库、探索文件树),并将结果返回给父代理。它们不能互相通信,也不能生成自己的子代理。这是一个严格的父子层级结构。
- 智能体团队更进一步。一个会话充当团队领导,并生成独立的团队成员,每个成员都作为具有自己上下文窗口的完整 Claude Code 实例运行。团队领导将任务分解为子任务,分配它们并监控进度。
The coordination happens through two mechanisms → a shared task list (JSON files on disk) and a mailbox system for peer-to-peer messaging.
协调通过两种机制发生 → 共享任务列表(磁盘上的 JSON 文件)和用于点对点消息传递的信箱系统。
Each teammate gets git worktree isolation. It’s a separate working directory with its own branch, sharing the same repository history.
每个团队成员都获得 git worktree 隔离。它是一个独立的工作目录,拥有自己的分支,但共享相同的仓库历史记录。
This means agents can write to overlapping parts of the codebase without file conflicts. When they finish, worktrees with no changes are cleaned up automatically. Worktrees with changes persist for human review before merging.
这意味着代理可以写入代码库中重叠的部分,而不会产生文件冲突。完成后,没有更改的工作树会自动清理。有更改的工作树会在合并前保留供人工审查。
6) Observability layer wraps everything. An event bus with lifecycle hooks logs all tool calls and messages, creating a complete audit trail of the agent’s actions and decisions.
6) 可观测性层包裹一切。带有生命周期钩子的事件总线会记录所有工具调用和消息,创建代理行为和决策的完整审计轨迹。
Background executors run daemon threads non-blocking, so observability never stalls the main loop.
后台执行器以非阻塞方式运行守护线程,因此可观测性不会阻塞主循环。
Finally, the master agent loop sits at the center of all six layers. It assembles context, calls the model, receives a tool request, executes it, feeds the result back in, and repeats. Every iteration is one turn.
最后,主代理循环位于所有六层的中心。它组装上下文、调用模型、接收工具请求、执行该请求、将结果反馈回来,然后重复。每次迭代都是一轮。
Within a turn, the model might request a tool call. That request flows through the permission system, gets executed, and the output feeds back into the loop as the next input.
在一轮内,模型可能会请求工具调用。该请求通过权限系统,被执行后,输出作为下一轮输入反馈到循环中。
The loop itself is single-threaded on purpose. All the intelligence lives in the layers around it, not in the loop logic. Anthropic calls it a dumb loop because the model reasons, and the harness mediates.
循环本身故意是单线程的。所有智能都存在于围绕它的各层中,而非循环逻辑本身。Anthropic 称其为“简单循环”,因为推理由模型完成,而框架负责协调。
This is the architecture behind Claude Code.
这就是 Claude Code 背后的架构。
If you want to dive deeper into Claude Sugagents and agent teams specifically, I recently wrote an article about them.
如果你想深入了解 Claude 子代理和代理团队,我最近写了一篇关于它们的文章。
It explains the difference between Claude sub-agents (isolated, fire-and-forget workers) and agent teams (persistent, peer-communicating instances with a shared task list), and when to use each.
它解释了 Claude 子代理(隔离的、即发即忘的工作者)与代理团队(持久的、具有共享任务列表并相互通信的实例)之间的区别,以及何时使用每种模式。
Read it below.
请在下方阅读。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力