🧠 OpenClaw Agent 记忆架构完全指南

Memory Skills 详解 + E-E-A-T 原则 + 长期/短期/情景/语义记忆实战

Agent 记忆的重要性

人类的记忆分为短期记忆(工作记忆)和长期记忆,AI Agent 也需要类似的记忆系统才能:

OpenClaw 的记忆架构

1. 短期记忆(Short-term Memory)

📝 对应:Context Window

容量:取决于模型的 Context Window(如 GPT-5.6 Sol 的 256K tokens)

持久性:仅在一次对话/任务中有效

用途:当前任务的推理、工具调用结果、中间步骤

管理策略:滑动窗口、摘要压缩、智能截断

2. 长期记忆(Long-term Memory)

💾 对应:Memory Skills + 外部存储

容量:几乎无限(取决于存储)

持久性:跨会话、跨任务保持

存储后端

# 配置 Memory 存储
memory:
  provider: clawhub/memory-core
  backend:
    type: redis              # 或 sqlite, postgresql, mongodb
    url: redis://localhost:6379
    ttl: 2592000           # 30 天过期(0 = 永不过期)

记忆类型

类型说明示例
情景记忆(Episodic)事件和经历"昨天用户问了关于 SEO 的问题"
语义记忆(Semantic)事实和知识"用户偏好使用 GPT-5.6 Sol"
程序记忆(Procedural)技能和流程"生成 SEO 页面的标准流程"

3. 工作记忆(Working Memory)

🔧 对应:Agent 状态 + 临时变量

用途:正在执行的任务状态、循环变量、条件判断结果

实现:通常存储在 Agent 的运行环境中(内存)

// Agent 工作记忆示例
const workingMemory = {
  currentTask: 'generate-seo-pages',
  progress: 3,              // 已完成 3 页
  total: 10,                // 总共 10 页
  lastResult: {...},         // 上一页的生成结果
  errors: []                // 错误记录
};

Memory Skills 详解

安装 memory-core

# 安装
openclaw skills install clawhub/memory-core

# 验证
openclaw skills info memory-core

基本用法

// 在 Agent 中启用 Memory
// ~/.openclaw/skills/my-agent/SKILL.md
---
name: my-agent
memory:
  provider: clawhub/memory-core
  types: [episodic, semantic, procedural]
---

# My Agent with Memory

我现在具备记忆能力...

记忆操作

// 存储记忆
await memory.store({
  type: 'episodic',
  content: '用户今天询问了 OpenClaw v2026.7 的升级方法',
  timestamp: Date.now(),
  tags: ['openclaw', 'upgrade', 'v2026.7']
});

// 检索记忆
const memories = await memory.search({
  query: 'OpenClaw 升级',
  type: 'episodic',
  limit: 5
});

// 更新记忆
await memory.update({
  id: 'mem_123',
  content: '用户今天询问了 OpenClaw v2026.7 的升级方法,并成功完成升级',
  additionalTags: ['success']
});

// 删除记忆
await memory.delete('mem_123');

E-E-A-T 原则与记忆

Google 的 E-E-A-T 原则(Experience, Expertise, Authoritativeness, Trustworthiness)同样适用于 AI Agent 的记忆设计:

E-E-A-T记忆应用实现方法
Experience(经验)记录 Agent 的实际操作经验情景记忆 + 操作日志
Expertise(专业)保存专业知识和最佳实践语义记忆 + 知识图谱
Authoritativeness(权威)引用权威来源和文档程序记忆 + 引用管理
Trustworthiness(可信)记录用户反馈和修正情景记忆 + 反馈循环

实战案例

案例 1:个性化客服 Agent

// 配置
customer_service:
  memory:
    provider: memory-core
    types: [episodic, semantic]
    
    # 记住用户偏好
    semantic:
      store:
        - user_preferences        # 用户偏好
        - product_interests       # 产品兴趣
        - issue_history           # 问题历史
    
    # 记住对话经历
    episodic:
      store:
        - conversation_summary    # 对话摘要
        - important_decisions     # 重要决策
        - follow_up_items         # 待跟进事项

# 使用示例
async function handleCustomerQuery(userId, query) {
  // 检索用户历史
  const userProfile = await memory.search({
    type: 'semantic',
    tags: [`user:${userId}`],
    query: 'preferences AND interests'
  });
  
  // 检索相关历史对话
  const pastConversations = await memory.search({
    type: 'episodic',
    tags: [`user:${userId}`],
    query: query
  });
  
  // 生成个性化回复
  const response = await generateResponse(query, userProfile, pastConversations);
  
  // 存储本次对话
  await memory.store({
    type: 'episodic',
    content: { query, response, timestamp: Date.now() },
    tags: [`user:${userId}`, 'conversation']
  });
  
  return response;
}

