AI Agent 工程设计精选文章
整理时间:2026年3月9日 | 整理者:小龙虾 🦞
本文档收录关于 AI Agent 工程设计的核心文章,涵盖 Claude Code、OpenAI Codex、上下文管理与记忆机制等主题。每篇文章附有原文引用(Recitation)与核心要点。
一、Claude Code 工程设计
1.1 Introducing Claude Code(Anthropic 官方)
来源: Anthropic 官方博客 链接: https://www.anthropic.com/news/claude-code 发布时间: 2025年2月
Anthropic 发布 Claude Code,一个运行在终端的 agentic 编程工具。它直接操作文件系统、执行命令、访问代码库,并可与 GitHub 交互。
关键引用:
“Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing multi-step tasks autonomously.”
核心设计要点:
- 工具调用架构: 核心工具包括 bash 执行、文件读写、grep 搜索、git 操作
- Permission-based 设计: 敏感操作(如写文件、执行命令)需要用户显式确认
- 系统提示驱动: 包含详细的操作原则、安全边界与角色定义
- Agentic Loop: 观察(Observe)→ 思考(Think)→ 行动(Act)→ 观察的循环
1.2 Claude Code 系统提示分析(社区)
来源: Anthropic 发布 + 社区研究者整理
Anthropic 官方公开了 Claude Code 的完整系统提示,引发大量社区分析。
系统提示核心结构:
| 层次 | 职责 |
|---|---|
| 角色层 | 明确 Claude Code 是专注工程任务的 AI assistant,有别于通用 Claude |
| 工具声明层 | 显式列出所有可用工具及调用规范 |
| 安全护栏层 | 对危险操作(rm -rf、网络外发等)有明确约束 |
| 行为准则层 | 包括 proactive clarification、minimal footprint 等原则 |
关键设计原则引用:
“The assistant should request only necessary permissions, avoid storing sensitive information beyond immediate needs, prefer reversible over irreversible actions, and err on the side of doing less and confirming with users when uncertain about intended scope.”
1.3 Building Effective Agents(Anthropic Engineering)
来源: Anthropic Engineering Blog 链接: https://www.anthropic.com/engineering/building-effective-agents 发布时间: 2024年12月
这是 Anthropic 工程团队总结的 Agent 构建最佳实践,非常值得精读。
关键引用:
“The most important lesson from deploying agents is to start simple. Many tasks that seem to require complex multi-agent architectures can be solved with simpler approaches.”
五大核心工作流模式:
- Prompt Chaining — 任务分解为顺序步骤,每步输出作为下一步输入,适合流水线处理
- Routing — 将输入分类并导向不同专用处理流程,提高专项能力
- Parallelization — 并行执行独立子任务后汇总,或多模型投票提升可靠性
- Orchestrator-Subagents — 主 Agent 分解任务并调度子 Agent 专项执行
- Evaluator-Optimizer — 生成 Agent + 评估 Agent 的迭代优化循环
Agent 适用场景判断:
“Agents are valuable when tasks require dynamic decision-making, when the right sequence of steps is hard to specify upfront, or when the task benefits from checking intermediate results.”
二、OpenAI Codex 工程设计
2.1 Introducing Codex(OpenAI 官方)
来源: OpenAI 官方博客 链接: https://openai.com/blog/introducing-codex 发布时间: 2025年
OpenAI 发布新一代 Codex,定位为云端 AI 软件工程师,专为异步、长周期的编程任务设计,可并行处理多个任务。
核心设计特点:
| 特性 | 说明 |
|---|---|
| 沙箱隔离 | 每个任务运行在独立容器化沙箱,安全隔离互不影响 |
| 异步执行 | 无需用户实时监督,支持长周期后台任务 |
| 深度 GitHub 集成 | 自主提交 PR、创建分支、回应 code review |
| 有限网络访问 | 沙箱内仅可访问预设白名单资源,防止数据泄露 |
| 并行处理 | 支持同时推进多个独立任务,大幅提升效率 |
与 Claude Code 对比:
| 维度 | Claude Code | OpenAI Codex |
|---|---|---|
| 部署方式 | 本地终端 | 云端服务 |
| 交互模式 | 实时交互 | 异步任务 |
| 任务模型 | 单任务 | 多任务并行 |
| 运行环境 | 本机环境 | 隔离容器 |
三、AI Agent 通用工程设计
3.1 LLM Powered Autonomous Agents(必读经典)
作者: Lilian Weng(OpenAI) 来源: Lilian Weng’s Blog 链接: https://lilianweng.github.io/posts/2023-06-23-agent/ 发布时间: 2023年6月
这是目前关于 AI Agent 架构最系统全面的综述文章,作者为 OpenAI 研究员。
核心框架引用:
“A LLM-based autonomous agent system is comprised of: Planning, Memory, and Tool Use.”
三大组件深度分析:
规划(Planning): - Chain of Thought(CoT): 让模型显式写出推理步骤,提升复杂问题解决能力 - Tree of Thoughts(ToT): 将推理扩展为树形搜索,探索多条解题路径 - ReAct: 交替进行推理(Reason)和行动(Act),与外部环境实时交互 - Reflexion: 通过自我反思与语言反馈纠正过去错误,无需梯度更新
记忆(Memory): - Sensory Memory: 原始输入缓冲(对应感觉记忆) - Short-term Memory(In-context): 当前上下文窗口内的工作记忆 - Long-term Memory: 外部向量数据库,支持模糊语义检索
工具使用(Tool Use): - MRKL(模块化推理、知识、语言) - Toolformer: 模型自主学习何时调用 API - HuggingGPT: LLM 作为控制器,调度专业 AI 模型完成任务
3.2 ReAct: Synergizing Reasoning and Acting in Language Models
作者: Shunyu Yao et al.(Google Brain / Princeton) 链接: https://arxiv.org/abs/2210.03629 发布时间: 2022年10月
关键引用:
“ReAct prompts LLMs to generate both verbal reasoning traces and actions pertaining to a task in an interleaved manner, allowing the model to perform dynamic reasoning to create, maintain, and adjust high-level plans for acting.”
核心机制:
Thought → Action → Observation 的交替循环,将推理链与外部工具调用深度结合。显式推理步骤大幅提升可解释性。
在 HotpotQA、FEVER、ALFWorld 等任务上显著优于纯 Chain-of-Thought。
为什么重要: ReAct 是现代 AI Agent(包括 Claude Code、LangChain Agent 等)的基础范式之一。
四、上下文管理与记忆机制
4.1 MemGPT: Towards LLMs as Operating Systems
作者: Charles Packer et al.(UC Berkeley) 链接: https://arxiv.org/abs/2310.08560 发布时间: 2023年10月 会议: NeurIPS 2023 Workshop
这篇论文提出将操作系统内存分层机制应用于 LLM 上下文管理,是 Agent 记忆设计的重要参考。
核心论点引用:
“We propose MemGPT, a system that intelligently manages different memory tiers to effectively provide extended context within the LLM’s limited context window, drawing inspiration from traditional OS hierarchical memory systems.”
内存分层架构:
主上下文(In-Window,类比 RAM)包含: - System Instructions - Working Context(当前任务信息) - FIFO Queue(对话历史滚动窗口)
外部存储(类比 Disk)包含: - Archival Storage(向量检索归档) - Recall Storage(对话历史完整版)
核心机制: - 主动内存管理: 模型自主决定何时将信息存入/读取外部存储 - 中断驱动: 类似操作系统的事件驱动机制处理记忆操作 - 无缝扩展: 突破上下文窗口限制,支持无限长会话
4.2 Cognitive Architectures for Language Agents (CoALA)
作者: Theodore R. Sumers et al.(Princeton / Google DeepMind) 链接: https://arxiv.org/abs/2309.02427 发布时间: 2023年9月
一篇将认知科学与 LLM Agent 设计系统结合的重要综述。
关键引用:
“We present a systematic framework for thinking about and building language agents, drawing on insights from cognitive science, AI planning, and recent work on LLM-based agents.”
记忆系统四分类框架:
| 记忆类型 | 描述 |
|---|---|
| Episodic Memory(情节记忆) | 过去经历的具体事件,可按时序检索 |
| Semantic Memory(语义记忆) | 通用知识与事实,不依附于具体情境 |
| Procedural Memory(程序记忆) | 技能与操作方法,体现为行为模式 |
| Working Memory(工作记忆) | 当前 context 中的活跃信息,容量有限 |
行动空间分类: - 外部行动: 与环境交互(工具调用、API 请求) - 内部行动: 推理、检索、记忆写入/读取 - 模型存储: 参数更新(fine-tuning)
4.3 A Survey on Large Language Model based Autonomous Agents
作者: Lei Wang et al.(Renmin University of China) 链接: https://arxiv.org/abs/2308.11432 发布时间: 2023年8月
最全面的 LLM Agent 综述之一,覆盖构建、应用、评估三大维度。
关键引用:
“We propose a unified framework to systematically organize and understand the field of LLM-based autonomous agents, encompassing the construction, application, and evaluation of such agents.”
Agent 构建三大支柱: - Profiling Module: 定义 Agent 的角色、能力与限制 - Memory Module: 设计记忆的存储、检索与更新机制 - Action Module: 规划行动序列并与环境交互
五、OpenClaw 工程设计深度解析
本章综合 OpenClaw 官方文档、源码分析及多篇专业技术博客,对其核心工程设计进行系统梳理。
参考来源: - 官方文档:https://docs.openclaw.ai - Towards AI 深度分析:OpenClaw Architecture Deep Dive - Memory 系统深度解析:Deep Dive: How OpenClaw’s Memory System Works - 三层架构技术原理:Deep Dive into OpenClaw Architecture - 系统架构概览:OpenClaw System Architecture Overview - Medium 实战分析:How OpenClaw Works
5.1 三层架构总体设计
OpenClaw 的核心设计哲学是将 AI 视为基础设施问题,而非单纯的应用层封装。它采用职责分离清晰的三层架构:
架构设计哲学引用(eastondev.com):
“OpenClaw divides the entire system into three layers, each managing its own concerns… Without layering, all logic piled together means changing one place might affect everything — completely unmaintainable.”
三层职责划分:
① Gateway 层(会话管理中枢) - 管理用户会话的完整生命周期 - 消息队列与调度(谁先处理) - 身份验证与权限控制 - WebSocket 持久连接维护 - Single-Writer 架构: 每个 Session 同一时间只允许一个 Agent run,防止并发写入导致状态竞争
② Channel 层(平台适配器) - 适配各平台消息格式(WhatsApp / Telegram / Discord / Feishu 格式各异) - 消息路由规则(私信 or 群聊,是否需要 @) - Adapter Pattern: 将多平台输入标准化为统一内部格式,解耦 Gateway 与 LLM 层 - 每个 Channel+用户组合独立隔离 Session,防止跨平台上下文混淆
③ LLM 层(模型接口) - 统一 Provider 接口(Claude / GPT / 本地模型调用方式一致) - Tool Calling(Function Calling) - 流式响应处理 - MCP Server 集成 - Provider Plugin System(2026年重构): 支持动态注册不同模型 Provider
完整消息流转(以 Feishu 为例): 1. Channel 层接收 webhook → 标准化消息格式 2. 路由决策:私聊 or 群聊,权限校验 3. Gateway 找到(或创建)该用户 Session,消息入队 4. LLM 层按配置选择 Provider,发送对话上下文 5. 模型返回结果 → Channel 层格式化回包
5.2 记忆系统深度解析
OpenClaw 的记忆系统是其最具工程价值的设计之一,其核心哲学是文件优先(File-First)——Markdown 文件是 Source of Truth,LLM 的 Context Window 仅是动态缓存。
核心设计引用(snowan.gitbook.io):
“Unlike traditional RAG systems that rely on vector databases, OpenClaw takes a file-first approach: Markdown files are the source of truth, and the memory system is designed to help AI agents remember context across conversations.”
三层记忆结构:
① 长期精华记忆(MEMORY.md) - 存储 Agent 的偏好、操作原则、持久化知识 - 仅在私密主会话中加载,防止隐私泄露至群聊/子 Agent - 人工可读、可编辑,具有极高透明度
② 每日日志(memory/YYYY-MM-DD.md) - Append-only 追加写入,记录当日活动、决策、对话摘要 - Session 启动时自动加载今日 + 昨日日志 - 类比工作记忆的滚动窗口
③ Session 记忆(SQLite + Vector Embeddings) -
存储位置:~/.openclaw/memory/<agentId>.sqlite -
对话历史自动建立向量索引,支持语义检索 - 按 Agent ID 隔离,每个 Agent
独立知识库
核心实现:MemoryIndexManager
1 | export class MemoryIndexManager { |
关键工程决策: - Singleton + 缓存: 防止重复建索引(INDEX_CACHE) - 文件监听: Debounced 文件变更同步,实时更新索引 - Provider 降级链: 本地 → OpenAI → Gemini → Voyage,优雅降级 - Delta-Based 增量同步: 只处理变更部分,优化性能 - SHA-256 去重: 避免对未变更内容重复 Embedding
混合检索(Hybrid Retrieval): - BM25 关键词检索 + 向量语义检索 双路并行 - 结合精确关键词匹配与语义理解,召回率显著优于单一方式
Markdown 分块算法:
OpenClaw 使用滑动窗口 + 重叠保留算法对 Markdown 内容分块:
1 | export function chunkMarkdown( |
自动压缩与 Memory Flush: - 当 Context Window 接近上限时,自动压缩(Compaction)旧对话 - Pre-Compaction Flush: 压缩前触发记忆写入,确保重要信息不丢失 - 类似操作系统的 Page Flush 机制
5.3 Skill 热插拔系统
技能即配置的设计哲学:
每个技能通过 SKILL.md
自描述,支持触发条件声明与调用规范定义。内置技能包括:feishu-doc、feishu-wiki、weather、healthcheck、skill-creator
等,社区技能通过 ClaWHub(https://clawhub.com)分发。
设计优势: - 自描述接口:
SKILL.md 定义触发意图、工具调用规范、示例 -
按需加载: 仅在意图匹配时读取
SKILL.md,零固定 Context 占用 -
可扩展生态: 用户可安装社区
Skill,不需要修改核心代码
安全隐患(Towards AI 警示):
“Security researchers found 400+ malicious plugins in its marketplace within two minutes of looking.”
这一事件凸显了 Agent 插件生态的供应链安全风险,是 AI Agent 工程的重要教训。
5.4 多会话 / 子 Agent 架构
会话层级设计:
| Session 类型 | 用途 |
|---|---|
| Main Session | 与用户直接对话的主会话 |
| Subagent | 并行任务执行,工具集隔离,状态独立 |
| ACP Session | 接入 Claude Code / Codex 等外部编程 Agent |
| Thread Session | 绑定 Discord 等平台的持久化对话线程 |
工程亮点: - sessions_spawn
隔离子会话,防止状态污染与 Context 竞争 - 子 Agent 完成后 Push
通知主会话,避免低效轮询 - ACP Harness 统一编排外部
Agent(Claude Code、Gemini 等) - Single-Writer 队列:
同一 Session 同时只允许一个 Run,保证消息顺序一致性
5.5 Heartbeat 主动调度机制
OpenClaw 内置 Heartbeat 机制,使 Agent 从被动响应转变为主动调度:
- 定时触发,检查邮件、日历、通知等待处理项
- 在安静时段(如深夜)自动抑制打扰
- 支持后台工作(内存整理、代码检查、文件同步)
- 与 Cron 系统协作:精确定时任务走 Cron,批量周期检查走 Heartbeat
5.6 设计亮点对标总结
| 工程维度 | OpenClaw 设计 | 对标学术概念 |
|---|---|---|
| 记忆分层 | MEMORY.md / 日志 / SQLite | MemGPT 内存分层架构 |
| 混合检索 | BM25 + Vector Hybrid | Dense-Sparse 混合检索 |
| 上下文管理 | 文件即状态 / 按需加载 | CoALA Working Memory |
| 多 Agent 协作 | Subagent + Push 通知 | Orchestrator-Subagents 模式 |
| 工具调用 | Skill 热插拔 / SKILL.md | ReAct Tool Use 范式 |
| 主动调度 | Heartbeat 机制 | Proactive Agent 设计 |
延伸阅读推荐
通用 LLM/Agent 工程: - Simon Willison’s Blog — LLM/Agent 实践分析:https://simonwillison.net/tags/agents/ - Eugene Yan — LLM Patterns(工程实践向):https://eugeneyan.com/writing/llm-patterns/ - Chip Huyen — Building LLM Applications for Production:https://huyenchip.com/2023/04/11/llm-engineering.html - LangChain Blog — Agent Design Patterns:https://blog.langchain.dev - Anthropic Research Blog:https://www.anthropic.com/research
OpenClaw 专项: - 官方文档:https://docs.openclaw.ai - Memory 系统深度解析(Gitbook):https://snowan.gitbook.io/study-notes/ai-blogs/openclaw-memory-system-deep-dive - 三层架构技术原理(eastondev.com):https://eastondev.com/blog/en/posts/ai/20260205-openclaw-architecture-guide/ - Towards AI 生产级 Agent 架构分析:https://towardsai.net/p/machine-learning/openclaw-architecture-deep-dive-building-production-ready-ai-agents-from-scratch - 系统架构概览(Substack):https://ppaolo.substack.com/p/openclaw-system-architecture-overview




