Chat Memory
Built and open-sourced by AI Advantage — the world's leading AI learning community.
We teach 40,000+ people how to actually use AI. This skill is how our own agents remember everything.
Want to learn more? Join us at aiadvantage.ai.
Solve the #1 problem with long-running AI agents: knowledge loss after context compaction.
The Problem
When sessions compact (summarize old messages to free context), specific details are lost:
names, decisions, file paths, reasoning. The agent retains a summary but loses the ability
to recall "What exactly did Annika say?" or "When did we decide to use v6 format?"
Most memory skills on ClawHub are just SKILL.md instructions — "write stuff to MEMORY.md."
That's not a solution. This skill ships real scripts that do real work.
The Solution: Three-Layer Memory Architecture
CODEBLOCK0
All three layers live under memory/ and are automatically vectorized by OpenClaw's
memory search, creating a navigational hierarchy: glossary finds the right session,
session provides the details.
Setup (run once)
Step 1: Convert existing sessions to Markdown
CODEBLOCK1
This scans all JSONL session logs in ~/.openclaw/agents/*/sessions/ and converts
them to memory/sessions/session-YYYY-MM-DD-HHMM-*.md. Truncates long assistant
responses to 2KB, skips system messages, tracks state to avoid re-processing.
Options:
- -
--new — Only convert sessions not yet processed (for incremental runs) - INLINECODE4 — Specify agent ID (default: main)
Step 2: Build the glossary
CODEBLOCK2
Scans all session transcripts and builds memory/SESSION-GLOSSAR.md with:
- - People — Who was mentioned, in how many sessions, date ranges
- Projects — Which projects discussed, with relevant topic tags
- Topics — Categorized themes (Email Drafts, Website Build, Security, etc.)
- Timeline — Per-day summary (session count, people, topics)
- Decisions — Extracted decision-like statements with dates
Options:
- -
--incremental — Only process new sessions (uses cached scan state)
Step 3: Set up cron jobs for auto-updates
Create two cron jobs (use a cheap model like Gemini Flash):
Job 1: Session sync + glossary rebuild (every 4-6 hours)
CODEBLOCK3
Optional Job 2: Pre-compaction memory flush check
Already built into AGENTS.md by default — just ensure the agent writes to
memory/YYYY-MM-DD.md before each compaction.
Customizing Entity Detection
Edit scripts/build-glossary.py to add your own known people and projects:
CODEBLOCK4
The glossary also detects topics via regex patterns. Add new patterns in the
topic_patterns dict for your domain.
How It Works With memory_search
Once set up, memory_search("Alice project decision") will find:
- 1. The glossary entry for Alice (which sessions she appears in)
- The actual session transcript where the decision was discussed
- Any MEMORY.md entry about Alice
This gives the agent a navigation layer (glossary) plus detail access
(transcripts) — much better than either alone.
File Structure After Setup
CODEBLOCK5
Cron Memory Optimizer
Cron jobs run in isolated sessions with zero memory context. The optimizer analyzes your cron jobs and suggests memory-enhanced versions:
CODEBLOCK6
This scans ~/.openclaw/cron/jobs.json, identifies jobs that would benefit from memory context, and generates memory/cron-optimization-report.md with before/after prompts and implementation guidance.
Example optimization:
CODEBLOCK7
The script is conservative (suggests only, never auto-modifies) and skips monitoring jobs that don't need context.
Sharing Memory Context with Cron Jobs, Subagents & Telegram Sessions
One of the biggest challenges in multi-session AI systems is context isolation. Here's how to share memory context across different execution environments:
For Cron Jobs
The problem: Cron jobs run in isolated sessions with zero memory context, making them blind to recent activities, people, and decisions.
The solution: Prepend a "memory preamble" to cron job prompts that instructs the agent to search memory before starting:
CODEBLOCK8
The cron-optimizer.py script analyzes your existing cron jobs and automatically suggests which ones would benefit from memory context. It generates a detailed report with before/after prompts.
Example transformation:
CODEBLOCK9
For Subagents (sessions_spawn)
The problem: Subagents start with empty context and don't know about recent activities or ongoing projects.
The solution: Include memory instructions in the task prompt when spawning subagents:
CODEBLOCK10
Tips:
- - Be specific with memory_search keywords for best results
- Include both recent (SESSION-GLOSSAR.md) and long-term (MEMORY.md) context
- Consider what the subagent needs to know to do its job effectively
For Telegram Group Sessions
The problem: Group sessions share the workspace but don't automatically know about the memory system or recent activities discussed in other sessions.
The solution: Two approaches depending on your setup:
Method 1: Push context via sessions_send
CODEBLOCK11
Method 2: Add memory awareness to AGENTS.md
Add guidance to your AGENTS.md so group sessions know to search memory:
CODEBLOCK12
Tips:
- - Group sessions can access the memory system if they know to use it
- Include memory search instructions in your group-specific agent guidelines
- Consider pushing critical updates from main to group sessions when decisions are made
For Knowledge Bases (Vectorized Databases)
If you have custom vectorized knowledge bases (e.g., using sentence-transformers), make them accessible across sessions:
Method 1: Query scripts
CODEBLOCK13
Method 2: Workspace storage
CODEBLOCK14
Method 3: Integration prompts
Include the script path in cron job and subagent prompts:
CODEBLOCK15
The key is making knowledge discovery automatic and standardized across all execution contexts — main session, cron jobs, subagents, and group sessions should all follow the same memory-aware patterns.
Tips
- - Run the full rebuild (
python3 scripts/build-glossary.py without --incremental)
occasionally to pick up improvements to entity detection
- - The glossary is most useful when KNOWNPEOPLE and KNOWNPROJECTS are populated —
spend 5 minutes adding your key contacts and projects
- - For agents that run 24/7, the cron job keeps everything current automatically
- Session transcripts can get large (our 297 sessions = 24MB) — this is fine,
OpenClaw's vector search handles it efficiently
- - Use the cron optimizer after setting up memory to enhance existing automation
Why This Exists
We run OpenClaw agents 24/7 for real work — client projects, research pipelines, content
production. After a week we had 300+ sessions and our agents kept forgetting critical
details after compaction. We built this to fix it, and it worked so well we open-sourced it.
What makes this different from other memory skills:
- - ✅ Real Python scripts — not just "instructions for the agent"
- ✅ Three-layer architecture — curated + auto-glossary + raw transcripts
- ✅ Cron automation — runs in the background, zero manual work
- ✅ Glossary with entity detection — people, projects, decisions, timeline
- ✅ Cron optimizer — makes your existing cron jobs context-aware
- ✅ Clean security score — no suspicious flags, no external dependencies
- ✅ Battle-tested — 300+ sessions, running in production daily
Built with 🔥 by AI Advantage — Join 40,000+ people learning to build with AI.
We don't just teach AI — we build with it every day. This skill is proof.
技能名称: chat-memory
详细描述:
聊天记忆
由 AI Advantage 构建并开源——全球领先的AI学习社区。
我们教授40,000+人如何真正使用AI。这项技能正是我们自己的智能体记住一切的方式。
想了解更多?加入我们:aiadvantage.ai
解决长期运行AI智能体的头号问题:上下文压缩后的知识丢失。
问题
当会话被压缩(总结旧消息以释放上下文)时,具体细节会丢失:
姓名、决策、文件路径、推理过程。智能体保留了摘要,但失去了回忆
Annika到底说了什么?或我们什么时候决定使用v6格式?的能力。
ClawHub上的大多数记忆技能只是SKILL.md指令——把内容写到MEMORY.md里。
这不是解决方案。这项技能提供了真正执行实际工作的真实脚本。
解决方案:三层记忆架构
第1层:MEMORY.md — 精选的长期记忆(人工编辑)
第2层:SESSION-GLOSSAR.md — 自动生成的结构化索引(人员/项目/决策/时间线)
第3层:memory/sessions/ — 完整的会话记录,作为可搜索的Markdown文件
所有三层都位于memory/目录下,并由OpenClaw的记忆搜索自动向量化,
形成一个导航层级:词汇表找到正确的会话,
会话提供详细信息。
设置(只需运行一次)
步骤1:将现有会话转换为Markdown格式
bash
python3 scripts/session-to-memory.py
这会扫描~/.openclaw/agents/*/sessions/中的所有JSONL会话日志,并将它们
转换为memory/sessions/session-YYYY-MM-DD-HHMM-*.md格式。将较长的助手
响应截断至2KB,跳过系统消息,跟踪状态以避免重复处理。
选项:
- - --new — 仅转换尚未处理的会话(用于增量运行)
- --agent main — 指定智能体ID(默认:main)
步骤2:构建词汇表
bash
python3 scripts/build-glossary.py
扫描所有会话记录并构建memory/SESSION-GLOSSAR.md,包含:
- - 人员 — 被提及的人,出现在多少会话中,日期范围
- 项目 — 讨论过的项目,附带相关主题标签
- 主题 — 分类主题(邮件草稿、网站构建、安全等)
- 时间线 — 每日摘要(会话数量、人员、主题)
- 决策 — 提取的类似决策的陈述及其日期
选项:
- - --incremental — 仅处理新会话(使用缓存的扫描状态)
步骤3:设置自动更新的定时任务
创建两个定时任务(使用廉价模型如Gemini Flash):
任务1:会话同步 + 词汇表重建(每4-6小时)
任务:先运行 python3 scripts/session-to-memory.py --new,然后
运行 python3 scripts/build-glossary.py --incremental。
报告有多少新会话被转换和索引。
可选任务2:压缩前记忆刷新检查
默认已内置于AGENTS.md中——只需确保智能体在每次压缩前写入
memory/YYYY-MM-DD.md。
自定义实体检测
编辑scripts/build-glossary.py以添加您自己已知的人员和项目:
python
KNOWN_PEOPLE = {
alice: Alice Smith — 项目经理,
bob: Bob Jones — 首席技术官,
}
KNOWN_PROJECTS = {
website-redesign: 网站重新设计 — 第一季度计划,
api-migration: API迁移 — v2到v3,
}
词汇表还通过正则表达式模式检测主题。在topic_patterns字典中为您的领域添加新模式。
与memory_search的配合方式
设置完成后,memory_search(Alice 项目 决策)将找到:
- 1. Alice的词汇表条目(她出现在哪些会话中)
- 讨论该决策的实际会话记录
- 任何关于Alice的MEMORY.md条目
这为智能体提供了导航层(词汇表)加上细节访问
(记录)——比单独使用任何一种都要好得多。
设置后的文件结构
memory/
├── MEMORY.md — 精选内容(您维护)
├── SESSION-GLOSSAR.md — 自动生成的索引
├── YYYY-MM-DD.md — 每日笔记
├── .glossary-state.json — 词汇表构建器状态
├── .glossary-scans.json — 缓存的扫描结果
└── sessions/
├── .state.json — 转换器状态
├── session-2026-01-15-0830-abc123.md
├── session-2026-01-15-1200-def456.md
└── ...
定时任务记忆优化器
定时任务在隔离的会话中运行,没有记忆上下文。优化器分析您的定时任务并建议增强记忆的版本:
bash
python3 scripts/cron-optimizer.py
这会扫描~/.openclaw/cron/jobs.json,识别能从记忆上下文中受益的任务,并生成memory/cron-optimization-report.md,包含优化前后的提示和实施指南。
优化示例:
原始:运行每日研究侦察...
增强:开始前:使用memory_search查找关于研究活动的最新上下文。检查memory/SESSION-GLOSSAR.md中相关的人员、项目和近期决策。然后使用此上下文继续执行原始任务。
运行每日研究侦察...
该脚本是保守的(仅建议,从不自动修改),并跳过不需要上下文的监控任务。
与定时任务、子智能体和Telegram会话共享记忆上下文
多会话AI系统最大的挑战之一是上下文隔离。以下是如何在不同执行环境之间共享记忆上下文:
对于定时任务
问题: 定时任务在隔离的会话中运行,没有记忆上下文,使它们对最近的活动、人员和决策一无所知。
解决方案: 在定时任务提示前添加一个记忆前言,指示智能体在开始前搜索记忆:
开始此任务前:使用memory_search查找与此任务相关的最新上下文。检查memory/SESSION-GLOSSAR.md中可能相关的人员、项目和近期决策。然后使用此上下文继续执行原始任务。
cron-optimizer.py脚本分析您现有的定时任务,并自动建议哪些任务能从记忆上下文中受益。它会生成一份详细的报告,包含优化前后的提示。
转换示例:
之前:你是一个研究侦察员。查找AI工具并报告发现...
之后:开始此任务前:使用memory_search查找与此任务相关的最新上下文。检查memory/SESSION-GLOSSAR.md中可能相关的人员、项目和近期决策。然后使用此上下文继续执行原始任务。
你是一个研究侦察员。查找AI工具并报告发现...
对于子智能体(sessions_spawn)
问题: 子智能体以空上下文启动,不了解最近的活动或正在进行的项目。
解决方案: 在生成子智能体时,在任务提示中包含记忆指令:
开始前:使用memory_search(相关关键词)查找最新上下文。
检查memory/SESSION-GLOSSAR.md中的人员、项目、决策。
检查MEMORY.md中的长期上下文。
然后继续:
[您的实际任务...]
提示:
- - 为获得最佳结果,memory_search关键词要具体
- 同时包含近期(SESSION-GLOSSAR.md)和长期(MEMORY.md)上下文
- 考虑子智能体需要知道什么才能有效完成工作
对于Telegram群组会话
问题: 群组会话共享工作空间,但不会自动了解记忆系统或其他会话中讨论的近期活动。
解决方案: 根据您的设置有两种方法:
方法1:通过sessions_send推送上下文
bash
从主会话向群组会话发送相关上下文
sessions_send telegram-group 记忆上下文:近期项目状态 - [摘要]
方法2:在AGENTS.md中添加记忆意识
在您的AGENTS.md中添加指导,以便群组会话知道搜索记忆:
markdown
群聊指南
当回答关于过去工作或正在进行的项目的问题时,
始终首先使用memory_search检查相关上下文。
提示:
- - 如果群组会话知道如何使用记忆系统,它们可以访问它
- 在您的群组特定智能体指南中包含记忆搜索指令
- 考虑在做出决策时,将关键更新从主会话推送到群组会话
对于知识库(向量化数据库)
如果您有自定义的向量化知识库(例如,使用sentence-transformers),使其跨会话可访问:
方法1:查询脚本
bash
创建一个任何会话都可以调用的查询脚本
python3 scripts/query-knowledge-base.py 搜索词
方法2:工作空间存储
bash
将数据库存储在工作空间中以便通用访问
mkdir -p knowledge-base/
在任务提示中包含数据库路径
使用位于./knowledge-base/d