memory-system

memory-system is a skill for Claude Code, Codex from bojieli/ai-agent-book. It costs 95 tokens per session (3,495 once invoked), scanned A, original, Apache-2.0.

A guide to giving an AI agent persistent, user-specific memory across conversations. It covers what to remember, how to store it, how to update or compress it, and when to use memory instead of a shared knowledge base.

In plain words
What is it for?
Use it to design memory records, separate short-term activity from long-term user facts, evaluate recall across sessions, resolve conflicts, remove sensitive details, and plan periodic cleanup.
Why use it?
It helps an agent retain useful preferences and facts without storing every message or mixing one user's information with general knowledge. It also addresses stale, conflicting, oversized, or privacy-sensitive memories.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it to design memory records, separate short-term activity from long-term user facts, evaluate recall across sessions, resolve conflicts, remove sensitive details, and plan periodic cleanup.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/bojieli/ai-agent-book/memory-system
About the project

AI Agent: Design Principles and Engineering Practice is an open-source book that explains how AI agents combine language models, context, and tools, with accompanying experiments and code. It is intended for readers studying the principles and engineering of AI agents, from fundamentals through production use. The catalogue skills support coding-agent work related to the book's subject matter.

bojieli/ai-agent-book · 52,744 stars · on GitHub

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add bojieli/ai-agent-book --skill memory-system
Clone the repo
git clone --depth 1 https://github.com/bojieli/ai-agent-book

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for memory-system

