Semantic Memory

Semantic Memory is the part of an AI agent’s long-term memory that stores facts, concepts, and general knowledge about the world, the user, and the domain it operates in. It’s the agent’s “second brain” — a persistent, searchable repository of what the agent has learned across all conversations and sessions.

CN:
语义记忆 (Semantic Memory) 是 AI Agent 长期记忆的一部分,用于存储关于世界、用户及其操作领域的事实、概念和通用知识。它是 Agent 的”第二大脑”——一个持久化、可搜索的仓库,存储 Agent 在所有对话和会话中学到的东西。

Think of Semantic Memory as a personal wiki that an AI agent builds and maintains for itself. Every time the agent learns something new — a user preference, a business rule, a technical decision — it writes a page in this wiki. Later, when the agent faces a new situation, it can search this wiki by meaning, not just by keywords, to recall relevant knowledge.

CN:
把语义记忆想象成 AI Agent 为自己建立和维护的个人维基百科。每次 Agent 学到新东西——用户偏好、业务规则、技术决策——它就在这个维基里写一页。以后当 Agent 面对新情况时,它可以通过含义(而不仅仅是关键词)搜索这个维基,回忆相关知识。

3. What’s Inside Semantic Memory

3.1 Semantic Search

Unlike keyword search (exact word matching), semantic search retrieves memories by meaning similarity using vector embeddings. A query for “UI preferences” can surface a memory written as “user prefers dark interfaces”.

CN:
与关键词搜索(精确词匹配)不同,语义检索通过向量嵌入含义相似度检索记忆。查询”UI 偏好”可以召回一条写为”用户喜欢暗色界面”的记忆

3.2 CRUD + Search

操作ENCN
StoreSave a fact/memory with its vector embedding保存事实/记忆及其向量嵌入
SearchFind relevant memories by semantic similarity按语义相似度查找相关记忆
GetRetrieve a specific memory by ID按 ID 获取特定记忆
UpdateModify an existing memory (with versioning)修改已有记忆(带版本控制)
DeleteRemove or deprecate outdated memories删除或废弃过时的记忆

3.3 Knowledge Organization

Memories are typically organized using:

  • Namespaces (like folders): e.g., (user_id, "preferences")(team_id, "decisions")
  • Collections (like categories): e.g., codebaseresearchgotchas
  • Keypath hierarchies: e.g., users.alice.preferences.language

CN:
记忆通常通过以下方式组织:

  • 命名空间(类似文件夹):如 (user_id, "preferences")(team_id, "decisions")
  • 集合(类似分类):如 codebaseresearchgotchas
  • 键路径层级:如 users.alice.preferences.language

3.4 Advanced Features

Production semantic memory systems often include:

  • Hybrid retrieval: combining vector search + keyword (BM25) search
  • Decay & forgetting: memories lose relevance over time unless validated
  • Consolidation: merging duplicate or related memories
  • Contradiction detection: identifying conflicting facts
  • Token-budget search: fitting results into LLM context limits

CN:
生产级语义记忆系统通常包含:

  • 混合检索:向量搜索 + 关键词(BM25)搜索结合
  • 衰减与遗忘:记忆随时间失去相关性,除非被验证
  • 整合:合并重复或相关的记忆
  • 矛盾检测:识别冲突的事实
  • Token预算搜索:将结果适配到 LLM 上下文限制内

Key Takeaways

要点ENCN
语义记忆存储事实、概念和通用知识Semantic Memory stores facts, concepts, and general knowledge语义记忆存储事实、概念和通用知识
语义检索按含义而非关键词查找Semantic search finds by meaning, not keywords语义检索按含义而非关键词查找
嵌入是语义检索的基础Embeddings are the foundation of semantic search嵌入是语义检索的基础
命名空间提供记忆的逻辑组织Namespaces provide logical organization of memories命名空间提供记忆的逻辑组织
生产系统需要混合检索 + 衰减 + 整合Production systems need hybrid retrieval + decay + consolidation生产系统需要混合检索 + 衰减 + 整合
记忆使 Agent 从无状态变为有状态Memory transforms agents from stateless to stateful记忆使 Agent 从无状态变为有状态
ChromaDB 适合本地开发和轻量级场景ChromaDB is suitable for local dev and lightweight scenariosChromaDB 适合本地开发和轻量级场景
PostgreSQL + pgvector 是生产级选择PostgreSQL + pgvector is a production-grade choicePostgreSQL + pgvector 是生产级选择

Type of Memory System

Think of an AI Agent as a new employee at your data company. Without memory, this employee lives in a state of permanent “Groundhog Day.” Every time you speak to him, it’s his first day on the job. He doesn’t remember the SQL query you asked for 5 minutes ago, doesn’t remember your preference for partitioned tables, and certainly doesn’t remember the mistake he made last week.

What is “Memory” in an AI Agent?

In the context of AI and Large Language Models (LLMs), a Memory System is not a single component (like RAM in a computer). Instead, it is a multi-layered architectural framework designed to store, retrieve, and synthesize information across different timescales.

Memory is what turns a stateless API caller into a true, stateful agent. It enables continuity, personalization, and learning over time. Without it, every interaction is isolated; with it, the agent becomes a colleague that grows with you.

记忆是把一个“无状态 API 调用者”变成真正的“有状态 Agent”的关键。它实现了连续性、个性化和长期学习。没有记忆,每次交互都是孤立的;有了记忆,Agent 就变成了一位与你一同成长的同事。

 In cognitive architectures for LLMs, we classify memory into four distinct types, borrowing heavily from cognitive psychology (Atkinson-Shiffrin model) but adapted for software engineering.
