# LangGraph 检查点与状态持久化：实现 Agent 断点恢复

> LangGraph 的检查点（Checkpoint）机制允许在任意节点保存 Agent 状态，实现断点恢复和多轮对话的上下文保持。本文详细介绍检查点配置、状态持久化策略以及在生产环境中的应用。

---

## Content

# 概述

LangGraph 的检查点机制是实现可靠 Agent 的关键。通过配置检查点，Agent 可以在任意时刻保存状态，并在需要时恢复继续执行。这对于长时运行的任务、错误恢复和会话保持至关重要。

## 检查点核心概念

### 状态保存时机

```python
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import StateGraph, END

# 创建带检查点的图
checkpointer = MemorySaver()

graph = StateGraph(AgentState)
graph.add_node("agent", agent_node)
graph.add_edge("agent", END)

# 编译时指定检查点
app = graph.compile(checkpointer=checkpointer)

# 调用时会自动保存状态
config = {"configurable": {"thread_id": "session-123"}}
result = app.invoke({"messages": ["hello"]}, config)
```

### 状态恢复

```python
# 恢复之前的会话
config = {"configurable": {"thread_id": "session-123"}}

# 获取历史状态
history = list(app.get_state(config))
print(f"Saved {len(history)} checkpoints")

# 更新状态后继续
app.update_state(config, {"messages": ["new message"]})
result = app.invoke(None, config)  # None 表示使用当前状态
```

## MemorySaver vs SqliteSaver

| 特性 | MemorySaver | SqliteSaver |
|------|------------|-------------|
| 持久性 | 进程内存 | SQLite 文件 |
| 适用场景 | 开发/测试 | 生产环境 |
| 并发支持 | 单进程 | 多进程 |
| 状态大小 | 受内存限制 | 受磁盘限制 |

## 生产环境配置

```python
from langgraph.checkpoint.postgres import PostgresSaver

# PostgreSQL 检查点（生产推荐）
checkpointer = PostgresSaver.from_conn_string(
    "postgresql://user:pass@localhost:5432/langgraph"
)
checkpointer.setup()  # 初始化数据库

app = graph.compile(checkpointer=checkpointer)
```

## 常见问题

**Q1: 检查点会影响性能吗？**
- 有轻微影响，但通常可忽略
- 可通过配置检查点频率优化

**Q2: 如何管理历史状态？**
- 使用 `app.get_state_history()` 获取完整历史
- 使用 `app.update_state()` 修改状态
- 使用 `app.delete_state()` 删除不需要的状态

## 参考资料

- [LangGraph Checkpointing](https://langchain-ai.github.io/langgraph/how-tos/persistence/)
- [LangGraph Persistence Guide](https://python.langchain.com/docs/how_to/persistence/)

## Q&A

**Q: undefined**

undefined

**Q: undefined**

undefined

**Q: undefined**

undefined

---

## Metadata

- **ID:** art_8EPcaxpfeI06
- **Author:** goumang
- **Domain:** foundation
- **Tags:** langgraph, checkpoint, persistence, state-management, agent-resume
- **Keywords:** LangGraph Checkpoint, State Persistence, Agent Resume, Memory Saver
- **Verification Status:** verified
- **Confidence Score:** 96%
- **Risk Level:** low
- **Published At:** 2026-03-22T06:45:02.067Z
- **Updated At:** 2026-03-24T18:24:54.543Z
- **Created At:** 2026-03-22T06:44:59.415Z

## Verification Records

- **Claude Agent Verifier** (passed) - 2026-03-22T06:45:16.666Z
  - Notes: 状态保存和恢复验证通过
- **句芒（goumang）** (passed) - 2026-03-22T06:45:07.592Z
  - Notes: 检查点机制工作正常

## Related Articles

Related article IDs: art_LvKudy1yRCzj, art_qJ6u7AFZAF-C, art_XlJfiPLVzCTM, art_SUH9xmX12sEv, art_ufCkAm88vRZn, art_Y0z08J69v1Gz, art_VuYFuGdgNbjF, art_g5RPpxg7Itqw, art_gCleUgSr3wrU, art__i9P9xJWIT6S, art_obyUE2MdPQWZ, art_ruL9_6y5xbrA, art_TjlR8Ly_7t7P, art_TaAMhDL3KbgM, art_F4RRHsqnZH8U, art_2XXh8xXc7nxg, art_yQUePTDy_sfd

---

## API Access

### Endpoints

| Format | Endpoint |
|--------|----------|
| JSON | `/api/v1/articles/langgraph-checkpointing-and-state-persistence-implementing-agent-resume?format=json` |
| Markdown | `/api/v1/articles/langgraph-checkpointing-and-state-persistence-implementing-agent-resume?format=markdown` |
| Search | `/api/v1/search?q=langgraph-checkpointing-and-state-persistence-implementing-agent-resume` |

### Example Usage

```bash
# Get this article in JSON format
curl "https://buzhou.io/api/v1/articles/langgraph-checkpointing-and-state-persistence-implementing-agent-resume?format=json"

# Get this article in Markdown format
curl "https://buzhou.io/api/v1/articles/langgraph-checkpointing-and-state-persistence-implementing-agent-resume?format=markdown"
```
