Memory.recall()
The Memory.recall() method retrieves messages from a specific thread, with support for pagination, filtering options, and semantic search.
Usage exampleDirect link to Usage example
const { messages } = await memory.recall({
threadId: 'thread-123',
perPage: 20,
})
ParametersDirect link to Parameters
threadId:
resourceId?:
vectorSearchString?:
perPage?:
page?:
include?:
id (required), optional threadId (defaults to main threadId), withPreviousMessages (number of messages before, defaults to 2 for vector search, 0 otherwise), and withNextMessages (number of messages after, defaults to 2 for vector search, 0 otherwise).filter?:
dateRange filters messages by creation date. metadata filters shallow message metadata by exact scalar key-value pairs using AND semantics. Metadata values can be strings, finite numbers, booleans, or null.orderBy?:
threadConfig?:
lastMessages?:
semanticRecall?:
workingMemory?:
{ enabled: boolean; template?: string; schema?: ZodObject<any> | JSONSchema7; scope?: 'thread' | 'resource' } or { enabled: boolean } to disable.threads?:
generateTitle controls automatic thread title generation from the conversation transcript. Can be a boolean or an object with custom model and instructions.Metadata filteringDirect link to Metadata filtering
Use filter.metadata to match shallow scalar metadata stored on messages:
const { messages } = await memory.recall({
threadId: 'thread-123',
filter: {
metadata: {
category: 'billing',
escalated: true,
priority: 2,
archivedAt: null,
},
},
})
All metadata entries are combined with AND semantics. A message must match every key and value with exact type equality. null matches metadata that's explicitly set to null. It doesn't match a missing key.
Metadata filters only support shallow scalar values: string, finite number, boolean, and null. Nested objects, arrays, NaN, and infinities aren't supported. Metadata keys must start with a letter or underscore and contain only alphanumeric or underscore characters. The limit is 128 characters. Reserved prototype keys such as __proto__, constructor, and prototype aren't allowed. Performance depends on the storage backend. Arbitrary metadata filters may require scanning candidate messages, so narrow the query with threadId, resourceId, or dateRange when possible.
ReturnsDirect link to Returns
messages:
Extended usage exampleDirect link to Extended usage example
import { mastra } from './mastra'
const agent = mastra.getAgent('agent')
const memory = await agent.getMemory()
// Retrieve messages with pagination
const { messages } = await memory!.recall({
threadId: 'thread-123',
perPage: 50,
vectorSearchString: 'What messages are there?',
include: [
{
id: 'msg-123',
},
{
id: 'msg-456',
withPreviousMessages: 3,
withNextMessages: 1,
},
],
threadConfig: {
semanticRecall: true,
},
})
console.log(messages) // MastraDBMessage[]
// Fetch all messages without pagination
const allMessages = await memory!.recall({
threadId: 'thread-123',
perPage: false, // Fetch all
})
// Convert to AI SDK format if needed
import { toAISdkV5Messages } from '@mastra/ai-sdk/ui'
const uiMessages = toAISdkV5Messages(messages)