ai-agent-book

Tutorial

bojieli/ai-agent-book

In-depth AI Agent book with open-source code and PDFs.

Overview

A comprehensive book on AI Agent design and engineering, covering 10 chapters from basics to multi-agent collaboration. The repository provides full book text, compiled PDFs in multiple languages, and chapter-based example code.

README Preview

# 深入理解 AI Agent:设计原理与工程实践\n\n**[English](README.en.md) | 中文 | [Tiếng Việt](README.vi.md) | [தமிழ்](README.ta.md)**\n\n本仓库是《深入理解 AI Agent:设计原理与工程实践》一书的开源主仓库,包含**全书正文**与**配套示例代码**。全书正文、配图与配套实验代码全部开源,欢迎把实验亲手跑一遍、提 issue 和 PR。\n\n## 📖 电子书\n\n全书提供多种语言版本:\n\n- **中文 PDF(原版)**:[`book/深入理解-AI-Agent-李博杰-v1.2.pdf`](book/深入理解-AI-Agent-李博杰-v1.2.pdf)\n- **英文 PDF**(社区贡献翻译,by [@nsdevaraj](https://github.com/nsdevaraj)):[`book-en/AI-Agents-in-Depth-Bojie-Li-v1.2.pdf`](book-en/AI-Agents-in-Depth-Bojie-Li-v1.2.pdf)\n- **泰米尔语 PDF**(社区贡献翻译,by [@nsdevaraj](https://github.com/nsdevaraj)):[`book-ta/AI-Agents-in-Depth-Bojie-Li-v1.2-ta.pdf`](book-ta/AI-Agents-in-Depth-Bojie-Li-v1.2-ta.pdf)\n- **越南语 PDF**(社区贡献翻译,by [@toanalien](https://github.com/toanalien)):[`book-vi/AI-Agents-in-Depth-Bojie-Li-v1.1-vi.pdf`](book-vi/AI-Agents-in-Depth-Bojie-Li-v1.1-vi.pdf)\n\n中文正文与编译好的 PDF 位于 [`book/`](book/) 目录;英文、泰米尔语与越南语翻译为**社区贡献**,分别位于 [`book-en/`](book-en/)、[`book-ta/`](book-ta/) 与 [`book-vi/`](book-vi/) 目录,内容可能滞后于中文原版:\n\n- **正文源码**:`book/introduction.md`(引言)、`book/chapter1.md` ~ `book/chapter10.md`(第一至第十章)、`book/afterword.md`(后记)\n- **自行编译**:安装 pandoc、xelatex、ElegantBook 文档类与相关字体后,运行\n\n  ```bash\n  cd book && bash build_pdf.sh\n  ```\n\n  图表由 `book/gen_*_figs.py` 生成、存于 `book/images/`,排版细节见 `book/preamble.tex` 与 `book/*.lua`。\n\n## 📑 内容速览(第 1–10 章)\n\n全书围绕核心公式 **Agent = LLM + 上下文 + 工具** 展开,十章内容如下:\n\n- **第 1 章 · Agent 基础知识**:从“模型即 Agent”的新范式出发,建立 **Agent = LLM + 上下文 + 工具** 的核心公式,并引入 Harness 工程——模型之外的一切工程能力,才是真正的竞争力所在。\n- **第 2 章 · 上下文工程**:上下文决定 Agent 的能力上限。深入大模型 API 的上下文结构、KV Cache 友好设计、提示工程、动态提示词与 Agent Skills、状态栏元信息,以及上下文压缩策略。\n- **第 3 章 · 用户记忆和知识库**:让 Agent 跨会话记住用户、并接入外部知识。涵盖用户记忆系统、RAG 基础管道,以及超越扁平文本的知识组织与检索(结构化索引、知识图谱等)。\n- **第 4 章 · 工具**:工具是 Agent 的双手。讲工具分类与通用设计原则、MCP 协议与工具选择的挑战、感知/执行/协作三类工具,以及事件驱动的异步 Agent。\n- **第 5 章 · Coding Agent 与代码生成**:代码是“能创造新工具的工具”,是通用 Agent 的元能力。以生产级 Coding Agent 为例,展示这一最强通用工具的完整实现。\n- **第 6 章 · Agent 的评估**:把 Agent 的表现变成可比较的信号。从评估环境、数据集设计、指标体系,到统计显著性、可观测性、评估驱动选型,直至生产级内部评

