Introduction¶
From August to October 2025, I delivered a series of technical lectures at the "AI Agent Bootcamp" run by Turing, the Chinese tech publisher. The goal was simple: to move AI Agent design from intuition-driven to principle-driven—not just getting a demo to run, but understanding why an Agent is designed the way it is and what trade-offs lie behind each architectural decision. This book was compiled and expanded from the notes and experiments of those lectures.
The book itself, from first idea to finished manuscript, was created through what might be called whisper coding (dictation-based collaboration)—and the tool I dictated to was Pine, our own voice Agent. To prepare each lecture, I would dictate a rough outline, have it survey the topic, and let it assemble a first draft. After the lecture, I would bring the Bootcamp students' feedback back to it, and we would discuss and polish over many rounds; iteration by iteration, the lecture notes grew into the book you hold today. Through it all I rarely typed—I spoke my thoughts aloud instead. Speech has far higher bandwidth than typing (normal speech runs about four times typing speed), so the "dictate—survey—discuss—revise" loop turned fast. In a sense, this book is not only about Agents; it is also a work an Agent helped create.
Since the release of DeepSeek R1 in early 2025, the AI field has moved beyond the pure foundation model stage (i.e., general-purpose large language model backbones) into the deep water of engineering deployment. Progress at the model layer is visible in two directions. First, through Agentic Reinforcement Learning, models have trained tool-calling capabilities into their parameters, mastering general abilities in areas like coding, mathematics, and computer use. Second, model iteration keeps accelerating—GPT-5.2 to GPT-5.5, Claude Opus 4.5 to 4.8, each leap taking just half a year. At the product layer, general Agents like Manus, Claude Code, and OpenClaw have redefined human-computer interaction, pushing the architectural paradigm of "code generation + file system" into the mainstream.
Looking back at the Agent architecture principles I summarized in the course nearly a year ago, I found something both gratifying and surprising: these principles have not gone stale; they have only become more classic. New terms—Skill, harness, loop engineering—have since swept the Agent industry, but the real sequence runs the other way: companies like Anthropic did not invent these concepts and watch the industry adopt them; a great many Agents were already doing these things, and Anthropic then distilled the practice into named design principles. Practice comes first, naming comes later.
The confidence behind these principles comes from pushing Agents into long-horizon, high-stakes work in the real world. As Chief Scientist of Pine AI, I built Pine with my team. To my knowledge, it is the first general Agent that can autonomously interact with real people and reliably handle sensitive, complex, long-horizon tasks involving money on its own: it calls carriers on behalf of users to negotiate bills, takes refunds and complaints up with merchants, and cancels subscriptions—all without a human stepping in. Such tasks routinely run to dozens of rounds of negotiation, and a single mistake means real financial loss. It was this unforgiving demand for reliability that hammered out, one by one, the architectural principles this book keeps returning to. The following examples come from that practice.
- Long before Skill became a buzzword, we were already using dynamic prompt loading to rein in runaway prompt growth, command-line execution tools to rein in runaway tool lists, and a system status bar to give the Agent awareness of its execution environment, the user's time, and its own work status.
- Long before harness became a buzzword, we were already using Claude Code-style methods against unstable tool calls, hallucinations, dangerous operations, unauthorized operations, and ignored instructions.
- Long before loop engineering became a buzzword (the term was not distilled and named by the industry until mid-2026), we were already using what this book calls the proposer-reviewer method to keep the model from declaring a task finished too early—whether giving up prematurely and announcing "it can't be done" after one blocked path, or claiming "all done" before the loop was actually closed. The core of the method is to let the Agent review its own output artifacts and iterate, so that verification—not the model's own feeling—decides when a task ends.
None of this is our exclusive invention, either: to my knowledge, most leading model and Agent companies have worked out similar methods on their own. That is why I launched the "AI Agent Bootcamp" course at Turing in August 2025 and have taught a hands-on AI Agent course at the University of Chinese Academy of Sciences continuously from 2024 to 2026—and why I chose to release this book as open source rather than lock it up for royalties: I want this knowledge to reach more practitioners.
Practice comes first, naming comes later. This sequence carries a very practical lesson for enterprise Agent development: if you wait for an Agent buzzword to catch on before putting it into practice, you are already a step behind. By the time a term is trending, the leading companies have usually worked through the problem it names. So how do you know what to do before the name arrives? Two things, I believe, are key.
First, have a real business that makes extreme demands on the Agent's capability ceiling, and keep drawing genuine feedback from it. Take Pine. A single task often takes hours or even weeks and can mean repeated back-and-forth with multiple stakeholders: hours on the phone, pages of complex forms filled out on a computer, several rounds of email. Through all of it, not one number can be wrong, and every exchange must stay careful enough to protect the user's interests. Only when you are immersed in a scenario this complex does practice naturally push you to build harnesses—to cover what the model cannot yet do on its own but the business requires. Conversely, if the business asks little of the capability ceiling and a modest model upgrade is all it takes, you will never have a reason to hone these architectural principles.
Second, you must build an Evaluation mechanism. This book returns to the point again and again: without evaluation, there is no progress. Evaluation lets you tell whether a change is genuinely better or merely lucky, so the Agent's direction of iteration no longer rests on gut feeling. At bottom, what we advocate is doing engineering—and building Agents—by scientific method, and evaluation is that method's foundation. Chapter 6 develops it in full.
However the underlying models advance and however product forms shift, almost all successful Agent systems follow the same architectural patterns. This is no coincidence: good design principles should outlast model iteration cycles, because they describe not the usage of a specific model but the fundamental patterns by which intelligent systems interact with the world.
Richard Sutton, Turing Award winner and father of reinforcement learning, once said that the evolution of the universe has gone through four stages: from dust to stars, from stars to life, and from life to agents (originally termed designed entities). Biological evolution is blind: random mutation, natural selection. Most organisms do not understand their own working principles and cannot design or remake living things on their own. Agents, however, are something entirely new in the history of cosmic evolution: by generating code they can bootstrap and evolve themselves, like a programmer who writes another programmer, who in turn writes the next. That is, an Agent can understand its own operating mechanism and, given a goal, create entirely new Agents—even improve itself. The mission of this book is to help you understand and master the principles of this creation.
The core formula of this book is just one sentence: Agent = LLM + Context + Tools. All three are indispensable.
More intuitively, it is Brain + Eyes + Hands and Feet. The brain (LLM) is responsible for thinking and decision-making, the eyes (Context) determine what information the Agent can see, and the hands and feet (Tools) determine what the Agent can do. (Strictly speaking, "eyes" is a rough analogy: Context includes not only environmental information and conversation history but also tool definitions, meaning the information the Agent "sees" also includes "what hands and feet are available." This metaphor aims to convey the core intuition: Context is all the information the model can perceive.)
For readers familiar with reinforcement learning, these three can also be mapped to the formal language of RL. Specifically, LLM corresponds to Policy, Context corresponds to Observation Space, and Tools correspond to Action Space. The three expressions refer to the same object, just at different levels of abstraction.
But each of these three words means far more than its surface reading. Chapter 1 breaks them down one by one from a practitioner's viewpoint and builds a complete mapping from intuition to academic concept.
Book Structure¶
This book consists of ten chapters, divided into three parts (Figure 0-1, Figure 0-2): Chapter 1 is the foundation, establishing a global understanding of Agents; Chapters 2 through 7 sequentially unfold the three pillars: Context (Chapters 2-3), Tools (Chapters 4-5), and Models (Chapters 6-7, Evaluation and Post-training); Chapters 8 through 10 are advanced topics and applications, showcasing Agent self-evolution, multimodality and real-time interaction, and multi-agent collaboration.
- Chapter 1 (Agent Fundamentals) opens with several real Agent products to build intuition, then works through the core formula at three levels: the implementation layer (LLM + Context + Tools), the intuitive layer (Brain + Eyes + Hands and Feet), and the academic layer (Policy, Observation Space, and Action Space). Through experiments it dissects how the ReAct loop operates—the "Think → Act → Observe" iteration—and introduces three learning paradigms for Agents: Post-training, In-Context Learning, and Externalized Learning. It closes with orchestration design patterns, from workflows to autonomous Agents, establishing a unified conceptual framework for the chapters that follow.
- Chapter 2 (Context Engineering) is the most critical chapter in the book, systematically explaining Context, the "eyes" of the Agent. It starts with the API message structure and the Agent's core loop, laying the foundation that "Context is a list of messages," then delves into the underlying principles of KV Cache (a mechanism for reusing historical computation results during LLM inference), and works through in turn: Prompt Engineering (including process-oriented design, tool descriptions, and business rule refinement) and Prompt Injection attack and defense, the on-demand loading mechanism of Agent Skills, Agent status bar technology, and Context Compression strategies. Complete definitions of each term are provided at their first appearance in the main text.
- Chapter 3 (User Memory and Knowledge Bases) extends context management to a persistent knowledge system across sessions, allowing the Agent not only to remember the content of the current conversation but also to accumulate and recall knowledge across multiple conversations. It covers four progressive strategies for user memory, the complete technical stack of RAG (Retrieval-Augmented Generation, i.e., first retrieving relevant documents and then having the model generate an answer, including different text search methods and search result ranking optimization), multimodal information extraction, more advanced knowledge organization methods, and Agentic RAG (where the Agent autonomously decides when and what to retrieve).
- Chapter 4 (Tools) explores the bridge for Agents to interact with the external world: tools are like the "hands and feet" of the Agent, enabling it to search the web, call APIs, operate databases, etc. It introduces the MCP tool interoperability standard and design principles for five types of tools (Perception, Execution, Collaboration, Event Triggering, User Communication), with a focus on the security mechanisms of execution tools and event-driven asynchronous Agent architectures.
- Chapter 5 (Coding Agent and Code Generation) argues that a Coding Agent plus a file system is the core technical foundation of every general-purpose Agent. Using the OpenClaw architecture as its main thread, it dissects the workflow and implementation techniques of Coding Agents and demonstrates the broad value of code generation beyond programming: from aiding thinking and building knowledge bases to dynamically creating new tools and Agent bootstrapping.
- Chapter 6 (Agent Evaluation) builds a scientific evaluation methodology. It covers evaluation environments (two core paradigms: tool-calling and human-computer interaction, plus simulation environments discussed separately at the chapter's end), dataset design principles, the LLM-as-a-Judge automated evaluation method, evaluation-driven model selection, and a complete closed loop for transforming evaluation results into system improvements.
- Chapter 7 (Model Post-training) delves into two post-training techniques: SFT (Supervised Fine-Tuning, i.e., teaching the model to "follow examples" using labeled data) and RL (Reinforcement Learning, i.e., letting the model improve autonomously through trial and error and reward feedback). With the core arguments "SFT memorizes, RL generalizes" and "Data and environment are more important than algorithms," it covers the full panorama of the pre-training/SFT/RL stages, classic RL theory, reward signal design (from binary rewards to process rewards, to the "reward the outcome, constrain the process" verified path penalty), single-turn and multi-turn reinforcement learning algorithms, and frontier explorations like sample efficiency optimization.
- Chapter 8 (Agent Self-Evolution) explores how to make Agents continuously improve without modifying model weights. The two major evolutionary paths are: learning from experience (strategy summarization, workflow recording, automatic system prompt optimization, externalization of Skills knowledge) and proactively discovering and creating tools (MCP-Zero, open-source tool integration, creating new tools with code).
- Chapter 9 (Multimodality and Real-Time Interaction) looks forward to Agents moving from the text world to the physical world. It covers Voice Agents (from serial pipelines to end-to-end models), Computer Use (letting Agents operate graphical interfaces like humans), and Robot Manipulation (VLA (Vision-Language-Action model) control and Sim2Real transfer), revealing the common architectural challenges brought by multimodality and real-time requirements.
- Chapter 10 (Multi-Agent Collaboration) discusses the ultimate form of AI Agent systems: how multiple Agents divide up work and collaborate. It systematically elaborates on a classification framework for multi-agent collaboration (Shared/Independent Context × Peer/Manager/Decentralized), demonstrates collaborative architecture design methods through cases like Translation Agents and Phone+Computer Agents, and looks ahead to frontier directions like Agent societies and Agent economies.
How to Read This Book¶
The chapters in this book are relatively independent. You can choose different reading paths based on your needs:
- If you are an Agent developer, read the whole book in order. Chapters 1 through 5 form the core knowledge system, and the evaluation methodology of Chapter 6 must not be skipped either. Chapter 7 is for readers who need to customize models, while Chapters 8 through 10 showcase advanced directions.
- If you have limited time, prioritize Chapter 1 (for the big picture) and Chapter 2 (for context engineering, the most critical skill). The underlying principles of KV Cache in Chapter 2 are quite technical; on a first read you can skip the mechanics and keep only the three core conclusions stated up front—nothing later depends on the rest.
- If your focus is model training, go straight to Chapter 7 (Model Post-training). The evaluation methods of Chapter 6 are a prerequisite for training, so read the two together—after Chapters 1 and 2 for the overall picture.
Each chapter contains a large number of experiments and thought questions, numbered in the format "Experiment X-Y" (X is the chapter number, Y is the sequence number within the chapter). The titles of experiments and thought questions carry star ratings for difficulty: ★ means entry-level, suitable for all readers; ★★ means medium difficulty, requiring some engineering practice; ★★★ means an advanced challenge, usually involving open-ended questions or complex system design. Most experiments come with complete runnable code, organized in the accompanying open-source repository:
Companion Code Repository: https://github.com/bojieli/ai-agent-book
The project names in the repository correspond one-to-one with the experiments in the book, and each project includes complete run instructions and dependency configurations. I strongly recommend running these experiments yourself: AI Agents are an intensely hands-on field, and many design intuitions only take shape while you are debugging.
A Terminology Note: This book is careful to distinguish two terms that casual usage often conflates. Reasoning is the model's step-by-step thinking—as in reasoning models, chain-of-thought, and reasoning tokens. Inference is the model's forward computation at deployment time—as in inference cost, inference stack, and inference-time scaling. (The Chinese edition renders them as two distinct words—思考 for reasoning, 推理 for inference—precisely to keep the two ideas apart; Chapter 10's translation case study refers back to this convention.) Other key terms are defined at their first occurrence in the text.
Prerequisites¶
This book is aimed at readers with some technical background, but does not require you to be an expert in a specific field. The prerequisites are listed below in two levels: "Required" and "Recommended", to help you assess your readiness.
Required: Foundation for reading the entire book
- Python Programming: Almost all experiments in the book are based on Python. You need to be familiar with Python's basic syntax, common data structures, package management (pip), and other fundamental concepts. Proficiency is not required, but you should be able to read and modify Python code of moderate complexity.
- Basic Experience with LLMs: You should have used ChatGPT, Claude, or similar products, and understand the basic interaction pattern of "Prompt → Model Response".
- An AI-Assisted Programming Tool: Install and get comfortable with at least one AI-assisted programming tool, such as Claude Code, Codex, Cursor, or Trae. For one thing, these tools will greatly speed up the experiments, which involve extensive code writing and debugging. For another, they are themselves mature Coding Agents: in using one, you will experience firsthand the core mechanisms this book keeps returning to—the ReAct loop, tool calls, context management. That firsthand experience is invaluable for understanding Agent design principles.
- General Software Engineering Knowledge: Be familiar with basic concepts such as command-line operations, Git version control, JSON data format, and REST APIs. These are the foundation for running experiments and understanding the Agent's tool-calling mechanism.
Recommended: Enhancing the reading experience for specific chapters
- Machine Learning Basics (Chapter 7): Understanding basic concepts like training vs. inference, loss functions, gradient descent, and overfitting helps in understanding model post-training.
- Basic Mathematics (Chapters 2-3, 7): An intuitive understanding of linear algebra (e.g., knowing that vectors can represent direction and magnitude, and matrices can perform batch operations) helps in understanding embeddings and attention mechanisms. Basic knowledge of probability and statistics helps in understanding evaluation metrics and expected rewards in reinforcement learning. The mathematics in the book does not involve complex derivations and focuses on intuitive explanations.
- Web Development Basics (Chapters 4, 9): Understanding concepts like HTTP, WebSocket, and front-end/back-end separation architecture helps in understanding event-driven asynchronous Agent architectures and real-time communication experiments for voice agents.
- Basic Understanding of the Transformer Architecture (Chapters 2, 7): Transformer is the underlying architecture of almost all current large language models. Readers who want to systematically build up their foundational knowledge of large models may enjoy Illustrating Large Models (图解大模型, published in Chinese by Turing), which uses intuitive diagrams to explain core concepts such as the Transformer architecture, pre-training, and fine-tuning—a good complement to this book's Agent engineering perspective.
If you are missing some of these prerequisites, don't be discouraged. The core value of this book lies in architectural design principles and engineering methodology, not in any specific algorithm or technique. Outside of Chapter 7 on post-training, the book asks very little mathematics or machine learning of you, and it works perfectly well as a starting point.
Agent technology is still evolving rapidly, but good architectural design principles have the power to endure through time. Master the "why" behind the designs, and you can keep your judgment clear as the waves of technology roll through. I hope this book becomes a reliable guide as you build AI Agents.
Acknowledgments¶
I would like to thank editors Meng Ge and Liu Meiying at Turing for their diligent editing and for their work organizing the Turing "AI Agent Bootcamp" course; I also thank Professor Liu Junming for offering the hands-on AI Agent course at the University of Chinese Academy of Sciences. Special thanks go to all the students of the Turing "AI Agent Bootcamp" and of the UCAS AI Agent course—teaching these courses brought me a wealth of valuable feedback and advice from you, and it sharpened my own grasp of these concepts.
I thank all my colleagues at Pine AI. Without a product as excellent as Pine AI, and the many challenges it brought with it, I could never have gone so deep into the Agent field, in understanding or in practice; and in one collision of ideas after another, my colleagues contributed a wealth of valuable thinking.
I also want to thank many friends across the AI industry (I won't name them all here). In discussion after discussion, you gave me honest feedback on my views, corrected many of my misjudgments, and raised my understanding of models and Agents.
Most of all, I thank my family, especially my wife, Meng Jiaying. She supported me throughout the writing of this book and offered many valuable suggestions along the way.