docs/src/content/en/reference/memory/createThread.mdx
The .createThread() method creates a new conversation thread in the memory system. Each thread represents a distinct conversation or context and can contain multiple messages.
await memory?.createThread({ resourceId: 'user-123' })
<PropertiesTable content={[ { name: 'resourceId', type: 'string', description: 'Identifier for the resource this thread belongs to (e.g., user ID, project ID)', isOptional: false, }, { name: 'threadId', type: 'string', description: 'Optional custom ID for the thread. If not provided, one will be generated.', isOptional: true, }, { name: 'title', type: 'string', description: 'Optional title for the thread', isOptional: true, }, { name: 'metadata', type: 'Record<string, unknown>', description: 'Optional metadata to associate with the thread', isOptional: true, }, ]} />
<PropertiesTable content={[ { name: 'id', type: 'string', description: 'Unique identifier of the created thread', }, { name: 'resourceId', type: 'string', description: 'Resource ID associated with the thread', }, { name: 'title', type: 'string', description: 'Title of the thread (if provided)', }, { name: 'createdAt', type: 'Date', description: 'Timestamp when the thread was created', }, { name: 'updatedAt', type: 'Date', description: 'Timestamp when the thread was last updated', }, { name: 'metadata', type: 'Record<string, unknown>', description: 'Additional metadata associated with the thread', }, ]} />
import { mastra } from './mastra'
const agent = mastra.getAgent('agent')
const memory = await agent.getMemory()
const thread = await memory?.createThread({
resourceId: 'user-123',
title: 'Memory Test Thread',
metadata: {
source: 'test-script',
purpose: 'memory-testing',
},
})
const response = await agent.generate('message for agent', {
memory: {
thread: thread!.id,
resource: thread!.resourceId,
},
})
console.log(response.text)