案例 2:自学习代码审查 Agent

// 配置
code_reviewer:
  memory:
    provider: memory-core
    types: [procedural, episodic]
    
    # 学习审查模式
    procedural:
      store:
        - code_patterns            # 代码模式
        - review_checklist        # 审查清单
        - common_issues          # 常见问题
    
    # 记录审查历史
    episodic:
      store:
        - review_history          # 审查历史
        - improvement_tracking    # 改进追踪

# 使用示例
async function reviewCode(code, filePath) {
  // 检索已知代码模式
  const patterns = await memory.search({
    type: 'procedural',
    query: `code pattern in ${filePath}`
  });
  
  // 执行审查
  const issues = await analyzeCode(code, patterns);
  
  // 如果发现新模式,存入程序记忆
  for (const issue of issues) {
    if (!patterns.find(p => p.id === issue.patternId)) {
      await memory.store({
        type: 'procedural',
        content: issue.pattern,
        tags: ['code_pattern', issue.category]
      });
    }
  }
  
  // 记录本次审查
  await memory.store({
    type: 'episodic',
    content: { filePath, issues, timestamp: Date.now() },
    tags: ['review', filePath]
  });
  
  return issues;
}

记忆管理最佳实践

  1. 分层存储:短期信息放 Context,长期信息放 Memory
  2. 定期清理:设置 TTL(过期时间),自动清理过时记忆
  3. 索引优化:为记忆添加合适的标签,便于检索
  4. 压缩摘要:定期将相关记忆压缩为摘要,节省空间
  5. 备份恢复:定期备份记忆数据,防止丢失
  6. 隐私保护:敏感信息加密存储,遵守隐私法规
✅ 最佳实践:结合 Self-Improving Agent 使用 Memory,让 Agent 真正具备持续学习能力。

常见问题

Q: Context Window 和 Memory 有什么区别?

A: Context Window 是短期的工作记忆,仅在一次对话中有效;Memory 是长期记忆,跨会话保存。两者配合使用效果最佳。

Q: 记忆会无限增长吗?

A: 可以配置 TTL(过期时间)自动清理,也可以定期手动清理旧记忆。建议为不同类型的记忆设置不同的 TTL。

Q: 如何保证记忆的准确性?

A: 1) 使用 Self-Improving Agent 验证记忆;2) 定期与用户确认重要记忆;3) 为记忆添加置信度评分;4) 实现记忆版本控制。

📚 推荐阅读

OpenClaw Agent Context Budget 管理 - 优化 Token 使用策略 | 妙趣AI
OpenClaw Agent Context Budget 完整管理指南:Token 预算分配、Context Window 优化、Context Expres
📂 tools | 🎯 相关度: 74%
OpenClaw 安全审计完整指南 - Skills/Agent/MCP 安全防护 | 妙趣AI
OpenClaw 安全审计指南,包含 Skills 安全扫描、MCP 权限控制、Agent 沙箱隔离、恶意 Skills 检测。
📂 tools | 🎯 相关度: 71%
OpenClaw & Agent Skills 术语百科 | 妙趣AI
用人类能听懂的大白话解释OpenClaw、Agent Skills、MCP、RAG等AI术语,有趣有梗有实战案例
📂 glossary | 🎯 相关度: 69%
AI术语百科 - OpenClaw & Agent Skills 完全指南 | 妙趣AI
AI术语百科,深入理解OpenClaw、Agent Skills、MCP、Tool Calling等核心概念。完全指南和实战应用。
📂 glossary | 🎯 相关度: 69%
OpenClaw Agent 记忆管理最佳实践 | 2026年最新 - 妙趣AI
OpenClaw Agent 记忆管理完全指南:学习短期记忆、长期记忆、MEMORY.md、记忆检索等最佳实践。
📂 tools | 🎯 相关度: 68%
Context Caching (上下文缓存) 详解 - OpenClaw & Agent Skills 术语百科
Context Caching(上下文缓存)详解:定义、原理、OpenClaw实战应用与代码示例,教你如何通过缓存减少token消耗降低AI Agent运行成本
📂 glossary | 🎯 相关度: 67%

📚 推荐阅读

这些文章可能对你有帮助

🛠️ MCP集成教程 📖 MCP术语详解 📖 MCP协议深入 🛠️ MCP无状态迁移 🛠️ 工具库 📖 术语百科