Memory Skills 详解 + E-E-A-T 原则 + 长期/短期/情景/语义记忆实战
人类的记忆分为短期记忆(工作记忆)和长期记忆,AI Agent 也需要类似的记忆系统才能:
容量:取决于模型的 Context Window(如 GPT-5.6 Sol 的 256K tokens)
持久性:仅在一次对话/任务中有效
用途:当前任务的推理、工具调用结果、中间步骤
管理策略:滑动窗口、摘要压缩、智能截断
容量:几乎无限(取决于存储)
持久性:跨会话、跨任务保持
存储后端:
# 配置 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 页面的标准流程" |
用途:正在执行的任务状态、循环变量、条件判断结果
实现:通常存储在 Agent 的运行环境中(内存)
// Agent 工作记忆示例
const workingMemory = {
currentTask: 'generate-seo-pages',
progress: 3, // 已完成 3 页
total: 10, // 总共 10 页
lastResult: {...}, // 上一页的生成结果
errors: [] // 错误记录
};
# 安装
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');
Google 的 E-E-A-T 原则(Experience, Expertise, Authoritativeness, Trustworthiness)同样适用于 AI Agent 的记忆设计:
| E-E-A-T | 记忆应用 | 实现方法 |
|---|---|---|
| Experience(经验) | 记录 Agent 的实际操作经验 | 情景记忆 + 操作日志 |
| Expertise(专业) | 保存专业知识和最佳实践 | 语义记忆 + 知识图谱 |
| Authoritativeness(权威) | 引用权威来源和文档 | 程序记忆 + 引用管理 |
| Trustworthiness(可信) | 记录用户反馈和修正 | 情景记忆 + 反馈循环 |
// 配置
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;
}
// 配置
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;
}
A: Context Window 是短期的工作记忆,仅在一次对话中有效;Memory 是长期记忆,跨会话保存。两者配合使用效果最佳。
A: 可以配置 TTL(过期时间)自动清理,也可以定期手动清理旧记忆。建议为不同类型的记忆设置不同的 TTL。
A: 1) 使用 Self-Improving Agent 验证记忆;2) 定期与用户确认重要记忆;3) 为记忆添加置信度评分;4) 实现记忆版本控制。