Skip to main content

Memory.summarizeThread()

The .summarizeThread() method summarizes a thread's conversation in one shot. It loads the thread's messages from storage and distills them with the same Observer plumbing that powers Observational Memory — as a standalone call, without Observational Memory attached to an agent.

Messages load page-by-page starting from the newest, bounded by lastMessages and maxInputTokens, so summarizing a long thread doesn't read its entire history from storage.

Nothing is written back to memory. The summary and extracted values are returned to you (and to each extractor's onExtracted hook), so you decide where they go — for example your own database.

Use this when a session ends and you want a summary or structured extraction of the whole conversation, such as a voice call at hang-up. To summarize messages you already have in hand (without loading them from a thread), use the standalone summarizeConversation() function instead — it takes the same options with messages in place of threadId.

Usage example
Direct link to Usage example

const result = await memory.summarizeThread({
model: 'openai/gpt-5-mini',
threadId: 'thread-123',
instructions: 'Summarize this voicemail call for the business owner.',
})

Parameters
Direct link to Parameters

threadId:

string
The unique identifier of the thread to summarize.

model:

string | LanguageModel | DynamicModel
Model that runs the summarization, such as a router string like '__GATEWAY_OPENAI_MODEL_MINI__'.

resourceId?:

string
ID of the resource that owns the thread. If provided, validates thread ownership. Also passed to extractor contexts.

lastMessages?:

number
Only summarize the last N messages of the thread. By default the whole thread is loaded, bounded by maxInputTokens.

maxInputTokens?:

number
= 1000000
Stop loading older messages once the collected messages exceed this estimated token count. The newest message is always included.

instructions?:

string
Extra guidance appended to the summarizer's system prompt, such as what to focus on or who the summary is for.

extract?:

Extractor[]
Extractors to run over the conversation. Extractors with a Zod schema run as a follow-up structured output call; schema-less extractors are extracted inline. Each extractor's onExtracted hook fires with the extracted value.

requestContext?:

RequestContext
Request context forwarded to the summarizer model and extractor hooks.

abortSignal?:

AbortSignal
Signal to cancel the summarization call.

Returns
Direct link to Returns

summary:

string
The distilled observations produced from the conversation, in dense bullet form.

extracted:

Record<string, unknown>
Values produced by extract extractors, keyed by extractor slug.

extractionFailures:

{ slug: string; error: string }[]
Extractors that failed to produce a valid value, with the reason. Only present when at least one extractor failed.

usage:

{ inputTokens?: number; outputTokens?: number; totalTokens?: number }
Token usage of the summarization call.

Extended usage example
Direct link to Extended usage example

The following example summarizes a finished voice call and stores a structured record in the application's own database:

src/call-summary.ts
import { Extractor } from '@mastra/memory'
import { z } from 'zod'
import { memory } from './mastra/memory'
import { callRecords } from './db'

const callSummary = new Extractor({
name: 'call-summary',
instructions: 'Return a concise summary of the call.',
schema: z.object({
summary: z.string(),
sentiment: z.enum(['positive', 'neutral', 'negative']),
requestedServices: z.array(z.string()),
}),
metadataKeyPath: false, // don't persist into memory metadata — the hook owns storage
onExtracted: async ({ current, threadId, resourceId }) => {
await callRecords.upsert({ callId: threadId, callerId: resourceId, record: current })
},
})

export async function onCallEnd(threadId: string, resourceId: string) {
await memory.summarizeThread({
model: 'openai/gpt-5-mini',
threadId,
resourceId,
instructions: 'Summarize this voicemail call for the business owner.',
extract: [callSummary],
})
}
On this page