README.md
[![agentmods](https://agentmods.dev/badge/skills/bojieli/ai-agent-book/memory-system/github.svg)](https://agentmods.dev/skills/bojieli/ai-agent-book/memory-system)
Your own site
<a href="https://agentmods.dev/skills/bojieli/ai-agent-book/memory-system"><img src="https://agentmods.dev/badge/skills/bojieli/ai-agent-book/memory-system/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for memory-system

Your own site · 80×15
<a href="https://agentmods.dev/skills/bojieli/ai-agent-book/memory-system"><img src="https://agentmods.dev/badge/skills/bojieli/ai-agent-book/memory-system.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 95 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,495 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00095 $0.03495
Opus 5.5 $0.00038 $0.01398
Sonnet 5.5 $0.00019 $0.00699
Haiku 4.5 $0.00010 $0.00349

Measured 14d ago against content hash e24528543087, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

memory-system scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 14d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/memory-system/SKILL.md · 81 lines

How it starts

The opening of the file, as written. The whole thing — 81 lines — stays where its author put it; the contents beside it link to each section on GitHub.

用户记忆系统

何时使用

  • 为 Agent 设计跨会话的用户记忆(记住偏好、身份、历史交互)
  • 选型记忆存储格式(Simple Notes / Enhanced Notes / JSON Cards / Advanced JSON Cards / 可执行代码)
  • 评估记忆系统好坏(对照三层次框架或 LoCoMo 基准)
  • 集成、改造或对比 Mem0、Memobase 等记忆框架
  • 处理记忆冲突、过期、膨胀、压缩与隐私脱敏
  • 判断某类信息该进用户记忆还是共享知识库
  • 设计记忆的压缩与定期整理策略

核心原则

  • 记忆与 RAG 的边界:单个用户的个性化信息(偏好、身份、关系、历史)走用户记忆;面向所有用户共享的集体知识(行业法规、公司流程、专业文档)走 RAG 知识库。两者尺度不同但底层技术相通(向量检索、知识压缩),也面临同样的麻烦:信息冲突、知识过期、检索不准。用户记忆同样可以反过来用 RAG 技术增强召回。

  • 记忆不是逐句存档:会话结束后用一次专门的 LLM 调用提取,提取结果须同时满足三条规则——选择性(丢弃"搜索返回 3 个选项"这类短期细节)、抽象化(把本次"靠窗座位"归纳为长期偏好)、结构化(用可检索字段保存事实)。

  • 先立评估标尺再设计。记忆能力可归纳为八项:个人信息保留、偏好追踪、上下文切换、记忆更新、多会话连续性、复杂思考、时间感知、冲突解决。工程上按三层次验收:

    • L1 基础回忆:精确存取用户直接给出的结构化事实("我的会员号是 12345")。
    • L2 多会话检索:跨对象、跨时期找全相关信息并主动澄清(用户有两辆车时问"为哪辆预约",而不是猜一辆)。
    • L3 主动服务:综合很久以前的记忆给出预见性帮助(订国际航班时关联数月前存储的护照信息并预警临期)。
    • 公开基准可参考 LoCoMo(平均约 300 轮、最多 35 个会话的超长多轮对话)。
  • 三个正交维度分开决策,勿混淆:放哪里(轨迹 / 用户长期记忆 / 业务状态)、怎么存(四种格式)、存什么(情景 / 语义 / 程序记忆)。三者可自由组合。

  • 轨迹(Trajectory)是单次运行的 append-only 原始流水,供追溯调试审计;运行时上下文可对它压缩重组。用户长期记忆是跨会话持久化、与用户 ID 绑定的档案,会被反复改写、合并、淘汰。前者是流水账,后者是档案。

  • 认知类型指导"存什么":情景记忆记具体事件(订了哪班航班),语义记忆存提炼出的稳定特征(用户是素食者),程序记忆学行为流程(先搜直飞→确认座位→用常旅客号)。

  • 选格式看工程需求(简单性与表达力的取舍),选类型看业务场景(需要记住事实、事件还是流程)——两套体系正交,分别决策。

  • 格式选型按关键性与数量:

    • Simple Notes:最小不可分事实,开销最低、O(1) 操作,但关联性丢失,综合查询需重新拼凑碎片。
    • Enhanced Notes:完整上下文段落,语义丰富,但存储冗余、更新需重写多个段落。
    • JSON Cards:类别→子类别→键值对三层嵌套,支持部分更新、可预测可扩展,但强制单一归类会丢失多维性("周末用 Python 开发个人项目"同时是时间/技术/活动偏好)。
    • Advanced JSON Cards:事实之外加 backstory(为何存储)、person、relationship(为谁存储)和时间戳,解决同名实体消歧(用户自己的牙科张医生 vs 父亲的心脏科张医生)。
    • 经验法则:关键且少量(偏好、关键人物关系)用 Advanced JSON Cards;大量且非关键的对话事实用 Simple Notes;生产系统多为混合模式,不同类别走不同路径。

    四种格式速查:

    格式 结构 优势 代价
    Simple Notes 最小不可分事实 开销极低、O(1) 操作 关联性丢失,综合查询需重新拼凑
    Enhanced Notes 完整上下文段落 叙事结构、语义丰富 存储冗余、属性变化需重写多段
    JSON Cards 类别→子类别→键值对 部分更新、可预测可扩展 刚性归类丢失多维性
    Advanced JSON Cards 事实 + backstory/person/relationship/时间戳 同名实体消歧、多身份区分 生成与维护成本高
  • 需要聚合统计、冲突检测、约束执行等确定性推理时,把记忆升级为可执行代码(User as Code):事实先进只增日志,再定期重建带类型状态;规则写成普通函数(如国际行程出发前护照有效期不足 180 天即告警),让"表示"和"推理"共用可验证介质。

  • 记忆库不是越大越好。三层压缩:重要性评分筛选(访问频率、时间衰减、情感强度、信息独特性四因素综合,低于阈值标记为可压缩或可删除)→ 相似记忆聚类生成代表性摘要(多次天气对话压成"用户经常询问天气,特别关心降雨",原始细节存档二级存储)→ 从情景记忆抽象泛化为语义/程序记忆(从多次购物对话学到"偏好性价比高的产品,重视用户评价")。

  • 业务状态("需要澄清"/"处理请求中"/"等待付款")是开发者定义的高层任务阶段抽象,与轨迹、长期记忆并列,在事件驱动架构中尤为重要;多数系统先实现轨迹 + 长期记忆两层即可。

Read the full file on GitHub · 81 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 14d ago First seen · 81 lines · 95 tokens per session scan A e24528543087

Subscribe to this mod's changes

memory-system is a skill published in the GitHub repository bojieli/ai-agent-book (52,744 stars, last pushed today), licensed Apache-2.0. It adds 95 tokens to every session and 3,495 once invoked, about $0.0004 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-24.