AI Agent Memory Architecture
The Core Problem
AI agents face a fundamental challenge: they have no persistent memory between sessions. The raw context window is finite and noisy — by the end of a long conversation, early information is diluted or lost. Building reliable long-term memory for agents is one of the most important unsolved problems in AI engineering.
Why Memory is Hard
Context Is Not Equal
LLM context windows are not uniform in weight. Information at the beginning of the context gets the most “attention” during generation. As the conversation grows, early messages become increasingly diluted — similar to how human working memory loses details over time.
The fix: Structured memory systems that surface important information at the right time, regardless of when it was added.
Session Boundaries Break Continuity
Each new conversation starts from scratch. Without memory management:
- The agent forgets everything from previous sessions
- Each session wastes time re-establishing context
- No learning from past mistakes or successes
The LLM Wiki Memory Pattern
This vault uses the LLM Wiki pattern (inspired by Andrej Karpathy and the community):
Key insight: Instead of retrieval-augmented generation (RAG) — where the agent searches external documents — keep the wiki as a compiled, cross-referenced artifact that the agent reads and maintains directly.
Three layers:
- Raw Sources (
raw/) — Immutable originals - Wiki (
wiki/) — LLM-maintained synthesized pages with cross-references - Schema (
CLAUDE.md,log.md) — Configuration and tracking
This pattern solves memory by: compiling knowledge into coherent pages, maintaining cross-references, and updating incrementally.
OpenClaw’s Five-Layer Memory System
OpenClaw implements the most sophisticated production memory architecture seen in AI agents:
| Layer | Storage | Timescale | Management |
|---|---|---|---|
| L1 身份层 | SOUL.md | Permanent | Human confirms changes |
| L2 长期记忆 | MEMORY.md (<3K tokens) | Long-term | Agent maintains |
| L3 中期记忆 | memory/YYYY-MM-DD.md | Medium-term | Harness auto-extracts |
| L4 短期记忆 | .learnings/ (ERRORS/LEARNINGS) | Short-term | Agent immediate notes |
| L5 持久化 | Skills + Obsidian + vector_store.db | Permanent | Shared/archived |
L1 — SOUL.md (Identity/Core)
The “constitution” of the agent. Contains:
- Core identity and purpose
- Non-negotiable values
- Key relationships and context
- Cannot be modified by agent without human approval
L2 — MEMORY.md (Long-term)
< 3,000 tokens hard limit. Contains:
- Cross-session learning
- Important project context
- Preferences and patterns
- Agent maintains autonomously
L3 — Date-stamped memory files
Medium-term extraction from sessions:
- Harness automatically extracts key decisions
- Organized by date
- Periodic compaction to L2
L4 — .learnings/ (Immediate feedback)
- ERRORS/: What went wrong in this session
- LEARNINGS/: What was discovered
- Pending → reviewed → promoted to L2
L5 — Skills + External Tools
- OpenClaw Skills persist expertise
- Obsidian integration for knowledge management
- SQLite vector database for semantic search
Claude-Mem — Persistent Context for Claude Code
Claude-Mem is a tool specifically designed to give Claude Code persistent memory:
- Automatic session summaries: After each session, extract key decisions and context
- Cross-session search: Ask questions about past sessions
- Context injection: On new sessions, relevant memories are injected automatically
Key insight: Memory is most valuable when it surfaces at the right moment, not just when searched.
Harness Engineering for Memory
Memory management is a core component of Harness Engineering:
Context Engineering
Designing what information enters the context and in what order:
- SOUL.md first (constitution, heaviest weight)
- AGENTS.md second (operational rules)
- Skills via extraDirs (loaded on demand)
- shared-context/ for cross-agent state
Compaction
When context approaches capacity:
- Soft compaction: Extract summaries, discard verbose tool outputs
- Hard compaction: Agent generates a coherent narrative summary
Memory Flush
{
"compaction": {
"mode": "safeguard",
"memoryFlush": { "softThresholdTokens": 40000 }
},
"contextPruning": { "mode": "cache-ttl", "ttl": "6h" }
}The Agent Self-Improvement Loop
Advanced agents implement a closed-loop learning system:
1. Agent makes error or discovers better approach
2. Immediately records to .learnings/ (pending)
3. Daily review cron (23:00-23:45) checks pending items
4. If ≥3 occurrences of same pattern → promote to MEMORY.md
5. Next session: self-improving-agent hook injects the learning
6. Agent no longer repeats the same mistake
This is the closest thing to “learning from experience” that current AI systems achieve.
Qoder Memory — 解决”上下文焦虑”
来自高德团队在 Qoder 平台上的实践(../sources/2026-05-07-告别氛围编程-Harness治理-SDD-团队级AI研发范式):
Qoder 的 Memory 机制是解决 AI “上下文焦虑”的核心能力。在长周期项目开发中,AI 需要记住大量信息:之前的决策、当前的进度、待办的事项等。Memory 提供了结构化方式来存储和管理这些信息。
三层知识库架构:
- 项目层:项目概述、目录结构、架构设计、技术选型 —— AI 理解项目上下文的基础
- 技术层:编码规范、中间件文档、最佳实践 —— 可跨项目复用的团队技术沉淀
- 资产层:可复用代码片段、组件模板、历史 PRD、归档测试 Case —— 团队多年积累的”砖块”
按需加载机制:顶层 README.md 作为”单一事实来源”和索引,AI 通过索引按需加载对应知识,避免上下文过载。原则:“如果某个信息不在文档里,对 AI 来说它就不存在。”
Memory 在 SDD 流程中的作用:Spec 模式中 AI 主动提问、逐步补齐完整 Spec,Memory 确保 AI 能在正确的上下文中做出正确的决策,而不是每次都从零开始。
[2026-07-20] 记忆悖论:完备性 ↔ 可用性的根本张力
来源:AI Agent 记忆-推理-进化三重悖论万字综述(Datawhale,管秉涛,2026-07-17)。
本页此前侧重”如何记得住”(五层系统、Dreaming、Claude-Mem);本综述补上反直觉的一面——记住越多反而越糊涂:信息的完备性与可用性根本互斥。
- 核心难点不在存储,而在选择:Amazon Bedrock AgentCore Memory(“学会忘记”为核心哲学)与 OpenAI ChatGPT Dreaming(对话间歇离线整合,计算开销压缩约 5 倍)共同验证此判断。本主题既有的 OpenClaw Auto-Dream 正是同一思路的开源实现。
- 记忆是控制问题,不是配置问题(MemCon):固定 top-k 检索是错的,最优策略高度上下文依赖(任务初期最小化检索避噪 / 重复任务策略复用 / 卡壳时查询重构 / 长期运行需剪枝)。MemCon 把每个记忆操作建模为 MDP、用 UCB bandit 控制器自适应选择 → 六任务三模型成功率最多 +15.2pp、token 处理量 −5–20%。“不存在一劳永逸的记忆策略,记忆操作本身需要推理来驱动。”
- 幽灵记忆(ghost memory,A-TMA):过时旧事实 / 过渡记录 / 当前新事实共存并被同时召回,模型无法区分时间有效性(“住纽约”/“搬到伦敦”/“住伦敦”三条同现 → 时间混淆)。A-TMA 保留过期+过渡数据、构建时间证据包、为 QA 附时间标签 → 矛盾问题准确率提升近 6 倍。记忆准确性不仅取决于”记了什么”,还取决于”什么时候记的”——纯检索架构无法覆盖此维度。
- 安全税两难:反幻觉提示引发过度保守拒答(答案在眼前也拒答)。不防幻觉 → 幽灵记忆致错;防幻觉过度 → 正确记忆被抑制。
- 结论:成熟记忆系统不是静态存储,而是动态信息调度器——每次推理前主动判断”此刻需要什么粒度/时间范围/抽象层次的信息”。记忆与推理是紧耦合循环(需推理来决定如何记忆,又需记忆来支撑推理)。
对接:记忆作为”控制问题”与本主题既有的 Context Engineering / Compaction 一脉相承,但把决策权从”手工规则”交给”推理驱动的自适应控制器”。另见 AI Agent 自我改进 中记忆-推理-进化的循环依赖。
新增证据(2026-08-06 批次:评测日报 08-03 / 08-05)
- AgentMemBench(arXiv 2608.00009,五种记忆策略横评,491 个标注问答轮次):LoCoMo 长程 Recall@5 上,上下文窗口 ICW / 网络增强 WAM / 图式情景 GEM / 压缩摘要 CBS 全部 ≤0.005,只有外部 KV 稠密检索 EKV 达 0.573——“近因窗口、摘要、实体图在长时程上直接坍塌,只有稠密检索能 scale”(代价:内存约 5,100 token vs 约 300)。对图记忆/摘要压缩两条流行路线是明确负面证据(当 hypothesis 用:样本 491 轮、判分模型仅 Qwen2.5-7B)。来源:评测日报 08-05。
- Meta Proactive Memory Agent(行为消融口径):“behavioral state decay” 命名;Claude Sonnet 4.5(action)+ Claude Opus 4.6(memory)使 Terminal-Bench 2.0 38%→46%、tau2-Bench 55%→62%;消融显示”每步全量注入”低于完整系统、“保持沉默”是策略组成部分;蒸馏到 Qwen3.5-27B 未训练版本反而降性能,需 SFT+RL 恢复;优于 Mem0。来源:评测日报 08-03。
互补结论:AgentMemBench 证明长程上”检索方法 > 窗口/摘要/图”;Meta 证明”注入时机与沉默策略 > 全量注入”。
新增证据(2026-08-10 批次:PAST-Bench 方法学)
- PAST-Bench(arXiv 2608.04003,来源:评测日报 08-09):以”开/关经验留存”受控对照测个人 agent 记忆价值(26 场景 204 episode × 7 基座 × 4 框架)。方法学贡献:把”增益幅度”与”增益是否走了 save→retrieve→update 路径”分离报告——表面增益相同的 agent,路径证据可能完全不同。与 ContextWeave 的”召回经验是否提升下游表现”是同一问题意识;今后记忆评测应同时交增益数字与路径证据。
⚠️ 矛盾 [2026-08-10] FinEvo-Bench “只进化 skill 优于 memory + skill 结合”(见 Agent 自我改进)对”记忆总是增益”的默认假设构成质疑——但注意 FinEvo 测的是任务表现进化、PAST-Bench 测的是个人 agent 记忆价值,两者口径不同,结论尚不能直接互推。
Related Pages
- OpenClaw — Production memory implementation
- Skills & MCP — Skills as persistent memory
- Harness Engineering — Harness controls memory
- Knowledge Management AI — LLM Wiki pattern
- AI Agent 自我改进 — 记忆/推理/进化的循环依赖
Sources: OpenClaw Auto-Dream, Claude-Mem, Agent Memory Tools Comparison, Memory in Practice
Related Sources
- 深度解析 Hermes Agent 如何实现“自进化”及其 Prompt - Context - Harness …
- 「纯干货」几万字都讲不明白的Memory架构与思考-2026-04-07
- OpenClaw Auto-Dream:让 AI Agent 像人类一样-睡觉-整理记忆-2026-04-02
- Context 还不够,Harness 才是 Agent 工程优化的正解?-2026-03-22
- 再见OpenClaw,memU Bot 接入飞书后,我的摸鱼看起来像加班-2026-02-06
- CodeGenius Memory:构建面向代码生成的可控上下文系统-2026-01-12
- AgentScope AutoContextMemory:告别Agent上下文焦虑-2025-12-26
- Claude-Mem:让 Claude Code 拥有持久记忆的神器-2025-12-15
- Claude-Mem:让 Claude 像人一样记住我说过的话-2025-12-04
- 云栖大会 - 菜鸟SRE Agent的Context Engineering实践-2025-09-26
- 国产Nano Banana来了!字节Seedream4.0发布,这次要让PS提前退休-2025-09-11
- 从 Prompt 到 Context:基于 1400+ 论文的 Context Engineering 系统综述-…
- OpenMemory MCP:让AI工具记住你的「专属上下文」,隐私与效率兼得的本地记忆管家-2025-08-04
- 实测-Qwen3 Coder+Code Context,开源平替Cursor的方案来了!-2025-07-26
- Context Engineering for Agents-2025-07-03
- factor-03-own-your-context-window-2025-07-03
- Karpathy-Context-Engineering
- Agent不长记性咋整?试试G-Memory,可进化的有组织“集体大脑”-2025-06-24