bojieli / ai-agent-book

bojieli / ai-agent-book

深入理解 AI Agent:设计原理与工程实践

In-Depth Understanding of AI Agents: Design Principles and Engineering Practices

English | 中文

本仓库是《深入理解 AI Agent:设计原理与工程实践》一书的开源主仓库,包含全书正文与配套示例代码。全书正文、配图与配套实验代码全部开源,欢迎把实验亲手跑一遍、提 issue 和 PR。 This repository is the main open-source hub for the book In-Depth Understanding of AI Agents: Design Principles and Engineering Practices, containing the full text and accompanying example code. The entire text, illustrations, and experimental code are open-source; you are welcome to run the experiments yourself, submit issues, and open PRs.


📖 电子书 (E-book)

全书提供中英双语版本: The book is available in both Chinese and English:

中文正文与编译好的 PDF 位于 book/ 目录;英文与泰米尔语翻译为社区贡献,分别位于 book-en/book-ta/ 目录,内容可能滞后于中文原版: The Chinese text and compiled PDF are located in the book/ directory. The English and Tamil translations are community contributions located in the book-en/ and book-ta/ directories, respectively, and their content may lag behind the Chinese original.

  • 正文源码: book/introduction.md(引言)、book/chapter1.md ~ book/chapter10.md(第一至第十章)、book/afterword.md(后记) Source Text: book/introduction.md (Introduction), book/chapter1.md ~ book/chapter10.md (Chapters 1-10), book/afterword.md (Afterword).
  • 自行编译: 安装 pandoc、xelatex、ElegantBook 文档类与相关字体后,运行 cd book && bash build_pdf.sh Self-compilation: After installing pandoc, xelatex, the ElegantBook document class, and relevant fonts, run cd book && bash build_pdf.sh.
  • 图表由 book/gen_*_figs.py 生成、存于 book/images/,排版细节见 book/preamble.texbook/*.lua。 Charts are generated by book/gen_*_figs.py and stored in book/images/. For typesetting details, see book/preamble.tex and book/*.lua.

📑 内容速览(第 1–10 章)

📑 Quick Overview (Chapters 1–10)

全书围绕核心公式 Agent = LLM + 上下文 + 工具 展开,十章内容如下: The book revolves around the core formula Agent = LLM + Context + Tools, with the ten chapters outlined below:

  • 第 1 章 · Agent 基础知识: 从“模型即 Agent”的新范式出发,建立 Agent = LLM + 上下文 + 工具 的核心公式,并引入 Harness 工程——模型之外的一切工程能力,才是真正的竞争力所在。 Chapter 1 · Agent Fundamentals: Starting from the new paradigm of “Model as Agent,” we establish the core formula Agent = LLM + Context + Tools and introduce “Harness Engineering”—the idea that all engineering capabilities outside the model are where the true competitive edge lies.

  • 第 2 章 · 上下文工程: 上下文决定 Agent 的能力上限。深入大模型 API 的上下文结构、KV Cache 友好设计、提示工程、动态提示词与 Agent Skills、状态栏元信息,以及上下文压缩策略。 Chapter 2 · Context Engineering: Context determines the upper limit of an Agent’s capabilities. We dive into LLM API context structures, KV Cache-friendly design, prompt engineering, dynamic prompts and Agent Skills, status bar metadata, and context compression strategies.

  • 第 3 章 · 用户记忆和知识库: 让 Agent 跨会话记住用户、并接入外部知识。涵盖用户记忆系统、RAG 基础管道,以及超越扁平文本的知识组织与检索(结构化索引、知识图谱等)。 Chapter 3 · User Memory and Knowledge Bases: Enabling Agents to remember users across sessions and access external knowledge. This covers user memory systems, basic RAG pipelines, and knowledge organization/retrieval beyond flat text (structured indexing, knowledge graphs, etc.).

  • 第 4 章 · 工具: 工具是 Agent 的双手。讲工具分类与通用设计原则、MCP 协议与工具选择的挑战、感知/执行/协作三类工具,以及事件驱动的异步 Agent。 Chapter 4 · Tools: Tools are the hands of an Agent. We discuss tool classification and general design principles, the MCP protocol and challenges in tool selection, three categories of tools (perception/execution/collaboration), and event-driven asynchronous Agents.

  • 第 5 章 · Coding Agent 与代码生成: 代码是“能创造新工具的工具”,是通用 Agent 的元能力。以生产级 Coding Agent 为例,展示这一最强通用工具的完整实现。 Chapter 5 · Coding Agents and Code Generation: Code is a “tool that can create new tools” and is a meta-capability for general-purpose Agents. Using a production-grade Coding Agent as an example, we demonstrate the complete implementation of this most powerful general-purpose tool.

  • 第 6 章 · Agent 的评估: 把 Agent 的表现变成可比较的信号。从评估环境、数据集设计、指标体系,到统计显著性、可观测性、评估驱动选型,直至生产级内部评估与仿真环境。 Chapter 6 · Agent Evaluation: Turning Agent performance into comparable signals. From evaluation environments, dataset design, and metric systems to statistical significance, observability, evaluation-driven selection, and production-grade internal evaluation and simulation environments.

  • 第 7 章 · 模型后训练: 预训练、SFT、RL 三阶段全景。何时选 SFT、何时选 RL,RLHF、算法比较、数据与环境,以及让模型学会工具调用、提升样本效率的前沿探索。 Chapter 7 · Model Post-training: A panoramic view of the three stages: Pre-training, SFT, and RL. We discuss when to choose SFT vs. RL, RLHF, algorithm comparisons, data and environments, and cutting-edge research on teaching models tool-calling and improving sample efficiency.

  • 第 8 章 · Agent 的自我进化: 不改权重也能成长。三种学习范式,从经验中学习、主动工具发现,到“从工具使用者到工具创造者”,让 Agent 从“聪明”走向“熟练”。 Chapter 8 · Agent Self-Evolution: Growing without changing weights. We explore three learning paradigms: learning from experience, proactive tool discovery, and moving from “tool user” to “tool creator,” helping Agents transition from “smart” to “proficient.”

  • 第 9 章 · 多模态与实时交互: 把感知与行动从文本扩展到语音、GUI 与物理世界。语音三范式(级联/端到端全模态/全双工)、流式语音感知与合成、Computer Use 与机器人操作。 Chapter 9 · Multimodality and Real-time Interaction: Extending perception and action from text to voice, GUI, and the physical world. We cover the three paradigms of voice (cascaded/end-to-end full-modal/full-duplex), streaming voice perception and synthesis, Computer Use, and robotic manipulation.

  • 第 10 章 · 多 Agent 协作: 群体的智能可以高于个体。多 Agent 分类框架、何时真正优于单 Agent、共享与不共享上下文的协作、失败模式,以及涌现的“Agent 社会”。 Chapter 10 · Multi-Agent Collaboration: Collective intelligence can exceed individual intelligence. We discuss multi-agent classification frameworks, when they are truly superior to single agents, collaboration with shared vs. unshared context, failure modes, and the emergence of “Agent Societies.”