OpenClaw 记忆系统详解:让 AI 真正记住你
记忆是 AI 助手的核心能力。本文深入讲解 OpenClaw 的记忆系统,包括短期会话记忆、长期 MEMORY.md、每日记忆文件等。学会配置和管理记忆,让你的 AI 助手真正了解你。
一、记忆系统架构
1.1 记忆层次
┌─────────────────────────────────────┐
│ 长期记忆 (MEMORY.md) │
│ curated memories, decisions │
└─────────────────────────────────────┘
↑
┌─────────────────────────────────────┐
│ 每日记忆 (memory/YYYY-MM-DD.md) │
│ raw logs, events │
└─────────────────────────────────────┘
↑
┌─────────────────────────────────────┐
│ 会话记忆 (Session Context) │
│ current conversation history │
└─────────────────────────────────────┘
1.2 记忆类型对比
| 类型 | 位置 | 用途 | 生命周期 |
|---|---|---|---|
| 会话记忆 |
| 当前对话上下文 | 会话结束清除 | 每日记忆 | memory/YYYY-MM-DD.md | 原始事件日志 | 永久保存 | 长期记忆 | MEMORY.md |
| 精选重要信息 | 永久保存,手动维护 |
—
二、会话记忆配置
2.1 会话记忆限制
{
channels: {
telegram: {
dmHistoryLimit: 30, // DM 会话保留 30 条用户消息
historyLimit: 50 // 群组会话保留 50 条消息
},
whatsapp: {
dmHistoryLimit: 20
}
}
}
2.2 每用户覆盖
{
channels: {
telegram: {
dms: {
"123456789": { historyLimit: 100 }, // 特定用户更多历史
"987654321": { historyLimit: 10 } // 特定用户更少历史
}
}
}
}
三、每日记忆文件
3.1 自动创建
OpenClaw 自动创建每日记忆文件:
memory/2026-03-23.md
memory/2026-03-24.md
...
3.2 记忆内容
每日记忆文件包含:
- 重要对话摘要
- 完成的任务
- 学到的信息
- 待办事项
3.3 手动添加记忆
2026-03-23
重要事件
- 开始使用 OpenClaw
- 配置了 WhatsApp 通道
完成的任务
- 安装 OpenClaw
- 配置 Claude API
待办事项
- [ ] 配置 Telegram
- [ ] 设置多代理
四、长期记忆 (MEMORY.md)
4.1 MEMORY.md 结构
长期记忆
个人信息
- 姓名:张三
- 时区:Asia/Shanghai
- 职业:软件工程师
偏好
- 喜欢简洁的回答
- 偏好使用 Claude Sonnet
- 工作时间:9:00-18:00
项目
- 项目 A:使用 Python + FastAPI
- 项目 B:使用 React + TypeScript
重要决定
- 2026-03-23:选择 Claude 作为主要模型
- 2026-03-24:启用多代理系统
4.2 记忆维护
定期审查(建议每周):
查看最近记忆
cat memory/2026-03-*.md
更新 MEMORY.md
# 将重要信息从每日记忆迁移到长期记忆
五、记忆搜索
5.1 语义搜索
OpenClaw 使用语义搜索查找相关记忆:
用户:我之前说过喜欢什么模型?
AI:[搜索 MEMORY.md] 你偏好使用 Claude Sonnet
5.2 搜索配置
{
memory: {
search: {
enabled: true,
maxResults: 5,
minScore: 0.5
}
}
}
六、记忆最佳实践
6.1 写什么
✅ 应该记录:
- 个人偏好
- 重要决定
- 项目信息
- 学习心得
❌ 不应该记录:
- 敏感信息(密码、密钥)
- 临时信息
- 琐碎细节
6.2 维护频率
| 任务 | 频率 | 时间 | 查看每日记忆 |
| 更新长期记忆 | 每周 | 15 分钟 |
| 清理旧记忆 | 每月 | 30 分钟 |
七、总结
记忆系统配置清单:
- [ ] 配置会话历史限制
- [ ] 创建 MEMORY.md
- [ ] 建立每日记录习惯
- [ ] 定期维护长期记忆
关键要点:
- 会话记忆用于当前对话
- 每日记忆记录原始事件
- 长期记忆存储精选信息
- 定期维护保持记忆质量
下一篇预告:《OpenClaw 自动化与技能开发:扩展你的 AI 能力》
/* 基础样式 */
.article-container {
max-width: 100%;
margin: 0 auto;
padding: 15px;
font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, sans-serif;
line-height: 1.8;
color: #333;
background: #fff;
}
/* 文章头部 */
.article-header {
border-bottom: 3px solid #4a90e2;
padding-bottom: 20px;
margin-bottom: 25px;
}
.main-title {
font-size: 1.8em;
color: #1a1a1a;
margin-bottom: 12px;
font-weight: 700;
line-height: 1.3;
}
.article-meta {
display: flex;
gap: 12px;
flex-wrap: wrap;
align-items: center;
}
.category-badge {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
padding: 4px 12px;
border-radius: 16px;
font-size: 0.85em;
font-weight: 500;
}
.read-time {
color: #666;
font-size: 0.85em;
}
/* 导读框 */
.intro-box {
background: linear-gradient(135deg, #f0f4ff 0%, #e8f0fe 100%);
border-left: 4px solid #4a90e2;
padding: 18px;
margin: 20px 0;
border-radius: 0 8px 8px 0;
display: flex;
gap: 12px;
align-items: flex-start;
}
.intro-icon {
font-size: 1.8em;
flex-shrink: 0;
}
.intro-text {
margin: 0;
color: #444;
font-size: 1em;
line-height: 1.6;
}
/* 目录 */
.toc-container {
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 8px;
padding: 15px;
margin: 25px 0;
}
.toc-title {
font-size: 1.2em;
color: #333;
margin-bottom: 12px;
font-weight: 600;
}
.toc-list {
list-style: none;
padding: 0;
margin: 0;
}
.toc-list li {
margin: 6px 0;
}
.toc-list a {
color: #4a90e2;
text-decoration: none;
display: block;
padding: 6px 10px;
border-radius: 4px;
font-size: 0.95em;
transition: all 0.2s;
}
.toc-list a:hover {
background: #4a90e2;
color: #fff;
}
/* 文章内容 */
.article-content {
font-size: 1em;
}
.article-title {
font-size: 1.6em;
color: #1a1a1a;
margin: 35px 0 18px;
padding-bottom: 8px;
border-bottom: 2px solid #4a90e2;
}
.section-title {
font-size: 1.4em;
color: #2c3e50;
margin: 30px 0 12px;
padding-left: 12px;
border-left: 4px solid #4a90e2;
}
.subsection-title {
font-size: 1.2em;
color: #34495e;
margin: 22px 0 10px;
}
.subsubsection-title {
font-size: 1.05em;
color: #666;
margin: 18px 0 8px;
}
/* 代码块 – 支持横向滚动 */
.code-wrapper {
margin: 18px 0;
border-radius: 6px;
overflow: hidden;
}
.code-block {
background: #1e1e1e;
padding: 15px;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
margin: 0;
border-radius: 6px;
}
.code-block code {
color: #d4d4d4;
font-family: “Fira Code”, “Consolas”, “Monaco”, monospace;
font-size: 0.85em;
line-height: 1.6;
white-space: pre;
}
.inline-code {
background: #f5f5f5;
color: #e83e8c;
padding: 2px 6px;
border-radius: 3px;
font-family: “Fira Code”, “Consolas”, monospace;
font-size: 0.9em;
}
/* 表格 – 响应式滚动 */
.table-wrapper {
margin: 20px 0;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.styled-table {
width: 100%;
border-collapse: collapse;
min-width: 600px;
font-size: 0.9em;
}
.styled-table thead {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
}
.styled-table th {
padding: 12px 15px;
text-align: left;
font-weight: 600;
white-space: nowrap;
}
.styled-table td {
padding: 12px 15px;
border-bottom: 1px solid #e1e4e8;
vertical-align: top;
}
.styled-table tbody tr {
transition: background 0.2s;
}
.styled-table tbody tr:hover {
background: #f8f9fa;
}
.styled-table tbody tr:nth-child(even) {
background: #fafbfc;
}
.styled-table tbody tr:nth-child(odd) {
background: #fff;
}
/* 列表 */
.styled-list {
list-style: none;
padding: 0;
margin: 15px 0;
}
.list-item {
padding: 6px 0 6px 22px;
position: relative;
color: #333;
}
.list-item::before {
content: “•”;
position: absolute;
left: 0;
color: #4a90e2;
font-weight: bold;
font-size: 1.2em;
}
.checkbox-item {
padding: 6px 0 6px 32px;
position: relative;
color: #333;
}
.checkbox-item.checked {
color: #28a745;
}
/* 信息框 */
.info-box {
background: #e8f4fd;
border-left: 4px solid #4a90e2;
padding: 15px;
margin: 18px 0;
border-radius: 0 6px 6px 0;
color: #2c3e50;
}
/* 分隔线 */
.section-divider {
border: none;
height: 2px;
background: linear-gradient(to right, transparent, #4a90e2, transparent);
margin: 35px 0;
}
/* 文章尾部 */
.article-footer {
margin-top: 40px;
padding-top: 25px;
border-top: 2px solid #e9ecef;
}
.tags-section {
margin-bottom: 20px;
}
.tag-label {
font-weight: 600;
color: #666;
margin-right: 8px;
}
.tag {
display: inline-block;
background: #f0f4ff;
color: #4a90e2;
padding: 4px 10px;
border-radius: 12px;
margin: 3px;
font-size: 0.85em;
text-decoration: none;
transition: all 0.2s;
}
.tag:hover {
background: #4a90e2;
color: #fff;
}
.back-to-top {
text-align: center;
padding: 15px;
background: #f8f9fa;
border-radius: 6px;
}
.back-to-top a {
color: #4a90e2;
text-decoration: none;
font-weight: 500;
}
/* 移动端优化 */
@media (max-width: 768px) {
.article-container {
padding: 12px;
}
.main-title {
font-size: 1.4em;
}
.article-title {
font-size: 1.3em;
}
.section-title {
font-size: 1.2em;
}
.code-block {
padding: 12px;
font-size: 0.8em;
}
.styled-table {
font-size: 0.85em;
min-width: 500px;
}
.styled-table th,
.styled-table td {
padding: 10px 12px;
}
.intro-box {
padding: 15px;
}
.intro-icon {
font-size: 1.5em;
}
}
@media (max-width: 480px) {
.main-title {
font-size: 1.3em;
}
.article-meta {
flex-direction: column;
gap: 8px;
}
.category-badge,
.read-time {
font-size: 0.8em;
}
.code-block {
font-size: 0.75em;
padding: 10px;
}
.styled-table {
min-width: 450px;
font-size: 0.8em;
}
.styled-table th,
.styled-table td {
padding: 8px 10px;
}
}
// 移动端表格提示
document.addEventListener(‘DOMContentLoaded’, function() {
var tables = document.querySelectorAll(‘.table-wrapper’);
tables.forEach(function(table) {
if (!table.querySelector(‘.scroll-hint’)) {
var hint = document.createElement(‘div’);
hint.className = ‘scroll-hint’;
hint.innerHTML = ‘👈 左右滑动查看更多’;
hint.style.cssText = ‘text-align:center;padding:8px;background:#f8f9fa;color:#666;font-size:12px;border-radius:0 0 8px 8px;’;
table.parentNode.insertBefore(hint, table.nextSibling);
}
});
});