🧠 OpenClaw 记忆持久化方案

📅 2026年5月19日 | 🏷️ Memory 专题 | ⏱️ 阅读时间:18分钟

让 Agent 拥有"永久记忆"——向量数据库、分层存储、自动提升,一个都不能少。

🎬 为什么 Agent 需要"永久记忆"?

凌晨1点47分,Agent 问我:"上次我们聊到哪了?" 我愣住了——它忘了3天前我们花2小时讨论的 Agent 架构方案。

就像王家卫电影《东邪西毒》里的欧阳锋——每天都有人找他,但没人记得昨天说过什么。Agent 也需要"醉生梦死"的解药——记忆持久化。

🔍 三种记忆类型

类型 存储位置 保留时间 用途
短期记忆 Session Context 本次对话 当前任务上下文
中期记忆 本地向量库 30-90天 近期会话摘要
长期记忆 持久化存储 永久 知识库、用户偏好

🏗️ 架构设计:三层记忆系统

// OpenClaw 记忆系统架构
┌─────────────────────────────────┐
│     短期记忆 (Session)            │
│  - 当前对话历史 (最近100条)      │
│  - 临时变量 (task context)      │
│  - 工具调用结果缓存              │
└──────────┬──────────────────────┘
           │ (自动压缩)
           ↓
┌─────────────────────────────────┐
│     中期记忆 (Working Memory)     │
│  - 会话摘要 (向量存储)           │
│  - 任务结果归档                 │
│  - 用户交互历史 (30天)          │
└──────────┬──────────────────────┘
           │ (自动提升)
           ↓
┌─────────────────────────────────┐
│     长期记忆 (Long-term Memory)   │
│  - 知识库 (RAG)                │
│  - 用户画像                    │
│  - 系统配置                    │
└─────────────────────────────────┘

📦 实现方案1:向量数据库(推荐)

配置 SQLite-VSS(本地向量库)

# 安装依赖
npm install sqlite-vss vector-ext

# 配置 OpenClaw
{
    "memory": {
        "type": "vector-store",
        "provider": "sqlite-vss",
        "path": "~/.openclaw/memory.db",
        "dimensions": 1536,  // OpenAI embedding 维度
        "embeddings": {
            "provider": "openai",
            "model": "text-embedding-3-small",
            "apiKey": "sk-..."
        },
        "index": {
            "capacity": 10000,
            "similarity": "cosine"
        }
    }
}

# 初始化向量库
openclaw memory init

基本操作

# 保存记忆
openclaw memory save \
  --key="user_pref_language" \
  --value="中文" \
  --type="preference"

# 搜索记忆
openclaw memory search "用户喜欢什么语言"

# 输出:
# ✓ 找到 3 条相关记忆
# 1. [偏好] 用户偏好语言:中文 (相似度: 0.92)
# 2. [历史] 2026-05-15: 用户要求用中文回复 (相似度: 0.85)
# 3. [配置] 系统语言设置为 zh-CN (相似度: 0.78)

# 删除记忆
openclaw memory forget --key="temp_data"

# 导出记忆
openclaw memory export --format=json > backup.json
💡 最佳实践: 对重要记忆设置 "pinned: true",避免被自动清理。

📦 实现方案2:分层存储(RAG)

// 配置 RAG 系统
{
    "rag": {
        "enabled": true,
        "knowledgeBase": {
            "path": "~/.openclaw/knowledge/",
            "formats": ["markdown", "pdf", "txt"],
            "chunkSize": 1000,
            "chunkOverlap": 200
        },
        "retrieval": {
            "topK": 5,
            "threshold": 0.7,
            "reranker": "cohere"  // 可选重排序
        },
        "embedding": {
            "provider": "openai",
            "model": "text-embedding-3-small"
        }
    }
}

// 添加知识库文档
openclaw rag add --file="./docs/api-reference.md"
openclaw rag add --dir="./knowledge-base/"

// Agent 自动检索
用户:"OpenClaw 支持哪些 MCP 工具?"
→ Agent 自动检索 knowledge-base/
→ 找到相关文档 chunk
→ 结合上下文回复

🔄 自动提升机制(Promotion)

不是所有记忆都值得永久保存。OpenClaw 会根据"重要性"自动提升记忆层级。

// 配置自动提升
{
    "memory": {
        "promotion": {
            "enabled": true,
            "rules": [
                {
                    "condition": "access_count > 5",
                    "action": "promote_to_longterm"
                },
                {
                    "condition": "type == 'user_preference'",
                    "action": "pin_forever"
                },
                {
                    "condition": "age > 30 days AND access_count == 0",
                    "action": "archive_or_delete"
                }
            ],
            "reviewThreshold": 2,  // 被访问2次后提示是否提升
            "autoPin": ["user_id", "api_keys", "important_facts"]
        }
    }
}

// 手动提升
openclaw memory promote --key="project_2026_plan" --reason="重要战略文档"

// 查看提升候选
openclaw memory candidates

// 输出:
// 以下记忆被访问多次,建议提升:
// 1. user_coding_style (访问8次)
// 2. miaoquai_traffic_stats (访问6次)
// 3. competitor_analysis_2026 (访问5次)

🎨 实战案例:miaoquai.com 的记忆系统

📊 实际数据(运营50天)

  • 短期记忆:每次对话平均 45 条消息
  • 中期记忆:30天会话摘要 120+ 条
  • 长期记忆:知识库 1842 页内容索引
  • 自动提升:15 条记忆被提升为长期记忆
  • 检索命中率:92%(RAG 系统)

案例:跨会话任务恢复

# 场景:用户昨天让我分析竞品,今天继续
用户:"继续昨天的竞品分析"

# Agent 自动检索
→ 短期记忆:空(新会话)
→ 中期记忆:找到昨天的会话摘要
  "2026-05-18: 分析了 ClawHub、futuretools.io、easywithai.com"
→ 长期记忆:找到相关知识库条目
  "竞品监控方法论"、"SEO 评估框架"

# Agent 回复:
"好的,昨天我们分析了3个竞品:
1. ClawHub: 66,500+ Skills,增长速度400%
2. futuretools.io: SEO 评分 85,内容质量高
3. easywithai.com: 用户交互性强

今天我继续分析剩下的2个竞品,还是先生成报告?"

# 用户:"先生成报告"
→ Agent 从记忆中提取昨天的数据,结合今天的补充,生成完整报告

📊 性能优化

# 1. 向量检索优化
{
    "memory": {
        "index": {
            "type": "hnsw",           // 高效近似检索
            "efConstruction": 200,    // 构建质量
            "efSearch": 50,           // 检索质量
            "M": 16                    // 图连接数
        }
    }
}

# 2. 缓存热门记忆
{
    "memory": {
        "cache": {
            "enabled": true,
            "maxSize": "512MB",
            "ttl": 3600,              // 1小时过期
            "hotKeys": ["user_profile", "system_config"]
        }
    }
}

# 3. 异步写入
{
    "memory": {
        "write": {
            "async": true,
            "batchSize": 100,
            "flushInterval": 5000      // 5秒刷盘
        }
    }
}
💡 性能数据: 优化后,向量检索速度从 500ms 降至 50ms,命中率维持在 95%+。

🌟 总结

3分37秒,我决定了要让 Agent 拥有"人类般"的记忆——不是简单的键值对,而是有层次、有温度、会成长的记忆系统。

你已经学会了:

记住:好的记忆系统不是"记住所有事",而是"记住该记住的事"。