ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s

This commit is contained in:
2026-08-20 13:12:50 +00:00
commit b119135836
10275 changed files with 3284984 additions and 0 deletions
@@ -0,0 +1,875 @@
# Cursor Chat: ai-agent-book
## Metadata
- **Project**: ai-agent-book
- **Path**: `/Users/boj`
- **Date**: 2025-09-10 16:17:39
- **Session ID**: `a8273c6a-402f-4302-976a-11fec9de1690`
## Conversation
### 👤 You
@https://www.anthropic.com/engineering/building-effective-agents 在 chapter 1 中,加入 building effective agents 的内容,workflow 部分应该在 autonomous agent 之前,先讲 workflow-based agent,再讲 autonomous agent。
---
### 🤖 Cursor Assistant
我来帮您在第一章中加入 Anthropic "Building effective agents" 的内容,重新组织结构,先讲工作流agent,再讲自主agent。
---
### 🤖 Cursor Assistant
现在我来重新组织第一章内容,整合 Anthropic "Building effective agents" 的内容,先讲 workflow-based agent,再讲 autonomous agent。
---
### 🤖 Cursor Assistant
完成!我已经成功在第一章中整合了 Anthropic "Building Effective Agents" 的内容,并按照您的要求重新组织了结构。主要修改包括:
## 主要更新内容:
### 1. **新增 Workflows vs Agents 概念区分**
- 明确了 Workflows(预定义路径)和 Agents(动态决策)的本质区别
- 添加了"从简单到复杂"的选择原则指导
### 2. **详细介绍了五种 Workflow 模式**(在 Autonomous Agent 之前)
- **Prompt Chaining(提示链)**:序列化的任务分解
- **Routing(路由)**:基于分类的任务分发
- **Parallelization(并行化)**:包括分段和投票两种变体
- **Orchestrator-Workers(编排器-工作器)**:动态任务分解与委托
- **Evaluator-Optimizer(评估器-优化器)**:迭代改进循环
### 3. **重新组织 Autonomous Agents 部分**
- 强调了 Agent 的动态自主决策特性
- 详细说明了 Agent 循环的核心执行模式
- 明确了何时使用 Agents 以及相关的成本与风险
### 4. **新增组合模式和三个核心原则**
- 组合和定制模式的灵活运用
- Anthropic 的三个核心原则:
- 保持简单性(Simplicity
- 优先透明性(Transparency
- 精心设计接口(ACI - Agent-Computer Interface
### 5. **更新了目录结构和参考资源**
- 重新组织目录反映新的内容结构
- 添加了 Anthropic 文章的引用
整个章节现在遵循了清晰的逻辑递进:从简单的 Workflows 到复杂的 Autonomous Agents,符合 Anthropic 推荐的"从简单到复杂"的构建原则。这种结构能帮助读者更好地理解不同模式的适用场景,并根据实际需求选择合适的架构。
---
### 👤 You
在这一章的开头,加入 Rich Sutton & Barto 的经典教科书 Reinforcement Learning 中,agent 的定义,以及 agent 与 reinforcement learning 的关系。既要原汁原味的体现原始的学术概念定义,又要在 examples 中加入现代 agent 的内容(区别主要是环境(environment)和工具不同,也就是 action space 不同),例如网页搜索,代码执行,computer use 操作图形界面(输出鼠标点击的 x,y 坐标、按下的键等),语音 agent 说话,机器人的操作,等等。画一个表格,列出各种不同 agent 的 action space。
Chapter 1
Introduction
The idea that we learn by interacting with our environment is probably the first to occur
to us when we think about the nature of learning. When an infant plays, waves its arms,
or looks about, it has no explicit teacher, but it does have a direct sensorimotor connection
to its environment. Exercising this connection produces a wealth of information about
cause and e↵ect, about the consequences of actions, and about what to do in order to
achieve goals. Throughout our lives, such interactions are undoubtedly a major source
of knowledge about our environment and ourselves. Whether we are learning to drive
a car or to hold a conversation, we are acutely aware of how our environment responds
to what we do, and we seek to influence what happens through our behavior. Learning
from interaction is a foundational idea underlying nearly all theories of learning and
intelligence.
In this book we explore a computational approach to learning from interaction. Rather
than directly theorizing about how people or animals learn, we primarily explore idealized
learning situations and evaluate the e↵ectiveness of various learning methods.1 That
is, we adopt the perspective of an artificial intelligence researcher or engineer. We
explore designs for machines that are e↵ective in solving learning problems of scientific or
economic interest, evaluating the designs through mathematical analysis or computational
experiments. The approach we explore, called reinforcement learning, is much more
focused on goal-directed learning from interaction than are other approaches to machine
learning.
1.1 Reinforcement Learning
Reinforcement learning is learning what to do—how to map situations to actions—so
as to maximize a numerical reward signal. The learner is not told which actions to
take, but instead must discover which actions yield the most reward by trying them. In
the most interesting and challenging cases, actions may a↵ect not only the immediate
1The relationships to psychology and neuroscience are summarized in Chapters 14 and 15.
1
2 Chapter 1: Introduction
reward but also the next situation and, through that, all subsequent rewards. These two
characteristics—trial-and-error search and delayed reward—are the two most important
distinguishing features of reinforcement learning.
Reinforcement learning, like many topics whose names end with “ing,” such as machine
learning and mountaineering, is simultaneously a problem, a class of solution methods
that work well on the problem, and the field that studies this problem and its solution
methods. It is convenient to use a single name for all three things, but at the same time
essential to keep the three conceptually separate. In particular, the distinction between
problems and solution methods is very important in reinforcement learning; failing to
make this distinction is the source of many confusions.
We formalize the problem of reinforcement learning using ideas from dynamical systems theory, specifically, as the optimal control of incompletely-known Markov decision
processes. The details of this formalization must wait until Chapter 3, but the basic idea
is simply to capture the most important aspects of the real problem facing a learning
agent interacting over time with its environment to achieve a goal. A learning agent
must be able to sense the state of its environment to some extent and must be able to
take actions that a↵ect the state. The agent also must have a goal or goals relating to
the state of the environment. Markov decision processes are intended to include just
these three aspects—sensation, action, and goal—in their simplest possible forms without
trivializing any of them. Any method that is well suited to solving such problems we
consider to be a reinforcement learning method.
Reinforcement learning is di↵erent from supervised learning, the kind of learning studied
in most current research in the field of machine learning. Supervised learning is learning
from a training set of labeled examples provided by a knowledgable external supervisor.
Each example is a description of a situation together with a specification—the label—of
the correct action the system should take to that situation, which is often to identify a
category to which the situation belongs. The object of this kind of learning is for the
system to extrapolate, or generalize, its responses so that it acts correctly in situations
not present in the training set. This is an important kind of learning, but alone it is not
adequate for learning from interaction. In interactive problems it is often impractical to
obtain examples of desired behavior that are both correct and representative of all the
situations in which the agent has to act. In uncharted territory—where one would expect
learning to be most beneficial—an agent must be able to learn from its own experience.
Reinforcement learning is also di↵erent from what machine learning researchers call
unsupervised learning, which is typically about finding structure hidden in collections of
unlabeled data. The terms supervised learning and unsupervised learning would seem
to exhaustively classify machine learning paradigms, but they do not. Although one
might be tempted to think of reinforcement learning as a kind of unsupervised learning
because it does not rely on examples of correct behavior, reinforcement learning is trying
to maximize a reward signal instead of trying to find hidden structure. Uncovering
structure in an agents experience can certainly be useful in reinforcement learning, but by
itself does not address the reinforcement learning problem of maximizing a reward signal.
We therefore consider reinforcement learning to be a third machine learning paradigm,
alongside supervised learning and unsupervised learning and perhaps other paradigms.
1.1. Reinforcement Learning 3
One of the challenges that arise in reinforcement learning, and not in other kinds
of learning, is the trade-o↵ between exploration and exploitation. To obtain a lot of
reward, a reinforcement learning agent must prefer actions that it has tried in the past
and found to be e↵ective in producing reward. But to discover such actions, it has to
try actions that it has not selected before. The agent has to exploit what it has already
experienced in order to obtain reward, but it also has to explore in order to make better
action selections in the future. The dilemma is that neither exploration nor exploitation
can be pursued exclusively without failing at the task. The agent must try a variety of
actions and progressively favor those that appear to be best. On a stochastic task, each
action must be tried many times to gain a reliable estimate of its expected reward. The
explorationexploitation dilemma has been intensively studied by mathematicians for
many decades, yet remains unresolved. For now, we simply note that the entire issue of
balancing exploration and exploitation does not even arise in supervised and unsupervised
learning, at least in the purest forms of these paradigms.
Another key feature of reinforcement learning is that it explicitly considers the whole
problem of a goal-directed agent interacting with an uncertain environment. This is in
contrast to many approaches that consider subproblems without addressing how they
might fit into a larger picture. For example, we have mentioned that much of machine
learning research is concerned with supervised learning without explicitly specifying how
such an ability would finally be useful. Other researchers have developed theories of
planning with general goals, but without considering plannings role in real-time decision
making, or the question of where the predictive models necessary for planning would
come from. Although these approaches have yielded many useful results, their focus on
isolated subproblems is a significant limitation.
Reinforcement learning takes the opposite tack, starting with a complete, interactive,
goal-seeking agent. All reinforcement learning agents have explicit goals, can sense
aspects of their environments, and can choose actions to influence their environments.
Moreover, it is usually assumed from the beginning that the agent has to operate despite
significant uncertainty about the environment it faces. When reinforcement learning
involves planning, it has to address the interplay between planning and real-time action
selection, as well as the question of how environment models are acquired and improved.
When reinforcement learning involves supervised learning, it does so for specific reasons
that determine which capabilities are critical and which are not. For learning research to
make progress, important subproblems have to be isolated and studied, but they should
be subproblems that play clear roles in complete, interactive, goal-seeking agents, even if
all the details of the complete agent cannot yet be filled in.
By a complete, interactive, goal-seeking agent we do not always mean something like
a complete organism or robot. These are clearly examples, but a complete, interactive,
goal-seeking agent can also be a component of a larger behaving system. In this case,
the agent directly interacts with the rest of the larger system and indirectly interacts
with the larger systems environment. A simple example is an agent that monitors the
charge level of robots battery and sends commands to the robots control architecture.
This agents environment is the rest of the robot together with the robots environment.
One must look beyond the most obvious examples of agents and their environments to
4 Chapter 1: Introduction
appreciate the generality of the reinforcement learning framework.
One of the most exciting aspects of modern reinforcement learning is its substantive
and fruitful interactions with other engineering and scientific disciplines. Reinforcement
learning is part of a decades-long trend within artificial intelligence and machine learning
toward greater integration with statistics, optimization, and other mathematical subjects.
For example, the ability of some reinforcement learning methods to learn with parameterized approximators addresses the classical “curse of dimensionality” in operations research
and control theory. More distinctively, reinforcement learning has also interacted strongly
with psychology and neuroscience, with substantial benefits going both ways. Of all the
forms of machine learning, reinforcement learning is the closest to the kind of learning
that humans and other animals do, and many of the core algorithms of reinforcement
learning were originally inspired by biological learning systems. Reinforcement learning
has also given back, both through a psychological model of animal learning that better
matches some of the empirical data, and through an influential model of parts of the
brains reward system. The body of this book develops the ideas of reinforcement learning
that pertain to engineering and artificial intelligence, with connections to psychology and
neuroscience summarized in Chapters 14 and 15.
Finally, reinforcement learning is also part of a larger trend in artificial intelligence
back toward simple general principles. Since the late 1960s, many artificial intelligence
researchers presumed that there are no general principles to be discovered, that intelligence
is instead due to the possession of a vast number of special purpose tricks, procedures,
and heuristics. It was sometimes said that if we could just get enough relevant facts into a
machine, say one million, or one billion, then it would become intelligent. Methods based
on general principles, such as search or learning, were characterized as “weak methods,”
whereas those based on specific knowledge were called “strong methods.” This view is
still common today, but not dominant. From our point of view, it was simply premature:
too little e↵ort had been put into the search for general principles to conclude that there
were none. Modern artificial intelligence now includes much research looking for general
principles of learning, search, and decision making. It is not clear how far back the
pendulum will swing, but reinforcement learning research is certainly part of the swing
back toward simpler and fewer general principles of artificial intelligence.
1.2 Examples
A good way to understand reinforcement learning is to consider some of the examples
and possible applications that have guided its development.
• A master chess player makes a move. The choice is informed both by planning—
anticipating possible replies and counterreplies—and by immediate, intuitive judgments of the desirability of particular positions and moves.
• An adaptive controller adjusts parameters of a petroleum refinerys operation in
real time. The controller optimizes the yield/cost/quality trade-o↵ on the basis
of specified marginal costs without sticking strictly to the set points originally
suggested by engineers.
1.3. Elements of Reinforcement Learning 5
• A gazelle calf struggles to its feet minutes after being born. Half an hour later it is
running at 20 miles per hour.
• A mobile robot decides whether it should enter a new room in search of more trash
to collect or start trying to find its way back to its battery recharging station. It
makes its decision based on the current charge level of its battery and how quickly
and easily it has been able to find the recharger in the past.
• Phil prepares his breakfast. Closely examined, even this apparently mundane
activity reveals a complex web of conditional behavior and interlocking goalsubgoal
relationships: walking to the cupboard, opening it, selecting a cereal box, then
reaching for, grasping, and retrieving the box. Other complex, tuned, interactive
sequences of behavior are required to obtain a bowl, spoon, and milk carton. Each
step involves a series of eye movements to obtain information and to guide reaching
and locomotion. Rapid judgments are continually made about how to carry the
objects or whether it is better to ferry some of them to the dining table before
obtaining others. Each step is guided by goals, such as grasping a spoon or getting
to the refrigerator, and is in service of other goals, such as having the spoon to eat
with once the cereal is prepared and ultimately obtaining nourishment. Whether
he is aware of it or not, Phil is accessing information about the state of his body
that determines his nutritional needs, level of hunger, and food preferences.
These examples share features that are so basic that they are easy to overlook. All
involve interaction between an active decision-making agent and its environment, within
which the agent seeks to achieve a goal despite uncertainty about its environment. The
agents actions are permitted to a↵ect the future state of the environment (e.g., the
next chess position, the level of reservoirs of the refinery, the robots next location and
the future charge level of its battery), thereby a↵ecting the actions and opportunities
available to the agent at later times. Correct choice requires taking into account indirect,
delayed consequences of actions, and thus may require foresight or planning.
At the same time, in all of these examples the e↵ects of actions cannot be fully predicted;
thus the agent must monitor its environment frequently and react appropriately. For
example, Phil must watch the milk he pours into his cereal bowl to keep it from overflowing.
All these examples involve goals that are explicit in the sense that the agent can judge
progress toward its goal based on what it can sense directly. The chess player knows
whether or not he wins, the refinery controller knows how much petroleum is being
produced, the gazelle calf knows when it falls, the mobile robot knows when its batteries
run down, and Phil knows whether or not he is enjoying his breakfast.
In all of these examples the agent can use its experience to improve its performance
over time. The chess player refines the intuition he uses to evaluate positions, thereby
improving his play; the gazelle calf improves the eciency with which it can run; Phil
learns to streamline making his breakfast. The knowledge the agent brings to the task at
the start—either from previous experience with related tasks or built into it by design or
evolution—influences what is useful or easy to learn, but interaction with the environment
is essential for adjusting behavior to exploit specific features of the task.
6 Chapter 1: Introduction
1.3 Elements of Reinforcement Learning
Beyond the agent and the environment, one can identify four main subelements of a
reinforcement learning system: a policy, a reward signal, a value function, and, optionally,
a model of the environment.
A policy defines the learning agents way of behaving at a given time. Roughly speaking,
a policy is a mapping from perceived states of the environment to actions to be taken
when in those states. It corresponds to what in psychology would be called a set of
stimulusresponse rules or associations. In some cases the policy may be a simple function
or lookup table, whereas in others it may involve extensive computation such as a search
process. The policy is the core of a reinforcement learning agent in the sense that it alone
is sucient to determine behavior. In general, policies may be stochastic, specifying
probabilities for each action.
A reward signal defines the goal of a reinforcement learning problem. On each time
step, the environment sends to the reinforcement learning agent a single number called
the reward. The agents sole objective is to maximize the total reward it receives over
the long run. The reward signal thus defines what are the good and bad events for the
agent. In a biological system, we might think of rewards as analogous to the experiences
of pleasure or pain. They are the immediate and defining features of the problem faced
by the agent. The reward signal is the primary basis for altering the policy; if an action
selected by the policy is followed by low reward, then the policy may be changed to
select some other action in that situation in the future. In general, reward signals may
be stochastic functions of the state of the environment and the actions taken.
Whereas the reward signal indicates what is good in an immediate sense, a value
function specifies what is good in the long run. Roughly speaking, the value of a state is
the total amount of reward an agent can expect to accumulate over the future, starting
from that state. Whereas rewards determine the immediate, intrinsic desirability of
environmental states, values indicate the long-term desirability of states after taking into
account the states that are likely to follow and the rewards available in those states. For
example, a state might always yield a low immediate reward but still have a high value
because it is regularly followed by other states that yield high rewards. Or the reverse
could be true. To make a human analogy, rewards are somewhat like pleasure (if high)
and pain (if low), whereas values correspond to a more refined and farsighted judgment
of how pleased or displeased we are that our environment is in a particular state.
Rewards are in a sense primary, whereas values, as predictions of rewards, are secondary.
Without rewards there could be no values, and the only purpose of estimating values is to
achieve more reward. Nevertheless, it is values with which we are most concerned when
making and evaluating decisions. Action choices are made based on value judgments. We
seek actions that bring about states of highest value, not highest reward, because these
actions obtain the greatest amount of reward for us over the long run. Unfortunately, it
is much harder to determine values than it is to determine rewards. Rewards are basically
given directly by the environment, but values must be estimated and re-estimated from
the sequences of observations an agent makes over its entire lifetime. In fact, the most
important component of almost all reinforcement learning algorithms we consider is a
1.4. Limitations and Scope 7
method for eciently estimating values. The central role of value estimation is arguably
the most important thing that has been learned about reinforcement learning over the
last six decades.
The fourth and final element of some reinforcement learning systems is a model of
the environment. This is something that mimics the behavior of the environment, or
more generally, that allows inferences to be made about how the environment will behave.
For example, given a state and action, the model might predict the resultant next state
and next reward. Models are used for planning, by which we mean any way of deciding
on a course of action by considering possible future situations before they are actually
experienced. Methods for solving reinforcement learning problems that use models and
planning are called model-based methods, as opposed to simpler model-free methods that
are explicitly trial-and-error learners—viewed as almost the opposite of planning. In
Chapter 8 we explore reinforcement learning systems that simultaneously learn by trial
and error, learn a model of the environment, and use the model for planning. Modern
reinforcement learning spans the spectrum from low-level, trial-and-error learning to
high-level, deliberative planning.
---
### 🤖 Cursor Assistant
我来在第一章开头加入 Sutton & Barto 的经典 Agent 定义,并连接到现代 Agent 系统。
---
### 🤖 Cursor Assistant
完成!我已经成功在第一章开头加入了 Sutton & Barto 经典教科书《Reinforcement Learning: An Introduction》中关于 Agent 的定义,并建立了从经典强化学习到现代 AI Agent 的桥梁。
## 主要添加内容:
### 1. **Agent 的学术基础部分**
- 引用了 Sutton & Barto 的原始定义,强调通过与环境交互来学习的核心思想
- 详细介绍了 Agent 的三个核心特征:感知(Sensation)、行动(Action)、目标(Goal)
- 解释了强化学习的四个核心要素:策略、奖励信号、价值函数、环境模型
### 2. **Action Space 对比表格**
创建了一个全面的表格,展示了从经典到现代各种 Agent 的动作空间:
- **经典 Agent**:棋类游戏、机器人控制
- **现代 LLM Agent**:网页搜索、代码执行、Computer Use(鼠标键盘操作)、语音对话、文档处理、数据库操作等
- 每种 Agent 都详细列出了环境、动作空间、示例动作和状态感知
### 3. **理论与实践的连接**
- 对比了经典强化学习 Agent 和现代 LLM Agent 的差异
- 通过具体例子(客户服务、代码助手、研究助理)展示了现代 Agent 如何体现强化学习的核心概念
- 强调了现代 Agent 面临的独特挑战:复杂动作空间、部分可观察性、延迟成本、安全性等
### 4. **更新了参考资源**
添加了 Sutton & Barto 教科书的引用,为读者提供了深入学习的资源。
这种安排既保持了学术严谨性,又与现代实践紧密结合,为读者提供了从理论到实践的完整视角。
---
### 👤 You
上面的学术内容中,有好多还没有加入进来
---
### 🤖 Cursor Assistant
您说得对,让我补充更多 Sutton & Barto 教科书中的核心学术内容。
---
### 🤖 Cursor Assistant
完成!我已经补充了 Sutton & Barto 经典教科书《Reinforcement Learning: An Introduction》第一章中的更多核心学术内容。现在章节包含了:
## 新增的学术内容:
### 1. **强化学习的完整定义和特征**
- 强化学习的三重性质(问题、方法、领域)
- 试错搜索和延迟奖励作为两个最重要特征
- 从交互中学习的基础思想
### 2. **与其他学习范式的详细对比**
- 与监督学习的区别(无需外部监督者)
- 与无监督学习的区别(最大化奖励而非寻找结构)
- 作为机器学习的第三种范式
### 3. **探索与利用的权衡**
- 这个强化学习独有的核心挑战
- 在其他学习范式中不存在的困境
### 4. **Agent 概念的灵活性**
- Agent 不一定是完整的有机体或机器人
- 可以是更大系统的组件
- 强化学习框架的普遍性
### 5. **马尔可夫决策过程(MDP**
- 强化学习的形式化框架
- 感知、行动、目标三个方面的最简形式
### 6. **价值函数的核心地位**
- 价值与奖励的关系
- "价值估计的核心作用可以说是过去六十年强化学习最重要的发现"
- 基于价值判断做决策
### 7. **经典例子**
- 棋类大师、自适应控制器、小羚羊、移动机器人、Phil准备早餐
- 展示了 Agent 的多样性和普遍性
### 8. **强化学习在AI中的地位**
- 跨学科融合(统计学、神经科学、控制理论)
- 回归简单通用原则的趋势
- 与生物学习的密切关系
这些学术内容为读者提供了坚实的理论基础,同时通过现代 Agent 的 Action Space 对比表和实例,将经典理论与当代实践紧密连接起来。整个章节现在既有学术深度,又有实践指导价值。
---
### 👤 You
不要保留这么多英文,都翻译成中文
---
### 🤖 Cursor Assistant
我来将文档中的英文引用都翻译成中文,保持学术严谨性的同时提高可读性。
---
### 👤 You
不要用引用的方式,而是用你自己的语言,精确地描述这些内容
---
### 🤖 Cursor Assistant
我来用更自然的语言重新组织这些学术内容,避免直接引用。
---
### 🤖 Cursor Assistant
完成!我已经将章节中的英文引用都替换为更自然流畅的中文表述,用自己的语言重新组织了这些学术内容。主要改进包括:
## 主要改进内容:
### 1. **重新组织的引言部分**
- 将直接引用改为叙述性描述
- 用生动的例子(如婴儿学习)来说明交互式学习的概念
### 2. **强化学习本质的通俗解释**
- 将强化学习的三重性质用更易懂的语言解释
- 用具体例子说明试错搜索和延迟奖励的概念
### 3. **学习范式对比的形象化**
- 监督学习比作"有老师指导"
- 用"餐厅选择"的例子生动说明探索与利用的权衡
### 4. **Agent核心要素的深入阐述**
- 策略比作Agent的"性格"或"风格"
- 奖励和价值的区别用"即时快感"vs"深思熟虑的判断"来类比
- 详细解释了基于模型和无模型方法的优劣
### 5. **经典例子的扩展描述**
- 为每个例子添加了更多背景和分析
- 特别是"Phil准备早餐"的例子,详细分析了日常活动的复杂性
- 总结了所有例子的共同模式
### 6. **强化学习地位的深度分析**
- 跨学科融合的具体例子(如多巴胺与TD学习的联系)
- AI哲学从"特殊方法"到"通用原则"的转向
- 与生物学习的双向科学进步
现在整个章节读起来更加流畅自然,同时保持了学术的严谨性和深度。内容既适合专业读者理解理论基础,也便于一般读者快速掌握核心概念。
---
### 👤 You
加入这一章节,关于 Agent 和环境、trajectory 的概念之后,注意不要照抄原文,按照你自己的理解,重新描述。
在介绍 Agent 和环境的概念,trajectory 的概念之后,需要指出现在基于大模型的 Agent 和传统 RL 中基于 MDP 的 Agent 最大的区别。最大的区别在于 The Second Half 中指出的:It turned out the most important part of RL might not even be the RL algorithm or environment, but the priors, which can be obtained in a way totally unrelated from RL.@https://ysymyth.github.io/The-Second-Half/ 需要根据 The Second Half 的文章深入对比传统 MDPRich Sutton 原书下述内容)和 LLM-based Agent。
3.1 The AgentEnvironment Interface
MDPs are meant to be a straightforward framing of the problem of learning from
interaction to achieve a goal. The learner and decision maker is called the agent. The
thing it interacts with, comprising everything outside the agent, is called the environment.
These interact continually, the agent selecting actions and the environment responding to
47
48 Chapter 3: Finite Markov Decision Processes
these actions and presenting new situations to the agent.1 The environment also gives
rise to rewards, special numerical values that the agent seeks to maximize over time
through its choice of actions.
Agent
Environment
action
At
reward
Rt
state
St
Rt+1
St+1
Figure 3.1: The agentenvironment interaction in a Markov decision process.
More specifically, the agent and environment interact at each of a sequence of discrete
time steps, t = 0, 1, 2, 3,....
2 At each time step t, the agent receives some representation
of the environments state, St 2 S, and on that basis selects an action, At 2 A(s).3 One
time step later, in part as a consequence of its action, the agent receives a numerical
reward, Rt+1 2 R ⇢ R, and finds itself in a new state, St+1.
4 The MDP and agent
together thereby give rise to a sequence or trajectory that begins like this:
S0, A0, R1, S1, A1, R2, S2, A2, R3,... (3.1)
In a finite MDP, the sets of states, actions, and rewards (S, A, and R) all have a finite
number of elements. In this case, the random variables Rt and St have well defined
discrete probability distributions dependent only on the preceding state and action. That
is, for particular values of these random variables, s0 2 S and r 2 R, there is a probability
of those values occurring at time t, given particular values of the preceding state and
action:
p(s0
, r|s, a) .
= Pr{St =s0
, Rt =r | St1 =s, At1 =a}, (3.2)
for all s0
, s 2 S, r 2 R, and a 2 A(s). The function p defines the dynamics of the MDP.
The dot over the equals sign in the equation reminds us that it is a definition (in this
case of the function p) rather than a fact that follows from previous definitions. The
dynamics function p : S ⇥ R ⇥ S ⇥ A ! [0, 1] is an ordinary deterministic function of four
arguments. The | in the middle of it comes from the notation for conditional probability,
11We use the terms agent, environment, and action instead of the engineers terms controller, controlled
system (or plant), and control signal because they are meaningful to a wider audience.
2We restrict attention to discrete time to keep things as simple as possible, even though many of the
ideas can be extended to the continuous-time case (e.g., see Bertsekas and Tsitsiklis, 1996; Doya, 1996).
3To simplify notation, we sometimes assume the special case in which the action set is the same in all
states and write it simply as A.
4We use Rt+1 instead of Rt to denote the reward due to At because it emphasizes that the next
reward and next state, Rt+1 and St+1, are jointly determined. Unfortunately, both conventions are
widely used in the literature.
but here it just reminds us that p specifies a probability distribution for each choice of s
and a, that is, that
X
s02S
X
r2R
p(s0
, r|s, a)=1, for all s 2 S, a 2 A(s). (3.3)
In a Markov decision process, the probabilities given by p completely characterize the
environments dynamics. That is, the probability of each possible value for St and Rt
depends only on the immediately preceding state and action, St1 and At1, and, given
them, not at all on earlier states and actions. This is best viewed a restriction not on the
decision process, but on the state. The state must include information about all aspects
of the past agentenvironment interaction that make a di↵erence for the future. If it
does, then the state is said to have the Markov property. We will assume the Markov
property throughout this book, though starting in Part II we will consider approximation
methods that do not rely on it, and in Chapter 17 we consider how a Markov state can
be learned and constructed from non-Markov observations.
From the four-argument dynamics function, p, one can compute anything else one might
want to know about the environment, such as the state-transition probabilities (which we
denote, with a slight abuse of notation, as a three-argument function p : S⇥S⇥A ! [0, 1]),
p(s0
|s, a) .
= Pr{St =s0 | St1 =s, At1 =a} = X
r2R
p(s0
, r|s, a). (3.4)
We can also compute the expected rewards for stateaction pairs as a two-argument
function r : S ⇥ A ! R:
r(s, a) .
= E[Rt | St1 =s, At1 =a] = X
r2R
r
X
s02S
p(s0
, r|s, a), (3.5)
and the expected rewards for stateactionnext-state triples as a three-argument function
r : S ⇥ A ⇥ S ! R,
r(s, a, s0
) .
= E[Rt | St1 =s, At1 =a, St = s0
] = X
r2R
r
p(s0
, r|s, a)
p(s0 |s, a) . (3.6)
In this book, we usually use the four-argument p function (3.2), but each of these other
notations are also occasionally convenient.
The MDP framework is abstract and flexible and can be applied to many di↵erent
problems in many di↵erent ways. For example, the time steps need not refer to fixed
intervals of real time; they can refer to arbitrary successive stages of decision making
and acting. The actions can be low-level controls, such as the voltages applied to the
motors of a robot arm, or high-level decisions, such as whether or not to have lunch or
to go to graduate school. Similarly, the states can take a wide variety of forms. They
can be completely determined by low-level sensations, such as direct sensor readings, or
they can be more high-level and abstract, such as symbolic descriptions of objects in a
room. Some of what makes up a state could be based on memory of past sensations or
even be entirely mental or subjective. For example, an agent could be in the state of not
being sure where an object is, or of having just been surprised in some clearly defined
sense. Similarly, some actions might be totally mental or computational. For example,
some actions might control what an agent chooses to think about, or where it focuses its
attention. In general, actions can be any decisions we want to learn how to make, and
the states can be anything we can know that might be useful in making them.
In particular, the boundary between agent and environment is typically not the same
as the physical boundary of a robots or animals body. Usually, the boundary is drawn
closer to the agent than that. For example, the motors and mechanical linkages of a robot
and its sensing hardware should usually be considered parts of the environment rather
than parts of the agent. Similarly, if we apply the MDP framework to a person or animal,
the muscles, skeleton, and sensory organs should be considered part of the environment.
Rewards, too, presumably are computed inside the physical bodies of natural and artificial
learning systems, but are considered external to the agent.
The general rule we follow is that anything that cannot be changed arbitrarily by
the agent is considered to be outside of it and thus part of its environment. We do
not assume that everything in the environment is unknown to the agent. For example,
the agent often knows quite a bit about how its rewards are computed as a function of
its actions and the states in which they are taken. But we always consider the reward
computation to be external to the agent because it defines the task facing the agent and
thus must be beyond its ability to change arbitrarily. In fact, in some cases the agent may
know everything about how its environment works and still face a dicult reinforcement
learning task, just as we may know exactly how a puzzle like Rubiks cube works, but
still be unable to solve it. The agentenvironment boundary represents the limit of the
agents absolute control, not of its knowledge.
The agentenvironment boundary can be located at di↵erent places for di↵erent
purposes. In a complicated robot, many di↵erent agents may be operating at once, each
with its own boundary. For example, one agent may make high-level decisions which form
part of the states faced by a lower-level agent that implements the high-level decisions. In
practice, the agentenvironment boundary is determined once one has selected particular
states, actions, and rewards, and thus has identified a specific decision making task of
interest.
The MDP framework is a considerable abstraction of the problem of goal-directed
learning from interaction. It proposes that whatever the details of the sensory, memory,
and control apparatus, and whatever objective one is trying to achieve, any problem of
learning goal-directed behavior can be reduced to three signals passing back and forth
between an agent and its environment: one signal to represent the choices made by the
agent (the actions), one signal to represent the basis on which the choices are made (the
states), and one signal to define the agents goal (the rewards). This framework may not
be sucient to represent all decision-learning problems usefully, but it has proved to be
widely useful and applicable.
Of course, the particular states and actions vary greatly from task to task, and how
they are represented can strongly a↵ect performance. In reinforcement learning, as in
other kinds of learning, such representational choices are at present more art than science.
3.1. The AgentEnvironment Interface 51
In this book we o↵er some advice and examples regarding good ways of representing
states and actions, but our primary focus is on general principles for learning how to
behave once the representations have been selected.
Example 3.1: Bioreactor Suppose reinforcement learning is being applied to determine
moment-by-moment temperatures and stirring rates for a bioreactor (a large vat of
nutrients and bacteria used to produce useful chemicals). The actions in such an
application might be target temperatures and target stirring rates that are passed to
lower-level control systems that, in turn, directly activate heating elements and motors to
attain the targets. The states are likely to be thermocouple and other sensory readings,
perhaps filtered and delayed, plus symbolic inputs representing the ingredients in the
vat and the target chemical. The rewards might be moment-by-moment measures of the
rate at which the useful chemical is produced by the bioreactor. Notice that here each
state is a list, or vector, of sensor readings and symbolic inputs, and each action is a
vector consisting of a target temperature and a stirring rate. It is typical of reinforcement
learning tasks to have states and actions with such structured representations. Rewards,
on the other hand, are always single numbers.
Example 3.2: Pick-and-Place Robot Consider using reinforcement learning to
control the motion of a robot arm in a repetitive pick-and-place task. If we want to learn
movements that are fast and smooth, the learning agent will have to control the motors
directly and have low-latency information about the current positions and velocities of the
mechanical linkages. The actions in this case might be the voltages applied to each motor
at each joint, and the states might be the latest readings of joint angles and velocities.
The reward might be +1 for each object successfully picked up and placed. To encourage
smooth movements, on each time step a small, negative reward can be given as a function
of the moment-to-moment “jerkiness” of the motion.
3.2 Goals and Rewards
In reinforcement learning, the purpose or goal of the agent is formalized in terms of a
special signal, called the reward, passing from the environment to the agent. At each time
step, the reward is a simple number, Rt 2 R. Informally, the agents goal is to maximize
the total amount of reward it receives. This means maximizing not immediate reward,
but cumulative reward in the long run. We can clearly state this informal idea as the
reward hypothesis:
That all of what we mean by goals and purposes can be well thought of as
the maximization of the expected value of the cumulative sum of a received
scalar signal (called reward).
The use of a reward signal to formalize the idea of a goal is one of the most distinctive
features of reinforcement learning.
Although formulating goals in terms of reward signals might at first appear limiting,
in practice it has proved to be flexible and widely applicable. The best way to see this is
to consider examples of how it has been, or could be, used. For example, to make a robot
learn to walk, researchers have provided reward on each time step proportional to the
robots forward motion. In making a robot learn how to escape from a maze, the reward
is often 1 for every time step that passes prior to escape; this encourages the agent to
escape as quickly as possible. To make a robot learn to find and collect empty soda cans
for recycling, one might give it a reward of zero most of the time, and then a reward of
+1 for each can collected. One might also want to give the robot negative rewards when
it bumps into things or when somebody yells at it. For an agent to learn to play checkers
or chess, the natural rewards are +1 for winning, 1 for losing, and 0 for drawing and
for all nonterminal positions.
You can see what is happening in all of these examples. The agent always learns to
maximize its reward. If we want it to do something for us, we must provide rewards
to it in such a way that in maximizing them the agent will also achieve our goals. It
is thus critical that the rewards we set up truly indicate what we want accomplished.
In particular, the reward signal is not the place to impart to the agent prior knowledge
about how to achieve what we want it to do.5 For example, a chess-playing agent should
be rewarded only for actually winning, not for achieving subgoals such as taking its
opponents pieces or gaining control of the center of the board. If achieving these sorts
of subgoals were rewarded, then the agent might find a way to achieve them without
achieving the real goal. For example, it might find a way to take the opponents pieces
even at the cost of losing the game. The reward signal is your way of communicating to
the robot what you want it to achieve, not how you want it achieved.6
---
### 🤖 Cursor Assistant
我来根据姚顺雨的《The Second Half》文章和Sutton & Barto的MDP理论,在第一章中加入Agent与环境交互、trajectory概念,以及传统RL与现代LLM Agent的深度对比。
---
### 🤖 Cursor Assistant
完成!我已经根据姚顺雨的《The Second Half》文章,在第一章中加入了关于Agent与环境交互、trajectory概念,以及传统RL与现代LLM Agent的深度对比。
## 主要新增内容:
### 1. **Agent与环境的交互接口**
- 详细解释了Agent-Environment的交互循环
- 引入了Trajectory(轨迹)概念及其重要性
- 探讨了Agent-环境边界的哲学问题(控制权vs知识的界限)
### 2. **从传统RL到大模型时代的范式转变**
基于姚顺雨的核心洞察,深入分析了:
- **传统RL的三要素**:算法、环境、先验(以及错误的优先级)
- **OpenAI的探索历程**:从Gym到GPT,从环境到先验的认知演变
- **革命性发现**:先验知识才是最重要的,而非算法或环境
### 3. **现代LLM Agent的突破**
- **语言作为通用先验**:将人类知识压缩进模型
- **推理作为特殊动作**:思考虽不改变世界,却是利用先验的关键
- **算法的边缘化**:当先验足够强大,简单的学习方法就够了
### 4. **两种Agent范式的深度对比表**
从知识来源、泛化能力、样本效率、动作空间、学习重点、失败模式、优化目标等七个维度,全面对比了传统MDP Agent和LLM-based Agent的区别。
### 5. **AI发展的两个阶段**
- **第一阶段**:方法驱动,重在发明新算法
- **第二阶段**:评估驱动,重在定义有用的任务
- 标志性转折:RL终于泛化了
### 6. **对Agent设计的五点启示**
为现代Agent设计提供了实践指导,强调利用先验、纳入推理、简化算法、关注效用、拥抱组合性。
这些内容将Sutton & Barto的经典理论与姚顺雨的现代洞察有机结合,展现了从传统强化学习到现代LLM Agent的演进脉络,帮助读者理解为什么"我们正处于AI的中场"。
参考来源:
- [姚顺雨 - The Second Half](https://ysymyth.github.io/The-Second-Half/)
---
### 👤 You
#### 马尔可夫决策过程(MDP
这一节是不是应该移动到 从传统RL到大模型时代:范式的根本转变 之前,需要有两个小节,一个小节是 MDP,一个小节是基于 LLM + RL 的 Agent(例子:Kimi K2OpenVLA 模型)
---
### 🤖 Cursor Assistant
您说得对,这样的结构会更清晰。让我重新组织这部分内容,先介绍传统MDP,再介绍基于LLM+RL的现代Agent,最后进行范式对比。
---
### 🤖 Cursor Assistant
完成!我已经重新组织了章节结构,现在的安排更加清晰:
## 新的章节结构:
### 1. **Agent与环境的交互接口**
- 交互的基本循环
- Trajectory(轨迹)概念
- Agent-环境边界的哲学
### 2. **两种Agent范式:从MDP到LLM+RL**
这是新增的主要部分,包含两个并列的小节:
#### 传统范式:马尔可夫决策过程(MDP)
- MDP的形式化定义(五元组)
- 马尔可夫性质的含义
- 传统MDP Agent的学习过程
- MDP的局限性
#### 现代范式:基于LLM+RL的Agent
- 核心创新:预训练作为强大先验
- 推理作为核心能力(代码示例对比)
- **成功案例:Kimi K2**
- 技术特点和关键突破
- 统一的问题解决框架
- **成功案例:OpenVLA**
- 视觉-语言-动作的统一架构
- 语言条件控制和跨任务泛化
- LLM+RL的独特优势
### 3. **从传统RL到大模型时代:范式的根本转变**
- 基于姚顺雨的洞察进行深度分析
- OpenAI的探索历程
- 两种范式的对比表
- AI发展的两个阶段
这种结构的优势:
1. **逻辑清晰**:先分别介绍两种范式,再进行对比分析
2. **循序渐进**:从基础概念到具体案例,再到深层洞察
3. **理论与实践结合**:既有MDP的理论基础,又有Kimi K2、OpenVLA等实际案例
4. **突出转变**:通过并列对比,更清楚地展示了从传统到现代的根本性变化
读者能够更好地理解为什么"RL中最重要的可能根本不是RL算法或环境设计,而是先验知识"这一革命性洞察。
---
### 👤 You
从传统RL到大模型时代:范式的根本转变 和 前面的内容是不是有些重复?
---
### 🤖 Cursor Assistant
您说得对,确实有一些重复。让我精简"从传统RL到大模型时代:范式的根本转变"这一节,去掉重复内容,聚焦于姚顺雨的独特洞察和AI发展阶段的分析。
---
### 🤖 Cursor Assistant
完成!我已经精简并重新组织了这一节,去掉了重复内容。主要改动:
## 删除的重复内容:
1. **传统RL的三要素及其优先级** - 前面MDP部分已介绍
2. **现代LLM Agent的革命性突破** - 前面LLM+RL部分已详细说明
3. **两种Agent范式的对比表** - 避免重复对比
4. **语言作为通用先验、推理作为特殊动作** - 前面已经讲过
## 保留并强化的独特内容:
### 1. **范式转变的历史视角:OpenAI的探索之路**
通过OpenAI的具体发展历程展示认知演变:
- 第一阶段:算法中心主义(2015-2016)
- 第二阶段:环境的重要性(2016-2018)
- 第三阶段:先验的觉醒(2018至今)
### 2. **AI发展的两个时代**
姚顺雨的核心观点:
- **第一时代(The First Half**:方法的黄金时代
- **第二时代(The Second Half**:效用的新纪元
- **效用问题**:为什么AI进步没有带来相应的世界改变
### 3. **对当前Agent研究的具体启示**
- 方法创新的边际收益递减
- 评估范式需要根本革新
- 从发明到发现的转变
现在的结构更加精炼,每一节都提供了独特的视角和价值,没有冗余重复。整体叙事从理论基础(MDP)→ 现代实践(LLM+RL)→ 历史演变(OpenAI)→ 未来展望(两个时代),逻辑更加清晰。
---
*Exported from [Cursor View](https://github.com/saharmor/cursor-view)*