深度智能体自带本地文件系统,用于卸载临时记忆。该文件系统存储在状态中,因此仅在单个线程内有效——当会话结束时,文件会被清除。 通过提供 LangGraph 的 Store 并设置 use_longterm_memory=True,可以为深度智能体启用长期记忆,让数据在多个线程与会话之间持久存在。

环境配置

from deepagents import create_deep_agent
from langgraph.store.memory import InMemoryStore

store = InMemoryStore()  # 也可以使用任何其他 Store 实现
agent = create_deep_agent(
    store=store,
    use_longterm_memory=True
)

工作原理

启用长期记忆后,深度智能体会维护两个独立的文件系统

1. 短期(临时)文件系统

  • 存储在智能体状态中
  • 仅在单个线程内持续有效
  • 线程结束后文件被清除
  • 通过常规路径访问,如 /notes.txt

2. 长期(持久)文件系统

  • 存储在 LangGraph Store 中
  • 跨所有线程和会话持久存在
  • 文件永久保存
  • 使用特殊前缀访问,如 /memories/notes.txt

/memories/ 路径约定

长期记忆的关键是 /memories/ 前缀:
  • /memories/ 开头的路径会写入 Store,实现持久化
  • 没有此前缀的文件仍保存在临时状态中
  • 所有文件系统工具(lsread_filewrite_fileedit_file)对两者都适用
# 临时文件(线程结束后丢失)
agent.invoke({
    "messages": [{"role": "user", "content": "Write draft to /draft.txt"}]
})

# 持久文件(跨线程保留)
agent.invoke({
    "messages": [{"role": "user", "content": "Save final report to /memories/report.txt"}]
})

跨线程持久化

位于 /memories/ 下的文件可以在任何线程中访问:
import uuid

# 线程 1:写入长期记忆
config1 = {"configurable": {"thread_id": str(uuid.uuid4())}}
agent.invoke({
    "messages": [{"role": "user", "content": "Save my preferences to /memories/preferences.txt"}]
}, config=config1)

# 线程 2:读取长期记忆(全新会话)
config2 = {"configurable": {"thread_id": str(uuid.uuid4())}}
agent.invoke({
    "messages": [{"role": "user", "content": "What are my preferences?"}]
}, config=config2)
# 智能体会读取线程 1 中保存的 /memories/preferences.txt

典型用例

用户偏好

存储跨会话持久的用户偏好:
agent = create_deep_agent(
    store=store,
    use_longterm_memory=True,
    system_prompt="""当用户告诉您他们的偏好时,请将它们保存到
    /memories/user_preferences.txt 以便您在未来的对话中记住它们。"""
)

自我改进指令

智能体可以根据反馈更新自己的指令:
agent = create_deep_agent(
    store=store,
    use_longterm_memory=True,
    system_prompt="""您有一个文件在 /memories/instructions.txt 中,其中包含额外的
    指令和偏好。

    在对话开始时阅读此文件以了解用户偏好。

    当用户提供反馈如“请始终做 X”或“我更喜欢 Y”时,
    使用 edit_file 工具更新 /memories/instructions.txt。"""
)
随着时间推移,指令文件会不断积累用户偏好,帮助智能体持续改进。

知识库

跨多轮对话逐步构建知识:
# 对话 1:记录项目信息
agent.invoke({
    "messages": [{"role": "user", "content": "We're building a web app with React. Save project notes."}]
})

# 对话 2:使用已有知识
agent.invoke({
    "messages": [{"role": "user", "content": "What framework are we using?"}]
})
# 智能体会读取 /memories/project_notes.txt 中的内容

研究项目

在多个会话之间持续维护研究进度:
research_agent = create_deep_agent(
    store=store,
    use_longterm_memory=True,
    system_prompt="""You are a research assistant.

    请将研究进度保存到 /memories/research/:
    - /memories/research/sources.txt —— 已找到的信息来源
    - /memories/research/notes.txt —— 关键发现与笔记
    - /memories/research/report.md —— 最终报告草稿

    这样可以让研究进度在多个会话之间持续推进。"""
)
(上述系统提示可改写为中文,例如:“这样可以让研究工作在多个会话之间持续进行。”)

Store 实现

任何 LangGraph 的 BaseStore 实现都可以搭配长期记忆使用。

InMemoryStore(开发环境)

适合测试和开发,但重启后数据会丢失:
from langgraph.store.memory import InMemoryStore

store = InMemoryStore()
agent = create_deep_agent(store=store, use_longterm_memory=True)

PostgresStore(生产环境)

在生产环境中使用持久化存储:
from langgraph.store.postgres import PostgresStore
import os

store = PostgresStore(connection_string=os.environ["DATABASE_URL"])
agent = create_deep_agent(store=store, use_longterm_memory=True)

最佳实践

使用描述性路径

为长期文件设计清晰的层级结构:
# ✅ 优秀:结构清晰
/memories/user_preferences/language.txt
/memories/projects/project_alpha/status.txt
/memories/research/quantum_computing/sources.txt

# ❌ 糟糕:命名随意
/memories/temp.txt
/memories/data.txt
/memories/file1.txt

说明哪些内容需要持久化

在系统提示中明确何时使用长期或短期存储:
system_prompt="""You have access to two types of storage:

SHORT-TERM (paths without /memories/):
- Current conversation notes
- Temporary scratch work
- Draft documents

LONG-TERM (paths starting with /memories/):
- User preferences and settings
- Completed reports and documents
- Knowledge that should persist across conversations
- Project state and progress

Always use /memories/ for information that should survive beyond this conversation."""

按助手身份隔离存储

在多租户应用中,通过 assistant_id 隔离不同助手的数据:
config = {
    "configurable": {
        "thread_id": "thread-123",
    },
    "metadata": {
        "assistant_id": "user-456"  # 为不同助手划分命名空间
    }
}

agent.invoke({"messages": [...]}, config=config)
每个助手在 Store 中会有独立的命名空间,避免数据相互污染。

在生产环境使用持久化 Store

# ❌ 仅适用于开发,重启后数据丢失
store = InMemoryStore()

# ✅ 适用于生产,数据持久化
from langgraph.store.postgres import PostgresStore
store = PostgresStore(connection_string=os.environ["DATABASE_URL"])

列出文件

ls 工具会同时显示两个文件系统中的文件:
agent.invoke({
    "messages": [{"role": "user", "content": "List all files"}]
})

# 示例输出:
# Transient files:
# - /draft.txt
# - /temp_notes.txt
#
# Long-term files:
# - /memories/user_preferences.txt
# - /memories/project_status.txt
列表中带有 /memories/ 前缀的文件来自持久化 Store。

限制

必须提供 Store

启用长期记忆时必须传入 Store:
# ❌ 会报错
agent = create_deep_agent(use_longterm_memory=True)  # 未提供 store

# ✅ 正确写法
agent = create_deep_agent(
    use_longterm_memory=True,
    store=InMemoryStore()
)

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.