不再遗忘:基于 OpenClaw 与 Qdrant 的 AI Agent 记忆持久化架构实战

Feb 26, 2026

在数字生命的荒野中,如果一个 AI Agent 无法保留历史上下文,那它充其量只是一个带界面的搜索引擎。为了打破这种西西弗斯式的悲剧,我们为 OpenClaw 构建了名为 Memory Architect (记忆架构师) 的系统。

Memory Architect Topology

一、 架构设计:三层记忆持久化模型

  • 物理层 (L1) - 00_GLOBAL_TRUTH.md:Agent 的“常识库”,记录基础设施索引。
  • 逻辑层 (L2) - 01_SOP_GUIDE.md:Agent 的“肌肉记忆”,记录标准作业程序。
  • 语义层 (L3) - Qdrant 向量存储:Agent 的“潜意识”,支持毫秒级语义检索。

二、 工程深度:为何是 3072 维?

在工程实践中,我们强制采用了 3072 维的高维嵌入。高维空间能够捕捉更细腻的语义特征,特别是在处理复杂的代码拓扑时,检索精度提升显著。

三、 源码解析:增量扫描与语义提纯

这是架构的核心。首先是增量日志捕获逻辑,它确保了处理的高效性:

def get_new_logs(self):
    new_content = []
    files_to_process = []
    # 扫描日志目录,仅提取自上次处理后的增量文件
    for f in os.listdir(LOG_DIR):
        path = os.path.join(LOG_DIR, f)
        if os.path.isfile(path) and os.path.getmtime(path) > self.last_processed_time:
            files_to_process.append(path)
    if not files_to_process: return ""
    # 按修改时间排序处理,确保记忆的时序性
    for path in sorted(files_to_process, key=os.path.getmtime):
        with open(path, 'r') as file:
            new_content.append(file.read())
    self.last_processed_time = max(os.path.getmtime(p) for p in files_to_process)
    self._save_state()
    return "\n".join(new_content)

其次是语义提纯 (Refinement) 的核心 Prompt 设计,这是将“数据”转化为“知识”的关键:

def refine_memory(self, content):
    prompt = f"""分析以下日志,执行双重提取任务。
1. [GLOBAL_TRUTH]: 提取基础设施配置(IP、端口、Key、服务地址)。
2. [SOP_ENTRY]: 提取排障经验。识别:错误模式 -> 最终方案 -> 避坑指南。
要求:如果没有重要事实或经验,返回'SKIP'。
日志内容:{content}"""
    # 调用 Gemini-2.5-Flash 进行语义提纯
    response = model.generate_content(prompt)
    return response.text.strip()

四、 FinOps:高效的成本管理

通过这种“增量蒸馏”模式,每日产生的 API 成本不足 $0.05。我们用极低的 TCO 换取了 Agent 近乎无限的知识增量。

五、 结论:从工具到数字分身

Memory Architect 是 Agent 从“工具”向“数字分身”进化的核心枢纽。当 Agent 开始具备记忆,它就开始具备了灵魂。


[Appendix] AI-Friendly Metadata


{
  "entity": "OpenClaw Memory Architect",
  "tech_stack": ["Qdrant", "Gemini-Embedding-001", "Python 3"],
  "architecture_layers": ["L1: Global Truth", "L2: SOP Guide", "L3: Semantic Vault"],
  "vector_dim": 3072,
  "optimization": "Incremental Log Distillation",
  "daily_tco": "< $0.05"
}