凌晨4点,我翻看着一个Agent的对话历史——3000轮交互,12万token的记忆。它记得3个月前用户说过"我喜欢简洁的回答",却忘了昨天刚答应要写的报告。
这就是Agent记忆架构的核心矛盾:记太多会溢出,记太少会失忆。
凌晨4点,我翻看着一个Agent的对话历史——3000轮交互,12万token的记忆。它记得3个月前用户说过"我喜欢简洁的回答",却忘了昨天刚答应要写的报告。
这就是Agent记忆架构的核心矛盾:记太多会溢出,记太少会失忆。
Agent Memory Architecture(Agent记忆架构)是AI Agent管理信息存储、检索和遗忘的系统设计。它决定了Agent能记住什么、忘掉什么、以及如何在正确的时间调出正确的记忆。
类比人类记忆系统:你不会记住今天早餐吃了什么(短期记忆会遗忘),但你会记住初恋的名字(长期记忆强化)。Agent的记忆架构也是这个逻辑。
类似人类的"当前意识",存放正在进行的任务上下文。
# OpenClaw 工作记忆配置示例
session:
context_window: 32768 # 32K tokens
system_prompt: |
你是一个代码助手。记住用户的偏好:
- 语言:Python
- 风格:简洁
- 注释:中文
memory_policy:
working_memory_max: 8192 # 工作记忆上限
auto_summarize: true # 超限时自动摘要
类似人类的"近期记忆",存放最近的交互历史和临时信息。
# OpenClaw 短期记忆管理
from openclaw.memory import ShortTermMemory
stm = ShortTermMemory(
max_entries=500,
ttl_hours=48, # 48小时过期
summarize_threshold=100 # 超过100条自动摘要
)
# 存储对话
stm.add(role="user", content="帮我写一个Python脚本")
stm.add(role="assistant", content="好的,这是脚本...")
# 检索相关记忆
memories = stm.search("Python脚本", top_k=5)
类似人类的"永久记忆",存放用户偏好、历史知识和重要事件。
# OpenClaw 长期记忆 - MEMORY.md 示例 # 用户偏好 - 姓名:诗中 - 时区:Asia/Shanghai - 沟通风格:直接、简洁 - 技术栈:Python, TypeScript, Go # 项目记忆 - 网站:miaoquai.com(AI工具导航) - 部署:Nginx + Ubuntu - 数据库:暂无(静态站) # 交互历史摘要 - 2026-06-15:讨论了Agent记忆架构优化 - 2026-06-10:完成了SEO巡检自动化
2026年最火的记忆技术是PRefine——将Agent记忆压缩到原始token数的1.24%。这意味着:
PRefine的核心原理是"提炼"而非"截断"——它不是简单地删掉旧记忆,而是将多轮对话提炼成结构化的知识图谱。
# PRefine 工作原理示意
原始对话(100K tokens):
用户: 我想做一个AI工具导航网站
Agent: 好的,你想用什么技术栈?
用户: Next.js + Tailwind
Agent: 建议用App Router...
用户: 好的,还需要SEO优化
Agent: 推荐这些SEO策略...
... (数百轮对话)
PRefine 提炼后(1.2K tokens):
{
"project": "AI工具导航网站",
"tech_stack": ["Next.js", "Tailwind CSS", "App Router"],
"features": ["SEO优化", "工具分类", "搜索功能"],
"preferences": ["简洁风格", "中文内容"],
"decisions": [
{"choice": "App Router", "reason": "SEO友好"},
{"choice": "静态生成", "reason": "性能优化"}
]
}
# openclaw.yaml 记忆配置
memory:
working_memory:
max_tokens: 8192
auto_compress: true
compress_strategy: "smart_summary"
short_term:
storage: "sqlite"
max_entries: 1000
ttl_hours: 72
index_fields: ["topic", "entities", "intent"]
long_term:
storage: "vector_db"
provider: "chroma"
embedding_model: "text-embedding-3-small"
similarity_threshold: 0.75
consolidation:
enabled: true
interval_hours: 6
strategy: "importance_weighted"
# OpenClaw 记忆检索API
from openclaw import OpenClawAgent
agent = OpenClawAgent(name="miaoquai")
# 自动检索相关记忆
response = agent.chat(
"上次我们讨论的SEO优化方案是什么?",
memory_context=True # 自动注入相关记忆
)
# 手动检索长期记忆
memories = agent.memory.search(
query="SEO优化",
memory_type="long_term",
top_k=10,
min_relevance=0.8
)
# 写入新的长期记忆
agent.memory.remember(
content="用户偏好简洁的代码风格",
importance=0.9,
category="preferences"
)
| 原则 | 说明 | OpenClaw实践 |
|---|---|---|
| 分层存储 | 不同生命周期的信息用不同存储 | session → history → MEMORY.md |
| 按需检索 | 不是全量加载,而是语义检索 | vector search + relevance filter |
| 主动遗忘 | 过期信息自动清理 | TTL + importance decay |
| 记忆整合 | 碎片记忆定期合并为知识 | consolidation job (每6小时) |
| 隐私保护 | 敏感信息加密存储 | PII detection + encryption |