CN: 在 LLM 的认知架构中,我们借鉴认知心理学(Atkinson-Shiffrin 模型)并将其适配到软件工程,将记忆划分为四种不同类型。

  1. Short-Term Memory (STM) – The immediate context window.
    短期记忆(STM) – 即时的上下文窗口。
  2. Long-Term Memory (LTM) – Permanent storage of facts and preferences.
    长期记忆(LTM) – 事实和偏好的永久存储。
  3. Episodic Memory – Memory of specific past events and interactions.
    情景记忆(Episodic Memory) – 对特定过去事件和交互的记忆。
  4. Semantic Memory – Memory of general world knowledge and structured concepts.
    语义记忆(Semantic Memory) – 对通用世界知识和结构化概念的记忆。

1. Short-Term Memory (STM) / Working Memory

This is the “scratchpad” of the agent. It holds whatever the LLM is currently processing. In practical terms, this is the Context Window combined with the current conversation turn.
CN: 这是 Agent 的“草稿纸”。它保存 LLM 当前正在处理的内容。在实际中,这就是 Context Window(上下文窗口) 加上当前的对话轮次。

Volatile, limited capacity (e.g., 128k tokens), and fast. It’s managed by the system prompt and the chat history.

易失性、容量有限(例如 128k tokens)、速度快。它由 System Prompt 和聊天历史管理。

Example: In a chat, the agent remembers your name because you just told it 2 messages ago. It remembers the user_id variable you passed in the current API call.
CN: 例子: 在聊天中,Agent 记得你的名字,因为你刚在 2 条消息前告诉过它。它记得你在当前 API 调用中传递的 user_id 变量。

2. Long-Term Memory (LTM)

This is the “hard drive” of the agent. It stores facts, user preferences, and configurations that persist across sessions (days, weeks, or months).
CN: 这是 Agent 的“硬盘”。它存储跨会话(数天、数周或数月)持久化的事实、用户偏好和配置。

 Characteristics: Persistent, high capacity, slower to access (requires a database query), and durable.
CN: 特点: 持久性、高容量、访问速度较慢(需要数据库查询)、耐用。

Example: The agent remembers that you always prefer parquet over csv, or that your company’s production database is postgres://prod-db:5432. This is stored in a PostgreSQL table keyed by your user_id.
CN: 例子: Agent 记得你总是更喜欢 parquet 而不是 csv,或者你公司的生产数据库是 postgres://prod-db:5432。这存储在按 user_id 索引的 PostgreSQL 表中。

3. Episodic Memory

This is the “diary” or “logbook” of the agent. It records specific, time-stamped past interactions, tasks completed, successes, and failures. It answers the question: “What happened previously?”
CN: 这是 Agent 的“日记”或“日志”。它记录具体的、带时间戳的过去交互、已完成的任务、成功和失败。它回答:“之前发生了什么?”

Characteristics: Chronological, specific, and context-rich. Used for error analysis, auditing, and experience reuse.
CN: 特点: 按时间顺序排列、具体、上下文丰富。用于错误分析、审计和经验复用。

 Example: “On July 15th at 3 PM, you asked me to generate a sales report, but I failed because the table sales_2023 was missing.” The agent recalls this specific failure to avoid repeating it or to suggest a fix.
CN: 例子: “在 7 月 15 日下午 3 点,你让我生成一份销售报告,但因为表 sales_2023 不存在,我失败了。” Agent 回忆起这个具体的失败,以避免重蹈覆辙或建议修复方案。

4. Semantic Memory

This is the “encyclopedia” of the agent. It stores general, factual, and conceptual knowledge about the world and your specific domain (like your company’s business logic), decoupled from specific experiences.
CN: 这是 Agent 的“百科全书”。它存储关于世界和你特定领域(如你公司的业务逻辑)的通用、事实性和概念性知识,与具体经历脱钩。

Characteristics: Factual, abstract, and structured. This is typically your RAG Knowledge Base.
CN: 特点: 事实性、抽象、结构化。这通常就是你的 RAG 知识库

Example: “The sales table is joined with customers on customer_id.” This is a fact about your data model. It is true regardless of who asked, or when.
CN: 例子: “sales 表通过 customer_id 与 customers 表关联。” 这是关于你数据模型的一个事实。无论谁问,或者什么时候问,它都是真的。

Summary Table of the Four Types

Memory TypeHuman AnalogyAI ImplementationPersistenceExample
Short-TermSticky Note / Scratchpadmessages array, Context WindowSession only (Volatile)Current chat conversation
Long-TermEmployee File / Hard DrivePostgreSQL / RedisPermanent (Years)“I prefer Python 3.11”
EpisodicPersonal Diary / LogsTime-series DB / Vector DBPermanent (Months/Years)“Last week, you asked for X and got error Y.”
SemanticEncyclopedia / WikiVector DB / Graph DBPermanent (Updated via RAG)“The Primary Key of orders is order_id.”

Key Takeaways Table

要点 (EN)要点 (CN)
Memory turns a stateless LLM into a stateful Agent.记忆将无状态 LLM 变为有状态 Agent。
STM is the current context window; manage it via sliding windows.短期记忆是当前上下文窗口;通过滑动窗口管理它。
LTM stores user preferences (key-value) in databases like PostgreSQL.长期记忆在 PostgreSQL 等数据库中存储用户偏好(键值对)。
Episodic is a chronological log of actions; use it for audit and learning.情景记忆是行动的时间顺序日志;用于审计和学习。
Semantic is general domain knowledge; typically powered by Vector DBs (RAG).语义记忆是通用领域知识;通常由向量数据库驱动(RAG)。
The build_system_context() method is where you integrate all four types for the LLM.build_system_context() 方法是整合所有四种记忆给 LLM 的地方。
In production, STM is ephemeral, LTM/Episodic/Semantic are persistent.在生产中,短期记忆是临时的,长期/情景/语义记忆是持久的。