---
title: "Reference: Memory.createThread() | Memory"
description: "Documentation for the `Memory.createThread()` method in Mastra, which creates a new conversation thread in the memory system."
packages:
  - "@mastra/memory"
---

# 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

```typescript
await memory?.createThread({ resourceId: "user-123" });
```

## Parameters

<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,
    },
  ]}
/>

## Returns

<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",
    },
  ]}
/>

## Extended usage example

```typescript title="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](/reference/v1/memory/memory-class)
- [Getting Started with Memory](/docs/v1/memory/overview) (Covers threads concept)
- [getThreadById](/reference/v1/memory/getThreadById)
- [listThreadsByResourceId](/reference/v1/memory/listThreadsByResourceId)
- [query](/reference/v1/memory/query)
