企业级 AI Chat / Agent 系统的“记忆系统设计”
AI Agent 的“记忆管理”本质是在解决一件事:让模型在有限上下文窗口之外,仍然能“持续理解用户与任务状态”。
Short-term Memory(短期记忆 / 会话记忆) 当前对话上下文
特点
存在于 prompt / message history
token 限制内
最频繁更新
存储方式
Redis / 内存 / DB(conversation table)
示例结构
{
"conversation_id": "xxx",
"messages": [
{"role": "user", "content": "帮我查知识库"},
{"role": "assistant", "content": "请确认是否查询"}
]
}
Long-term Memory(长期记忆 / 用户记忆) 跨会话“记住用户”
内容类型
用户偏好(技术栈、风格)
历史任务总结
重要事实(项目、身份)
行为习惯
存储方式(推荐)
PostgreSQL + pgvector(主流)
或 Milvus / Weaviate
{
"user_id": "u123",
"memory": "用户偏好 ",
"embedding": [0.12, 0.33, ...],
"type": "preference | fact | summary",
"score": 0.87,
"updated_at": "2026-07-05"
}
作用
personalization
长期上下文补全
提升 agent “像人”
Episodic Memory(事件记忆 / 工作流记忆) “发生过什么事”
特点
面向任务/流程
可回放
类似日志增强版
{
"session_id": "chat_001",
"event": "rag_query",
"input": "查询用户轨迹",
"output": "调用track_user tool",
"result": "success",
"timestamp": 1723456789
}
企业级“记忆系统架构”
┌──────────────┐
│ User Query │
└──────┬───────┘
↓
┌────────────────────┐
│ Memory Router LLM │
└────────────────────┘
┌────────────┬──────────────┬─────────────┐
↓ ↓ ↓ ↓
Short Memory Long Memory RAG Tools
↓ ↓ ↓ ↓
└────────────┴──────────────┴─────────────┘
↓
Prompt Assembly Engine
↓
LLM Output
↓
Memory Write Back System
LangGraph 中的实现方式(推荐结构)
[Input]
↓
[Retrieve Memory Node]
↓
[Retrieve RAG Node]
↓
[Tool Decision Node]
↓
[LLM Node]
↓
[Memory Write Node]
学而不思则罔,思而不学则殆!

浙公网安备 33010602011771号