Self-Learning Agent — Knowledge Card Memory System
A production-tested memory architecture for AI agents that wake up fresh each session. Instead of one monolithic memory file that grows until it's unusable, this system uses atomic knowledge cards (~350 tokens each) searched semantically, daily logs for raw notes, and a slim master index loaded every session.
Architecture
CODEBLOCK0
Why This Works
- - MEMORY.md is tiny (~2KB). It loads fast, gives the agent orientation, and points to everything else.
- Knowledge cards are atomic. Each one covers ONE topic in ~350 tokens. Semantic search finds the right cards without loading everything.
- Daily logs are append-only scratch pads. Raw session notes, not curated.
- Cards are curated wisdom. Daily logs are raw data. The agent periodically distills daily logs into cards during maintenance.
Setup
1. Create the directory structure
CODEBLOCK1
2. Create MEMORY.md (master index)
This file is loaded every session. Keep it under 2KB. It should contain:
CODEBLOCK2
3. Add to your AGENTS.md / system prompt
CODEBLOCK3
Knowledge Card Format
Every card has YAML frontmatter and dense content:
CODEBLOCK4
Card Quality Rules
- 1. ONE topic per card. Three insights = three cards.
- ~350 tokens max. Dense beats verbose.
- Zero-context readable. Include specifics (commands, paths, values).
- Tags are searchable keywords. Lowercase, hyphenated.
- Update, don't duplicate. If a card exists for the topic, merge new info into it.
- No fluff. Every sentence should contain a fact, a command, or a decision.
Good Card Example
CODEBLOCK5
Bad Card Example
CODEBLOCK6
(Too vague, no specifics, no actionable info, multiple topics in one card)
Capture Triggers
Automatic (agent should capture without being asked)
- - Hard-won debugging lessons (3+ attempts to fix something)
- Configuration gotchas (things that work differently than expected)
- User corrections ("no, do it THIS way")
- Non-obvious facts about infrastructure, people, or projects
- Workflow improvements discovered during a task
Manual
- - User says
/learn, "remember this", or "save this" - User explicitly corrects the agent's approach
What NOT to Capture
- - Obvious/trivial information
- Temporary context (one-time fixes that won't recur)
- Things already in existing cards
- Conversation summaries (that's what daily logs are for)
Daily Log Format
Append to memory/YYYY-MM-DD.md:
CODEBLOCK7
Memory Maintenance
Periodically (every few days), the agent should:
- 1. Read recent daily logs
- Identify significant events worth preserving long-term
- Create or update knowledge cards from insights
- Remove outdated info from MEMORY.md
- Update the card categories table in MEMORY.md
Think of it like a human reviewing their journal and updating their mental model.
Promotion Rules
When the same lesson appears 3+ times in cards:
- - Promote it to AGENTS.md as a permanent rule
- Mark the original card as "promoted"
- This prevents the agent from re-learning the same lesson
Session Workflow
CODEBLOCK8
Scaling
This system has been tested with:
- - ~36 knowledge cards (~350 tokens each = ~12.6K tokens total)
- Daily logs spanning months
- Semantic search via embeddings (qwen3-embedding or similar)
At this scale, semantic search finds relevant cards in <100ms. The master index stays under 2KB. The agent loads only what it needs.
If you hit 100+ cards, consider:
- - Archiving cards older than 6 months that haven't been accessed
- Splitting categories into subdirectories
- Adding a card index file per category
Comparison with Monolithic Memory
| Monolithic (one big file) | Knowledge Cards |
|---|
| Load time | Grows forever | Constant (~2KB index) |
| Search |
Full-text scan | Semantic vector search |
| Updates | Append-only chaos | Atomic card updates |
| Noise ratio | High (old + new mixed) | Low (curated cards) |
| Session cost | Tokens scale with history | Tokens stay flat |
自学习代理 — 知识卡片记忆系统
一种经过生产验证的AI代理记忆架构,使代理每次会话都能以全新状态启动。该系统不使用单一且不断增长直至无法使用的记忆文件,而是采用原子化知识卡片(每张约350个token)进行语义搜索、使用日常日志记录原始笔记,以及每次会话加载的精简主索引。
架构
workspace/
├── MEMORY.md # 主索引(约2KB,每次会话加载)
├── memory/
│ ├── cards/ # 知识卡片(每张约350个token)
│ │ ├── topic-name.md # 每个文件一个主题,YAML前置元数据
│ │ ├── another-topic.md
│ │ └── ...
│ └── YYYY-MM-DD.md # 日常会话日志(原始笔记)
为何有效
- - MEMORY.md 体积小巧(约2KB)。加载速度快,为代理提供方向指引,并指向其他所有内容。
- 知识卡片 是原子化的。每张卡片用约350个token覆盖一个主题。语义搜索无需加载全部内容即可找到相关卡片。
- 日常日志 是仅追加的草稿本。记录原始会话笔记,不经过整理。
- 卡片是精炼的智慧,日常日志是原始数据。 代理在维护期间定期将日常日志提炼为知识卡片。
设置
1. 创建目录结构
bash
mkdir -p memory/cards
2. 创建 MEMORY.md(主索引)
该文件每次会话都会加载。保持其大小在2KB以下。应包含:
markdown
MEMORY.md — 主索引
记忆工作原理
- - 本文件: 精简索引(约2KB)。每次主会话加载。
- 知识卡片: memory/cards/*.md(约N张卡片,每张约350个token)。通过语义搜索查找。
- 日常日志: memory/YYYY-MM-DD.md。原始会话笔记。
- 不要将所有内容倾倒在此处。应编写知识卡片。
身份信息
[代理名称、模型、所有者、关键事实]
快速上下文
[2-3行当前重要事项]
卡片分类
[将类别映射到卡片主题的表格]
当前优先级
[正在积极处理的事项]
3. 添加到 AGENTS.md 或系统提示词
markdown
每次会话
- 1. 读取 MEMORY.md(精简索引)
- 使用 memory_search 搜索与当前任务相关的上下文
- 浏览今天和昨天的日常日志以获取近期上下文
- 开始工作
记忆规则
- - 心理笔记在会话重启后不会保留。文件才会。
- 当有人说记住这个时 → 编写一张知识卡片
- 当学到一条经验时 → 编写一张知识卡片
- 当犯了一个错误时 → 记录下来,以免未来的你重蹈覆辙
知识卡片格式
每张卡片都有YAML前置元数据和紧凑内容:
markdown
topic: 描述性主题名称
category: system|human|infrastructure|tools|workflow|projects|lessons|career|security|models
tags: [tag1, tag2, tag3]
created: YYYY-MM-DD
updated: YYYY-MM-DD
实际内容。紧凑、事实性、无废话。
为没有任何上下文的未来你而写。
包含具体命令、路径、配置值。
保持在350个token以下。
卡片质量标准
- 1. 每张卡片一个主题。 三个见解 = 三张卡片。
- 最多约350个token。 紧凑优于冗长。
- 零上下文可读。 包含具体信息(命令、路径、值)。
- 标签是可搜索的关键词。 小写,连字符连接。
- 更新,不重复。 如果该主题已有卡片,将新信息合并进去。
- 无废话。 每个句子都应包含一个事实、一条命令或一个决策。
好卡片示例
markdown
topic: Cortex CSRF自动化
category: infrastructure
tags: [cortex, csrf, thehive, api, security]
created: 2026-03-19
updated: 2026-03-19
Cortex 3.1.8使用非标准CSRF。Cookie: CORTEX-XSRF-TOKEN,请求头: X-CORTEX-XSRF-TOKEN。
标准的Play框架绕过请求头(Csrf-Token: nocheck)不起作用。
流程:登录 → 使用会话Cookie GET任意端点 → 从Set-Cookie中捕获CORTEX-XSRF-TOKEN →
在所有POST/PUT/DELETE请求中同时发送为Cookie和X-CORTEX-XSRF-TOKEN请求头。
捷径:生成第一个API密钥后,使用Authorization: Bearer即可完全绕过CSRF。
第一个用户的POST /api/user(无认证)仅在数据库中有零个用户时有效。
坏卡片示例
markdown
topic: 今天学到的东西
今天我学到了很多关于Cortex和TheHive的东西。CSRF的问题真的很棘手,
花了很长时间才弄清楚。我还学到了如何设置组织和用户。
总的来说,这是一次富有成效的会话。
(过于模糊,没有具体信息,没有可操作的内容,一张卡片包含多个主题)
捕获触发条件
自动(代理应无需提示自动捕获)
- - 来之不易的调试经验(尝试3次以上才修复的问题)
- 配置陷阱(与预期工作方式不同的事项)
- 用户纠正(不,应该这样做)
- 关于基础设施、人员或项目的非显而易见的事实
- 在任务中发现的工作流程改进
手动
- - 用户说 /learn、记住这个或保存这个
- 用户明确纠正代理的方法
不应捕获的内容
- - 显而易见/琐碎的信息
- 临时上下文(不会重复发生的一次性修复)
- 已有卡片中已包含的内容
- 对话摘要(那是日常日志的用途)
日常日志格式
追加到 memory/YYYY-MM-DD.md:
markdown
HH:MM — 简短标题
发生了什么,决定了什么,学到了什么。
链接到任何创建的卡片:→ card: topic-name
记忆维护
定期(每隔几天),代理应:
- 1. 读取近期的日常日志
- 识别值得长期保留的重要事件
- 从见解中创建或更新知识卡片
- 从MEMORY.md中移除过时信息
- 更新MEMORY.md中的卡片分类表
可以将其想象为人类回顾日记并更新自己的心智模型。
提升规则
当同一条经验在卡片中出现3次以上时:
- - 将其提升到AGENTS.md中作为永久规则
- 将原始卡片标记为已提升
- 这可以防止代理重新学习同一条经验
会话工作流程
会话开始
│
├── 读取 MEMORY.md(始终,约2KB)
├── 使用 memory_search 搜索任务相关卡片
├── 浏览今天和昨天的日常日志
│
├── [执行工作]
│
├── 捕获见解 → 知识卡片
├── 记录会话 → 日常日志
│
会话结束
扩展性
该系统已通过以下规模测试:
- - 约36张知识卡片(每张约350个token = 总计约12.6K token)
- 跨越数月的日常日志
- 通过嵌入向量进行语义搜索(qwen3-embedding或类似模型)
在此规模下,语义搜索在<100ms内找到相关卡片。主索引保持在2KB以下。代理只加载所需内容。
如果达到100张以上卡片,可考虑:
- - 归档超过6个月且未被访问的卡片
- 将类别拆分为子目录
- 为每个类别添加卡片索引文件
与单一文件记忆的对比
| 单一文件(一个大文件) | 知识卡片 |
|---|
| 加载时间 | 不断增长 | 恒定(约2KB索引) |
| 搜索 |
全文扫描 | 语义向量搜索 |
| 更新 | 仅追加的混乱 | 原子化卡片更新 |
| 噪声比 | 高(新旧混合) | 低(精炼卡片) |
| 会话成本 | token随历史增长 | token保持稳定 |