Skip to main content

Memory.listThreadsByResourceId()

The .listThreadsByResourceId() method retrieves threads associated with a specific resource ID with pagination support.

Usage Example

await memory.listThreadsByResourceId({
resourceId: "user-123",
page: 0,
perPage: 10,
});

Parameters

resourceId:

string
The ID of the resource whose threads are to be retrieved

page:

number
Page number (0-indexed) to retrieve

perPage:

number
Maximum number of threads to return per page

orderBy?:

{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }
Sort configuration with field and direction (defaults to { field: 'createdAt', direction: 'DESC' })

Returns

result:

Promise<StorageListThreadsByResourceIdOutput>
A promise that resolves to paginated thread results with metadata

The return object contains:

  • threads: Array of thread objects
  • total: Total number of threads for this resource
  • page: Current page number (same as the input page parameter)
  • perPage: Items per page (same as the input perPage parameter)
  • hasMore: Boolean indicating if more results are available

Extended usage example

src/test-memory.ts
import { mastra } from "./mastra";

const agent = mastra.getAgent("agent");
const memory = await agent.getMemory();

let currentPage = 0;
const perPage = 25;
let hasMorePages = true;

while (hasMorePages) {
const result = await memory?.listThreadsByResourceId({
resourceId: "user-123",
page: currentPage,
perPage: perPage,
orderBy: { field: "createdAt", direction: "ASC" },
});

if (!result) {
console.log("No threads");
break;
}

result.threads.forEach((thread) => {
console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`);
});

hasMorePages = result.hasMore;
currentPage++; // Move to next page
}

On this page