Memory
A file-based agent gets memory from a memory.ts file that default-exports a Memory instance. Use this page for the file-based convention; use the memory docs for message history, semantic recall, storage, and processors.
Without memory.ts or config.memory, the agent has no memory by default. Each generate() or stream() call starts without remembered conversation state unless you pass the prior context yourself.
QuickstartDirect link to Quickstart
Create memory.ts next to the agent's config.ts:
import { Memory } from '@mastra/memory'
export default new Memory()
The exported instance becomes the agent's memory. If your app configures a storage provider on the main Mastra instance, memory data is stored there. See storage for more information.
Use the same resource and thread values when calling the agent to continue a conversation:
const response = await weatherAgent.generate('Remember that I prefer Celsius.', {
memory: {
resource: 'user-123',
thread: 'weather-chat',
},
})
Configure memoryDirect link to Configure memory
Pass options to new Memory() when the default behavior isn't enough.
import { Memory } from '@mastra/memory'
export default new Memory({
options: {
lastMessages: 20,
workingMemory: {
enabled: true,
scope: 'resource',
},
},
})
Visit the Memory reference for constructor options. Use these pages for related memory features:
- Storage: configure persistence for memory data.
- Semantic recall: retrieve relevant past messages by semantic meaning.
- Memory processors: filter, trim, or transform messages before memory adds them to model context.
Precedence with configDirect link to Precedence with config
config.memory wins over memory.ts. If neither is present, the assembled file-based agent has no memory. See config.ts precedence for the full merge table.