本文档整理自 Letta 官方文档、研究论文及 GitHub 仓库
原项目:MemGPT → 现名 Letta
论文:arXiv:2310.08560
📌 项目概览
什么是 MemGPT/Letta?
MemGPT(Memory-GPT)是一个创新的 LLM 记忆管理系统,现更名为 Letta。它由 UC Berkeley 的研究团队开发,旨在解决大语言模型的上下文窗口限制问题。
核心理念:
“Teaching LLMs to manage their own memory for unbounded context”
让 LLM 学会管理自己的记忆,实现无限上下文
GitHub 数据:
- ⭐ 21.2k stars
- 🍴 2.2k forks
- 👥 158 位贡献者
🧠 核心问题:上下文窗口限制
现有 LLM 的痛点
有限上下文窗口
- GPT-4: 128K tokens
- Claude: 200K tokens
- 长文档、多轮对话容易溢出
无法持久化记忆
- 每次对话都是"从头开始"
- 无法记住用户偏好、历史交互
无法进行长期学习
- 不能从交互中积累知识
- 无法自我改进
🎯 解决方案:虚拟上下文管理
核心创新:操作系统启发
MemGPT 借鉴了传统操作系统的虚拟内存机制:
| 操作系统 | MemGPT |
|---|---|
| 物理内存 (有限) | LLM上下文窗口 (有限) |
| 磁盘存储 (无限) | 外部存储 (无限) |
| 分页交换 | 智能内存交换 |
分层内存架构
Letta 分层内存系统:
Main Context (主上下文)
- 系统提示词 (System Prompt)
- 核心记忆块 (Core Memory Blocks)
- 当前对话历史 (Recent Messages)
- 工具调用结果
- 受限于 LLM 上下文窗口
External Memory (外部存储)
- 归档消息 (Archived Messages)
- 事实数据库 (Facts DB)
- 用户画像 (User Profiles)
- 学习到的知识
- 持久化存储,无限容量
内存管理工具 (Memory Tools)
core_memory_append: 追加核心记忆core_memory_replace: 替换核心记忆archival_memory_search: 搜索归档archival_memory_insert: 插入归档
🔧 技术架构详解
1. Stateful Agent(状态化智能体)
Letta Agent 的组成部分:
Stateful Agent = {
system_prompt: "系统提示词",
memory_blocks: [ # 记忆块
{label: "human", value: "用户信息"},
{label: "persona", value: "角色设定"},
{label: "facts", value: "事实知识"}
],
messages: [ # 消息历史
# 包含用户消息、助手回复、工具调用
],
tools: [ # 可用工具
"web_search",
"memory_management",
"file_operations"
]
}
记忆块(Memory Blocks)特点:
- 可编辑:Agent 可以通过工具修改自己的记忆
- 可共享:同一块记忆可以附加到多个 Agent
- 可固定:重要记忆常驻上下文窗口
- 可持久:所有状态存储在数据库中
2. 内存管理工具
核心记忆管理:
core_memory_append(label, content): 向核心记忆块追加内容core_memory_replace(label, new_content): 替换核心记忆块内容
归档记忆管理:
archival_memory_search(query, page): 搜索归档记忆archival_memory_insert(content): 插入到归档记忆
对话历史管理:
conversation_search(query, page): 搜索历史对话
3. 分页策略
# 简化的分页管理逻辑
class MemGPTManager:
def __init__(self):
self.warning_threshold = 0.7 # 70%警告
self.flush_threshold = 1.0 # 100%强制换出
def check_memory_pressure(self, context_usage):
if context_usage > self.flush_threshold:
self.evict_oldest()
elif context_usage > self.warning_threshold:
self.summarize_old_messages()
def evict_oldest(self):
# FIFO驱逐最旧消息
old_messages = self.fifo_queue.dequeue()
summary = self.summarize(old_messages)
self.archival_memory.store(summary)
🚀 实际应用场景
场景1:超长文档分析
传统方式:
- 文档长度: 500K tokens
- LLM 限制: 128K tokens
- ❌ 无法一次性处理
MemGPT 方式:
- 文档分块存储在外部记忆
- LLM 按需检索相关段落
- ✅ 可以处理无限长文档
场景2:多会话持久化对话
传统聊天机器人:
用户: 我叫张三
Agent: 你好张三!
--- 新会话 ---
用户: 我叫什么?
Agent: 我不知道
MemGPT 智能体:
用户: 我叫张三
Agent: [调用 core_memory_append("human", "Name: 张三")]
Agent: 你好张三!
--- 新会话 ---
用户: 我叫什么?
Agent: [检索 core_memory]
Agent: 你叫张三!
场景3:持续学习与自我改进
持续学习循环:
- 用户交互 → 提取洞察 → 更新记忆
- 应用知识 ← 积累知识 ← 提供更好响应
💻 代码示例
API 使用示例(Python)
from letta_client import Letta
import os
# 初始化客户端
client = Letta(api_key=os.getenv("LETTA_API_KEY"))
# 创建带记忆的 Agent
agent_state = client.agents.create(
model="openai/gpt-4o",
memory_blocks=[
{
"label": "human",
"value": "Name: Robert. Occupation: Software Engineer"
},
{
"label": "persona",
"value": "I am a helpful AI assistant with memory capabilities."
}
],
tools=["web_search", "fetch_webpage", "memory_management"]
)
print(f"Agent created with ID: {agent_state.id}")
# 发送消息
response = client.agents.messages.create(
agent_id=agent_state.id,
input="What do you know about me?"
)
for message in response.messages:
print(message)
API 使用示例(TypeScript)
import Letta from "@letta-ai/letta-client";
const client = new Letta({
apiKey: process.env.LETTA_API_KEY
});
// 创建 Agent
const agentState = await client.agents.create({
model: "openai/gpt-4o",
memory_blocks: [
{
label: "human",
value: "Name: Robert. Occupation: Software Engineer"
},
{
label: "persona",
value: "I am a self-improving AI assistant."
}
],
tools: ["web_search", "fetch_webpage"]
});
// 发送消息
const response = await client.agents.messages.create(
agentState.id,
{ input: "What do you know about me?" }
);
for (const message of response.messages) {
console.log(message);
}
📊 与传统 RAG 的对比
| 特性 | 传统 RAG | MemGPT/Letta |
|---|---|---|
| 记忆管理 | 外部向量数据库 | 分层内存系统 |
| 上下文感知 | 检索后拼接 | 智能内存交换 |
| 自我更新 | ❌ 静态 | ✅ Agent 可修改自己的记忆 |
| 长期学习 | ❌ 无 | ✅ 持续积累知识 |
| 工具调用 | 可选 | 内置内存管理工具 |
| 实现复杂度 | 高 | 低(开箱即用) |
🔬 研究论文核心观点
论文信息
- 标题:MemGPT: Towards LLMs as Operating Systems
- arXiv:2310.08560 (2023年10月)
- 作者:UC Berkeley 研究团队
核心贡献
虚拟上下文管理(Virtual Context Management)
- 首次将 OS 虚拟内存思想应用于 LLM
- 实现无限上下文的幻觉
分层存储管理
- Main Context ↔ External Memory 自动交换
- 类似 CPU 缓存层次结构
中断驱动控制流
- Function calling 作为"系统中断"
- Agent 主动管理内存
🛠️ 相关产品
Letta Code
- 本地终端运行的记忆优先编码 Agent
- 支持 skills 和 subagents
- 推荐模型:Opus 4.5, GPT-4o
Letta API
- 构建应用的底层 API
- 管理 Agent 的记忆和上下文
- Python & TypeScript SDK
Letta ADE
- Web 界面的 Agent 开发环境
- 可视化管理和调试
📚 相关资源
| 资源 | 链接 |
|---|---|
| 官网 | https://letta.ai |
| 文档 | https://docs.letta.com |
| GitHub | https://github.com/letta-ai/letta |
| 论文 | https://research.memgpt.ai |
| Discord | https://discord.gg/letta |
🎯 总结
MemGPT/Letta 的核心价值
- 突破上下文限制:通过虚拟内存机制,理论上实现无限上下文
- 真正的 Stateful Agent:持久化记忆,支持长期学习
- 自我改进能力:Agent 可以修改自己的记忆,不断进化
- 生产就绪:完整的 API、SDK、CLI 工具链
适用场景
- 需要长期记忆的个人助手
- 复杂文档分析
- 持续学习的客服系统
- 研究型对话 Agent
文档整理完成于 2025年2月
如有更新,请参考官方文档