memoryagentllmarchitecture type: concept 创建: 2026-04-10 更新: 2026-04-10

DeerFlow Memory 系统

DeerFlow 的长期记忆系统,通过 LLM 从对话中提取结构化事实并持久化,在后续交互中自动注入上下文。

架构概览

对话消息

   │ MemoryMiddleware(入队)

MemoryUpdateQueue(Debounce 30s + per-thread 去重)


后台线程:MemoryUpdater._process_updates()

   │ LLM 调用(MEMORY_UPDATE_PROMPT)

facts 提取 + 去重 + 原子写入

memory.json(持久化)

   │ MemoryMiddleware(下次注入)

<memory> 标签(最多15 facts)

MemoryUpdateQueue(队列+Debounce)

class MemoryUpdateQueue:
    def add(thread_id, messages, agent_name=None,
            correction_detected=False, reinforcement_detected=False):
        # Per-thread 去重:新消息合并到同一 ConversationContext
        # 启动/重置 debounce timer(30s 可配置)
        # 时间到了 → _process_updates() 批量处理

信号检测

  • correction_detected:用户明确纠正了 agent 的方向/理解/输出
  • reinforcement_detected:用户给予了正面反馈

这两个信号在 prompt 中影响 fact 置信度和分类。

MEMORY_UPDATE_PROMPT(LLM 提取)

两阶段工作:

  1. Reflection
    • 错误/重试检测 → category=“correction”
    • 用户纠正检测 → category=“correction” + sourceError
    • 项目约束发现 → 记录为 facts
  2. Memory 更新:按 section 指南更新结构

Section 指南

Section内容篇幅
workContext专业角色/公司/项目/技术栈2-3句
personalContext语言/沟通偏好/关键兴趣1-2句
topOfMind多个并发关注点(详细段落)3-5句
recentMonths近1-3个月活动详细总结4-6句/1-2段
earlierContext3-12个月重要历史模式3-5句
Facts结构化事实条目(见下)

Fact 结构

{
    "id": "uuid",
    "content": "对事实的文字描述",
    "category": "preference|knowledge|context|behavior|goal|correction",
    # category 含义:
    #   preference: 用户偏好(语言/风格/格式偏好)
    #   knowledge: 知识背景(技术栈/领域知识)
    #   context: 工作/生活上下文
    #   behavior: 行为模式(如何工作/沟通)
    #   goal: 已知目标/优先级
    #   correction: 纠正(包含 sourceError)
    "preference": "...",  # 可选,详细偏好说明
    "confidence": 0.0-1.0,  # 置信度
    "createdAt": "ISO8601",
    "source": "conversation|manual"  # 来源
}

注入机制

MemoryMiddleware 在每次 agent 交互后:

  1. 队列消息(异步,不阻塞主流程)
  2. 下次交互时,将最多 15 个 fact 注入到 <memory> 标签

注入格式:

<memory>
{facts}
</memory>

存储

backend/.deer-flow/memory.json,原子写入:

# temp file + rename(操作系统原子性保证)
temp_path = path.with_suffix('.tmp')
temp_path.write_text(json.dumps(data, ensure_ascii=False, indent=2))
temp_path.rename(path)

配置

memory:
  enabled: true
  debounce_seconds: 30
  max_facts: 15

与 nanobot Dream 的区别

维度DeerFlow Memorynanobot Dream
触发每次交互后异步(Debounce)Cron 调度(默认2小时)
输出结构化 facts + context sections最小编辑 MEMORY.md/SOUL.md
持久化JSON 文件Markdown 文件 + GitStore
注入<memory> XML 标签直接追加到 prompt
规模更轻量,fact-based更重,完整记忆文档

DeerFlow memory 是”fact 提取”,nanobot Dream 是”文档编辑”。

与 MemPalace 的对比

维度DeerFlow MemoryMemPalace
存储JSON 文件ChromaDB verbatim + SQLite KG
触发Debounce 30s 队列Hook 触发(Stop / PreCompact)
提取LLM fact 提取无提取,raw 存储
检索prompt 内注入语义搜索 + KG 查询
KGSQLite temporal triples
Agent 记忆Specialist Agents + diary
审计WAL JSONL 审计
benchmarkLongMemEval 96.6%

相关概念

  • deer-flow — memory 系统的宿主框架
  • memory-system — nanobot 的两阶段 Dream 记忆系统(对比)