Memory.createThread()
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.
Usage Example
await memory?.createThread({ resourceId: "user-123" });
Parameters
resourceId:
string
Identifier for the resource this thread belongs to (e.g., user ID, project ID)
threadId?:
string
Optional custom ID for the thread. If not provided, one will be generated.
title?:
string
Optional title for the thread
metadata?:
Record<string, unknown>
Optional metadata to associate with the thread
Returns
id:
string
Unique identifier of the created thread
resourceId:
string
Resource ID associated with the thread
title:
string
Title of the thread (if provided)
createdAt:
Date
Timestamp when the thread was created
updatedAt:
Date
Timestamp when the thread was last updated
metadata:
Record<string, unknown>
Additional metadata associated with the thread
Extended usage example
src/test-memory.ts
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);
Related
- Memory Class Reference
- Getting Started with Memory (Covers threads concept)
- getThreadById
- getThreadsByResourceId
- query