Sliding Window Memory is a conversation memory strategy that keeps only the most recent N messages (or K interactions) in the buffer. When a new message arrives and the buffer exceeds the limit, the oldest message is automatically evicted — just like a window sliding forward over a stream of conversation.
CN: Sliding Window Memory(滑动窗口记忆)是一种对话记忆策略,它只在缓冲区中保留最近的 N 条消息(或 K 轮交互)。当新消息到达且缓冲区超过限制时,最旧的消息会被自动移除——就像一扇窗口在对话流上向前滑动。
What does Sliding Window Memory include?
Core Concepts
概念 (Concept)
EN
CN
Window Size (k)
The maximum number of interactions (or messages) to keep
保留的最大交互(或消息)数量
FIFO Eviction
First-In-First-Out: oldest messages are removed first
先进先出:最早的消息最先被移除
Buffer
The data structure that holds the recent messages
存放最近消息的数据结构
Trimming
The act of removing old messages when limit is exceeded
超过限制时移除旧消息的行为
Two Measurement Approaches
Count-based Window: Keep the last N messages (or K turns). Simple and predictable.
Token-based Window: Keep messages until a token budget is reached, then evict oldest.
CN:
基于数量的窗口: 保留最后 N 条消息(或 K 轮)。简单且可预测。
基于 Token 的窗口: 保留消息直到达到 token 预算,然后驱逐最旧的消息。
Key Caveats
System messages should NOT be trimmed: System prompts are foundational and should always be kept.
Tool call pairs must be preserved together: If you trim a tool result, you must also trim its corresponding tool call, otherwise you break the API contract.
Sliding window loses long-term context: It cannot answer questions like “Remember what I said 50 turns ago?”
CN:
System 消息不应该被裁剪: System prompt 是基础性的,应该始终保留。
Tool call 配对必须一起保留: 如果你裁剪了一个 tool result,也必须裁剪对应的 tool call,否则会违反 API 协议。
滑动窗口丢失长期上下文: 它无法回答诸如“还记得 50 轮前我说了什么吗?”这样的问题。
Key Takeaways
要点 (Key Point)
EN
CN
滑动窗口只保留最近 N 条消息
Sliding window keeps only the most recent N messages
Conversation State is the “notepad” that your AI agent carries throughout a conversation. It remembers what was said, what files were uploaded, what decisions were made, and where the conversation is heading.
CN: Conversation State 就是 AI Agent 在整个对话过程中随身携带的“记事本”。它记住了说过什么、上传了什么文件、做了什么决定、对话正在往哪个方向走。
Imagine you’re a waiter at a busy restaurant. You have a small notepad for each table. Table 3 ordered appetizers, Table 7 has a gluten allergy, Table 5 is celebrating a birthday. You don’t memorize everything—you write it down. That notepad is your “state.” When you come back to Table 3 later, you check your notepad to remember what they ordered and what they need next. LangGraph’s Conversation State is exactly that notepad—but for your AI agent.
CN (比喻): 想象你是一个繁忙餐厅的服务员。你为每一桌客人准备了一个小记事本。3号桌点了前菜,7号桌有麸质过敏,5号桌在过生日。你不会把一切都记在脑子里——你写下来。那个记事本就是你的“状态”。当你后来回到3号桌时,你查看记事本,记起他们点了什么、下一步需要什么。LangGraph 的 Conversation State 就是那个记事本——不过是给 AI Agent 用的。
Core Contents
3.1 State(状态)—— 数据容器
State is a shared data structure passed between nodes in a LangGraph. Every node reads the current state and returns updates (partial state). The graph accumulates these updates over time.
CN: State 是在 LangGraph 的节点之间传递的共享数据结构。每个节点读取当前状态并返回更新(部分状态)。图会随时间累积这些更新。
3.2 Reducers(归约器)—— 如何合并更新
EN: When multiple nodes return updates to the same state key, how should those updates be merged? Reducers define this logic. For example, operator.add appends new messages to the existing list rather than replacing it.
CN: 当多个节点对同一个 state key 返回更新时,这些更新应该如何合并?Reducer(归约器)定义了这种逻辑。例如,operator.add 将新消息追加到现有列表中,而不是替换它。
This is critical: without reducers, later nodes would overwrite previous state. With reducers, state accumulates intelligently.
EN: A thread is a unique conversation identifier (like a thread_id). Each thread maintains its own independent state. This allows your agent to handle thousands of concurrent conversations without mixing them up.
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 模型)并将其适配到软件工程,将记忆划分为四种不同类型。
Long-Term Memory (LTM) – Permanent storage of facts and preferences. 长期记忆(LTM) – 事实和偏好的永久存储。
Episodic Memory – Memory of specific past events and interactions. 情景记忆(Episodic Memory) – 对特定过去事件和交互的记忆。
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 Type
Human Analogy
AI Implementation
Persistence
Example
Short-Term
Sticky Note / Scratchpad
messages array, Context Window
Session only (Volatile)
Current chat conversation
Long-Term
Employee File / Hard Drive
PostgreSQL / Redis
Permanent (Years)
“I prefer Python 3.11”
Episodic
Personal Diary / Logs
Time-series DB / Vector DB
Permanent (Months/Years)
“Last week, you asked for X and got error Y.”
Semantic
Encyclopedia / Wiki
Vector DB / Graph DB
Permanent (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.