内存
🌐 Memory
内存使你的智能体能够记住用户消息、智能体回复和工具结果,从而在多次互动中提供所需的上下文,使其保持一致性、维持对话流畅,并随着时间的推移产生更好的回答。
🌐 Memory enables your agent to remember user messages, agent replies, and tool results across interactions, giving it the context it needs to stay consistent, maintain conversation flow, and produce better answers over time.
Mastra 支持三种互补的存储类型:
🌐 Mastra supports three complementary memory types:
- 消息记录 - 保存当前对话的最近消息,以便在用户界面中显示,并用于在交流过程中保持短期连续性。
- 工作内存 - 存储持久的、结构化的用户数据,例如名称、偏好和目标。
- 语义回忆 - 基于语义意义而非精确关键词,从以往对话中检索相关消息,类似于人类通过联想内存信息。需要使用向量数据库和嵌入模型。
如果组合内存超过模型的上下文限制,内存处理器可以过滤、裁剪或优先处理内容,以保留最相关的信息。
🌐 If the combined memory exceeds the model's context limit, memory processors can filter, trim, or prioritize content so the most relevant information is preserved.
入门Direct link to 入门
🌐 Getting started
选择一个存储选项以开始:
🌐 Choose a memory option to get started:
存储Direct link to 存储
🌐 Storage
在启用内存之前,你必须先配置存储适配器。Mastra 支持多种数据库,包括 PostgreSQL、MongoDB、libSQL 等 更多。
🌐 Before enabling memory, you must first configure a storage adapter. Mastra supports several databases including PostgreSQL, MongoDB, libSQL, and more.
存储可以在实例级别(所有代理共享)或代理级别(每个代理专用)进行配置。
🌐 Storage can be configured at the instance level (shared across all agents) or at the agent level (dedicated per agent).
对于语义检索,你可以在主要存储之外使用像 Pinecone 这样的独立向量数据库。
🌐 For semantic recall, you can use a separate vector database like Pinecone alongside your primary storage.
请参阅存储文档,了解配置选项、支持的提供商和示例。
🌐 See the Storage documentation for configuration options, supported providers, and examples.
调试内存Direct link to 调试内存
🌐 Debugging memory
当启用跟踪时,你可以检查代理在每个请求中用于上下文的具体消息。跟踪输出显示包含在代理上下文窗口中的所有内存——包括最近的消息历史和通过语义回忆调取的消息。
🌐 When tracing is enabled, you can inspect exactly which messages the agent uses for context in each request. The trace output shows all memory included in the agent's context window - both recent message history and messages recalled via semantic recall.

这种可见性可以帮助你理解代理为什么会做出特定决策,并验证内存检索是否按预期工作。
🌐 This visibility helps you understand why an agent made specific decisions and verify that memory retrieval is working as expected.
下一步Direct link to 下一步
🌐 Next steps