






















┌─────────────────────────────────────────────────────────────────────────┐
│ Agent Application │
└─────────────────────────────────────────────────────────────────────────┘
│
┌─────────────┬───────────────┼───────────────┬─────────────┐
│ │ │ │ │
▼ ▼ ▼ ▼ │
┌─────────────┐ ┌───────────┐ ┌───────────┐ ┌───────────┐ │
│ backend │ │ store │ │checkpointer│ │ memory │ │
│ (操作环境) │ │ (数据库) │ │ (存档器) │ │ (记忆加载)│ │
├─────────────┤ ├───────────┤ ├───────────┤ ├───────────┤ │
│ 文件读写 │ │ 键值存储 │ │ 状态快照 │ │ 加载记忆 │ │
│ 命令执行 │ │ 跨会话共享 │ │ 暂停/恢复 │ │ 注入提示 │ │
└──────┬──────┘ └─────┬─────┘ └─────┬─────┘ └─────┬─────┘ │
│ │ │ │ │
│ │ │ │ │
▼ ▼ ▼ ▼ │
┌─────────────┐ ┌───────────┐ ┌───────────┐ ┌───────────┐ │
│ 文件系统 │ │ 结构化数据 │ │ 对话状态 │ │ 记忆文件 │ │
│ 执行环境 │ │ 长期记忆 │ │ 执行进度 │ │ AGENTS.md │ │
└─────────────┘ └───────────┘ └───────────┘ └───────────┘ │
│
┌──────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 数据流向 │
│ │
│ memory ──加载──▶ system prompt ──▶ AI 决策 ──▶ backend 写入 ──▶ store │
│ │ │
│ ▼ │
│ checkpointer 保存状态 │
└─────────────────────────────────────────────────────────────────────────┘
backend - 操作环境backend=(lambda rt: StoreBackend(rt, namespace=lambda ctx: NAMESPACE))
职责:管理 AI 与外部环境的交互
| 功能 | 说明 |
|---|---|
常见实现:
StateBackend - 文件存在 agent 状态中(临时)FilesystemBackend - 文件存在本地文件系统StoreBackend - 文件存在 LangGraph Store 中(持久化)checkpointer - 状态快照checkpointer=InMemorySaver()
职责:保存对话的执行状态
| 功能 | 说明 |
|---|---|
常见实现:
InMemorySaver - 内存中(临时,重启丢失)SqliteSaver - SQLite 数据库(持久化)PostgresSaver - PostgreSQL(生产环境)store - 结构化数据存储store=store # SqliteStore 实例
职责:提供跨会话的持久化存储
| 功能 | 说明 |
|---|---|
常见实现:
InMemoryStore - 内存中(临时)SqliteStore - SQLite 数据库PostgresStore - PostgreSQL(支持向量搜索)memory - 记忆加载memory=[MEMORY_FILE] # ["/memories/AGENTS.md"]
职责:启动时加载记忆文件到系统提示
| 功能 | 说明 |
|---|---|
| 组件 | 类比 | 管理的数据 | 生命周期 | 典型用途 |
|---|---|---|---|---|
# 第一次对话: "记住公司的主题色是#FD5108"
#
# 1. memory 加载 /memories/AGENTS.md 到 system prompt
# 2. AI 决定使用 edit_file 工具更新记忆
# 3. backend (StoreBackend) 执行文件编辑
# 4. store (SqliteStore) 持久化保存数据
# 5. checkpointer 保存当前对话状态
# 第二次对话: "你有哪些记忆"
#
# 1. memory 再次加载 /memories/AGENTS.md(已包含主题色)
# 2. AI 从 system prompt 中读取到记忆内容
# 3. AI 回答: "公司主题色为 #FD5108"
关键区别:
checkpointer 保存的是对话进度(thread 级别)store 保存的是业务数据(全局共享)backend 是操作接口(怎么读写)memory 是加载机制(读什么)┌─────────────────────────────────────────────────────────────────────────────┐
│ 存储架构对比 │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ StateBackend StoreBackend │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Agent State │ │ LangGraph │ │
│ │ (会话状态) │ │ BaseStore │ │
│ └────────┬────────┘ └────────┬────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Checkpointer │ │ SqliteStore / │ │
│ │ (内存/数据库) │ │ PostgresStore │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
│ 生命周期: thread 级别 生命周期: 全局持久化 │
│ 跨线程共享: ❌ 跨线程共享: ✅ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
# StateBackend 将文件存储在 agent 的 state 中
files = self.runtime.state.get("files", {})
# state 结构:
{
"messages": [...],
"files": {
"/path/to/file.txt": {
"content": ["line1", "line2"],
"created_at": "2025-01-01T00:00:00",
"modified_at": "2025-01-01T00:00:00"
}
}
}
# StateBackend 本身不直接持久化,依赖 checkpointer
# 文件数据作为 state 的一部分被 checkpointer 保存
agent = create_deep_agent(
backend=StateBackend, # 文件存在 state.files 中
checkpointer=InMemorySaver(), # state 被保存在内存中
)
# 数据流:
# write_file() → WriteResult(files_update={...})
# → Command(update={"files": {...}})
# → state 更新
# → checkpointer 保存
| 场景 | 数据是否保留 |
|---|---|
# StoreBackend 使用 LangGraph 的 BaseStore
store = self.runtime.store # SqliteStore / PostgresStore
# 存储结构: namespace + key + value
store.put(
namespace=("memories",), # 命名空间
key="/memories/AGENTS.md", # 文件路径
value={ # 文件数据
"content": ["line1", "line2"],
"created_at": "...",
"modified_at": "..."
}
)
# StoreBackend 直接使用 store 进行持久化
class StoreBackend:
def write(self, file_path: str, content: str) -> WriteResult:
store = self._get_store() # 获取 SqliteStore/PostgresStore
namespace = self._get_namespace()
# 直接写入 store,立即持久化
store.put(namespace, file_path, store_value)
return WriteResult(path=file_path, files_update=None)
# 注意: files_update=None,因为不需要更新 state
| 场景 | 数据是否保留 |
|---|---|
# ============ StateBackend ============
def write(self, file_path: str, content: str) -> WriteResult:
files = self.runtime.state.get("files", {})
# 返回 files_update,让 LangGraph 更新 state
new_file_data = create_file_data(content)
return WriteResult(
path=file_path,
files_update={file_path: new_file_data} # ← 返回更新
)
# ============ StoreBackend ============
def write(self, file_path: str, content: str) -> WriteResult:
store = self._get_store()
namespace = self._get_namespace()
# 直接写入 store,立即持久化
store.put(namespace, file_path, store_value)
return WriteResult(
path=file_path,
files_update=None # ← 不需要更新 state
)
# ============ StateBackend ============
def read(self, file_path: str, ...) -> str:
# 从 state 中读取
files = self.runtime.state.get("files", {})
file_data = files.get(file_path)
# ============ StoreBackend ============
def read(self, file_path: str, ...) -> str:
# 从 store 中读取
store = self._get_store()
namespace = self._get_namespace()
item = store.get(namespace, file_path)
from langgraph.store.sqlite import SqliteStore
# 创建 store
with SqliteStore.from_conn_string("checkpoints.db") as store:
# 写入数据
store.put(
namespace=("memories",), # 命名空间(类似文件夹)
key="/memories/AGENTS.md", # 键(类似文件名)
value={ # 值(文件内容)
"content": ["# Memory", "主题色: #FD5108"],
"created_at": "2025-01-01T00:00:00",
"modified_at": "2025-01-01T00:00:00"
}
)
# 读取数据
item = store.get(("memories",), "/memories/AGENTS.md")
# Item(namespace=['memories'], key='/memories/AGENTS.md', value={...})
# 搜索数据
items = store.search(("memories",)) # 返回该命名空间下所有项
-- SQLite 内部表结构
CREATE TABLE store (
namespace TEXT, -- 命名空间
key TEXT, -- 键
value JSON, -- 值(JSON 格式)
created_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (namespace, key)
);
-- 写入操作
INSERT OR REPLACE INTO store (namespace, key, value, created_at, updated_at)
VALUES ('["memories"]', '/memories/AGENTS.md', '{...}', ..., ...);
-- 读取操作
SELECT * FROM store
WHERE namespace = '["memories"]' AND key = '/memories/AGENTS.md';
| 场景 | 推荐 Backend | 原因 |
|---|---|---|
from deepagents.backends import CompositeBackend, StateBackend, StoreBackend
# 混合存储:临时文件用 StateBackend,长期记忆用 StoreBackend
backend = CompositeBackend(
default=StateBackend(), # 默认使用临时存储
routes={
"/memories/": StoreBackend(namespace=lambda ctx: ("memories",)),
"/knowledge/": StoreBackend(namespace=lambda ctx: ("knowledge",)),
}
)
# 文件路径决定存储位置:
# /tmp/file.txt → StateBackend (临时)
# /memories/AGENTS.md → StoreBackend (持久化)
# /knowledge/docs.md → StoreBackend (持久化)
| 特性 | StateBackend | StoreBackend |
|---|---|---|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。