FAQ (5)

Troubleshooting
How do I fix Markdown bold not rendering when the bold text starts with a Chinese quotation mark?

GitHub/CommonMark does not treat the opening ** as an emphasis delimiter when it is preceded by normal Chinese text and immediately followed by Chinese punctuation such as “. In book/chapter1.md, replace:

因此它是**“构建”这一部分内部的一个观察视角**,而不是覆盖全书十章的目录。

with the reworded version from the fix:

因此,它是**构建部分内部的一个观察视角**,而不是覆盖全书十章的目录。

If the quotation marks must be preserved, use HTML instead:

因此它是<strong>“构建”这一部分内部的一个观察视角</strong>,而不是覆盖全书十章的目录。

Then reload the GitHub preview or published page to verify the phrase is bold.

GitHub Issue #1136
informational
How should I study a dense AI agent book when the concepts feel abstract and I forget earlier chapters?

Use an active, project-based approach instead of reading linearly. 1) Reimplement each chapter's experiments yourself rather than only reading the supplied code; a coding agent can help, but write and run the code. 2) Carefully observe experiment outputs and debug failures such as inaccessible APIs or unexpected results—these issues deepen understanding. 3) Apply the ideas to a real or personal AI-agent project. 4) Use the chapter thinking questions for self-testing. 5) Follow the cycle “first make the book thin, then thick, then thin again”: extract the core ideas, expand them with notes and experiments, then distill them into your own concise principles. The maintainers have also added these recommendations to the README FAQ; further reading questions can be discussed in the repository's Discussions area.

GitHub Issue #933
informational
Why doesn't appending tokens at the end invalidate the Transformer KV cache?

The description should be corrected. For each token, K_i = x_i @ W_k and V_i = x_i @ W_v depend only on that token's input and the projection weights; causal attention does not make K_i or V_i depend on previous tokens. Causal masking only determines which keys/values a token can attend to. When new tokens are appended, the existing prefix and its cached K/V remain unchanged, so the cache is still valid. The new token computes attention as z_i = softmax(Q_i · K_{0..i}^T / sqrt(d)) @ V_{0..i} using the cached prefix and its new entry. If an earlier token in the prefix changes, the affected cache entries must be recomputed.

GitHub Issue #1104
Troubleshooting
How do I fix MkDocs homepage chapter cards showing outdated titles that don't match the sidebar or actual chapter pages?

The mismatch is caused by hardcoded chapter cards in the project root index.md. When manuscript titles or chapter order changed, the homepage 章节速览 cards were not updated, so Chapter 8 still displayed Agent 的持续进化 even though book/chapter8.md is titled 模型后训练.

Permanent fix: apply/merge the PR fix(site): generate homepage chapter index from manuscripts. Generate the homepage chapter cards during every MkDocs build from the current manuscript headings, using the configured navigation order and each edition's URL prefix and filename. Then rebuild and redeploy the site.

Temporary manual workaround: edit the hardcoded <span class="exp-title"> entries in index.md so they match the actual chapter headings and links. For Chapter 8, change the card title to 第 8 章 · 模型后训练. Also verify Chapters 5–9 because their titles/order appear shifted. Regenerate or update all localized homepage files (index.he.md, index.ko.md, index.ptbr.md, etc.) instead of fixing only the default homepage.

GitHub Issue #1138
informational
Do I need to understand every line of code in the AI Agent book experiments?

No. You do not need to understand every line before continuing. First understand the experiment's goal, architecture, and what each core component does. Read the chapter and pseudocode, then use a coding agent to implement the experiment from scratch until it produces the expected result. Study the implementation details only when you need fine-grained control over the behavior or want to learn the underlying language/framework. Focus on clearly describing requirements and design; let AI handle repetitive implementation code. The experiment READMEs and newly added pseudocode can help clarify the design.

GitHub Issue #637