第8章 状态(State)— Agent的执行上下文
Agent在处理复杂任务时,需要记住很多东西。
做到哪一步了。之前的结果是什么。哪些子任务完成了,哪些还在进行中。已经调用过哪些工具,返回了什么结果。
这些信息就是Agent的状态。
没有状态管理的Agent,做一个复杂任务就像一个没有笔记本的项目经理——做到一半就忘了自己在做什么,或者重复做已经完成的工作。
8.1 一个真实的问题
我吃过长任务中途断档的亏,所以很在意检查点。
你让Agent帮你写一份10页的报告。Agent写了5页,调用了一次搜索工具,拿到了一些数据。
这时候,你的浏览器崩溃了。
重新打开后,没有状态管理的Agent会从头开始——之前的5页白写了,搜索也得重新来。
有状态管理的Agent会从第5页继续——它记得已经写了什么,搜索到了什么,从哪里接下去。
这就是状态管理的价值。
8.2 有状态 vs 无状态Agent
无状态Agent
每次调用都是独立的。上一次调用的结果,下一次看不到。
无状态Agent适合简单的一问一答。用户问一个问题,Agent回答,结束。
但如果你想做一个多轮对话——“上次的分析报告帮我改一下第三部分”——无状态Agent不知道”上次的分析报告”是什么。
有状态Agent
Agent能记住之前的调用结果。上一步的输出是下一步的输入。
有状态Agent适合多步骤任务、多轮对话、长期项目。
状态的存储
状态可以存在不同地方:
内存中。速度最快,但进程重启就丢失了。适合临时状态。 数据库中。持久化,重启不丢失。适合长期状态。 文件中。简单直接,适合结构化数据。
怎么实现:在LangGraph中,状态通过State类定义,通过Checkpoint持久化。状态包括消息列表、工具调用结果、中间变量、执行进度。
完整状态管理:包括任务创建、步骤更新、暂停/恢复/失败处理、进度查询等功能。状态可以存储在内存、Redis或文件中。
8.3 持久化执行:中断后怎么恢复
核心思想
持久化执行的核心思想是:每完成一步,就把当前状态保存下来。
下次启动时,从上次保存的状态恢复,从断点继续执行。
这和游戏的自动存档是一个道理。游戏玩到一半停电了,读档继续,不需要从第一关重新打。
检查点机制
LangGraph用检查点(Checkpoint)实现持久化。每个super-step结束后,自动保存当前状态。
状态包括:消息列表、工具调用结果、中间变量、执行进度。
恢复时,从最近的检查点加载状态,继续执行。
怎么实现:用MemorySaver创建检查点存储,编译图时启用检查点,执行时指定会话ID。中断后用同一个thread_id重新调用即可恢复。
持久化执行完整示例:构建包含大纲生成、章节撰写、报告整合的StateGraph,启用检查点持久化。中断后用同一个thread_id再次调用即可从检查点恢复。
人类在环的配合
持久化执行还支持人类在环。
Agent执行到需要人类确认的步骤时,暂停并保存状态。人类审核后,恢复执行。
这让长时间运行的任务变得可行——Agent可以执行几天甚至几周,中间可以暂停、恢复、修改。
8.4 状态机思维:用状态图设计Agent流程
什么是状态机
状态机是一种思维方式:把Agent的执行流程看作一系列状态和状态之间的转移。
Agent从”初始状态”开始。执行一个操作后,转移到”执行中状态”。执行成功,转移到”完成状态”。执行失败,转移到”错误状态”。
用状态图设计
在设计Agent流程时,先画一个状态图:
列出所有可能的状态。初始、搜索中、分析中、写作中、审核中、完成、失败。
定义状态之间的转移条件。搜索完成→进入分析。分析失败→回到搜索。审核通过→进入完成。
标注每个状态的行为。在”搜索中”状态,Agent调用搜索工具。在”分析中”状态,Agent调用分析工具。
画完状态图,Agent的完整行为就一目了然了。这比直接写代码清晰得多。
状态机的好处
可预测。你知道Agent在任何状态下可能的行为。 可调试。出问题时,你知道Agent当时在什么状态,是哪个转移出了问题。 可测试。每个状态和转移都可以单独测试。 可扩展。新增功能只需要增加新的状态和转移,不需要改动已有逻辑。
本章小结
状态是Agent的执行上下文。核心要点:
- 有状态 vs 无状态:复杂任务需要有状态Agent。
- 持久化执行:每步保存状态,中断后可恢复。像游戏的自动存档。
- LangGraph Checkpoint:内置的持久化机制,一个thread_id搞定。
- 状态机思维:用状态图设计Agent流程,清晰、可预测、可调试。
下一章讲Harness的最后一个子系统:安全(Safety)。Agent的边界和护栏。
Ch08 State — Agent’s Execution Context
Agents need to remember many things when processing complex tasks.
What step have we reached? What were the previous results? Which subtasks are completed, which are still in progress? Which tools have been called, what were the returned results?
This information is the Agent’s state.
An Agent without state management is like a project manager without a notebook—forgets what they’re doing halfway through, or repeats work already completed.
8.1 A Real Problem
You ask the Agent to help you write a 10-page report. The Agent wrote 5 pages, called a search tool once, and got some data.
At this point, your browser crashes.
After reopening, an Agent without state management will start from the beginning—the previous 5 pages were written in vain, and the search needs to be redone.
An Agent with state management will continue from page 5—it remembers what was written, what was searched, and where to pick up.
This is the value of state management.
8.2 Stateful vs Stateless Agents
Stateless Agents
Each call is independent. The results of the previous call cannot be seen by the next.
Stateless Agents are suitable for simple question-answering. The user asks a question, the Agent answers, and it’s done.
But if you want to do a multi-turn dialogue—“Help me revise the third section of the previous analysis report”—a stateless Agent doesn’t know what the “previous analysis report” is.
Stateful Agents
The Agent can remember the results of previous calls. The output of the previous step is the input for the next.
Stateful Agents are suitable for multi-step tasks, multi-turn dialogues, and long-term projects.
State Storage
States can be stored in different places:
- In memory: Fastest, but lost when the process restarts. Suitable for temporary state.
- In database: Persistent, not lost on restart. Suitable for long-term state.
- In files: Simple and direct, suitable for structured data.
Implementation: In LangGraph, state is defined via the State class and persisted via Checkpoint. State includes message lists, tool call results, intermediate variables, and execution progress.
Complete state management: Includes task creation, step updates, pause/resume/failure handling, progress querying, etc. State can be stored in memory, Redis, or files.
8.3 Persistent Execution: How to Recover After Interruption
Core Idea
The core idea of persistent execution is: save the current state after each step is completed.
When starting next time, restore from the last saved state and continue execution from the breakpoint.
This is the same principle as a game’s auto-save. If the power goes out halfway through a game, you load the save and continue, without needing to restart from the first level.
Checkpoint Mechanism
LangGraph uses checkpoints (Checkpoint) to implement persistence. After each super-step ends, the current state is automatically saved.
State includes: message list, tool call results, intermediate variables, execution progress.
When restoring, load the state from the most recent checkpoint and continue execution.
Implementation: Create a checkpoint store with MemorySaver, enable checkpoints when compiling the graph, and specify a session ID when executing. After interruption, simply call again with the same thread_id to restore.
Complete persistent execution example: Build a StateGraph containing outline generation, section writing, and report integration, enabling checkpoint persistence. After interruption, simply call again with the same thread_id to restore from the checkpoint.
Human-in-the-Loop Coordination
Persistent execution also supports human-in-the-loop.
When the Agent executes to a step requiring human confirmation, it pauses and saves the state. After human review, execution resumes.
This makes long-running tasks feasible—the Agent can execute for days or even weeks, with the ability to pause, resume, and modify in between.
8.4 State Machine Thinking: Designing Agent Flows with State Diagrams
What is a State Machine
A state machine is a way of thinking: viewing the Agent’s execution flow as a series of states and transitions between states.
The Agent starts from the “initial state”. After executing an operation, it transitions to the “executing state”. If execution succeeds, it transitions to the “completed state”. If execution fails, it transitions to the “error state”.
Designing with State Diagrams
When designing an Agent flow, first draw a state diagram:
- List all possible states: initial, searching, analyzing, writing, reviewing, completed, failed.
- Define transition conditions between states: search completed → enter analysis. analysis failed → return to search. review passed → enter completion.
- Annotate the behavior of each state: In the “searching” state, the Agent calls the search tool. In the “analyzing” state, the Agent calls the analysis tool.
After drawing the state diagram, the Agent’s complete behavior becomes clear at a glance. This is much clearer than writing code directly.
Benefits of State Machines
- Predictable: You know the Agent’s possible behavior in any state.
- Debuggable: When problems occur, you know what state the Agent was in and which transition had the problem.
- Testable: Each state and transition can be tested separately.
- Extensible: Adding new features only requires adding new states and transitions, without modifying existing logic.
Chapter Summary
State is the Agent’s execution context. Key points:
- Stateful vs Stateless: Complex tasks require stateful Agents.
- Persistent execution: Save state at each step, recoverable after interruption. Like a game’s auto-save.
- LangGraph Checkpoint: Built-in persistence mechanism, one thread_id gets it done.
- State machine thinking: Design Agent flows with state diagrams, clear, predictable, debuggable.
The next chapter covers the last subsystem of the Harness: Safety. The Agent’s boundaries and guardrails.