Memory クラス
Memory クラスは、Mastra で会話履歴とスレッドベースのメッセージストレージを管理するための信頼性の高いシステムを提供します。会話の永続化、セマンティック検索、効率的なメッセージ取得が可能です。会話履歴にはストレージ Provider の設定が必要です。セマンティックリコールを有効にする場合は、ベクトルストアと Embedder も指定する必要があります。
使用例使用例への直接リンク
src/mastra/agents/test-agent.ts
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'test-agent',
name: 'test-agent',
instructions: 'You are an agent with memory.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
options: {
workingMemory: {
enabled: true,
},
},
}),
})
注記
Agent で workingMemory を有効にするには、メインの Mastra インスタンスにストレージ Provider を設定する必要があります。詳細は Mastra クラスを参照してください。
コンストラクターのパラメーターコンストラクターのパラメーターへの直接リンク
storage?:
MastraCompositeStore
Memory データを永続化するためのストレージ実装。指定しない場合のデフォルトは
new DefaultStorage({ config: { url: "file:memory.db" } })。vector?:
MastraVector | false
セマンティック検索機能に使用するベクトルストア。ベクトル操作を無効にするには
false を設定します。embedder?:
EmbeddingModel<string> | EmbeddingModelV2<string>
ベクトル埋め込みに使用する Embedder インスタンス。セマンティックリコールを有効にする場合は必須です。
options?:
MemoryConfig
Memory の設定オプション。
lastMessages?:
number | false
コンテキストに含める最新メッセージの数。メッセージ履歴機能全体を無効にするには
false を設定します(メッセージはコンテキストに読み込まれず、保存もされません)。制限なくすべてのメッセージを取得するには Number.MAX_SAFE_INTEGER を使用します。新しいメッセージを保存せずに読み込むには、readOnly オプションを使用します。readOnly?:
boolean
true の場合、新しいメッセージを Memory に保存せず、ワーキングメモリを読み取り専用コンテキストとして提供します(updateWorkingMemory Tool は含まれません)。プレビュー、内部ルーティング Agent、Memory を参照するだけで変更すべきでないサブ Agent などの読み取り専用操作に便利です。
semanticRecall?:
boolean | { topK: number; messageRange: number | { before: number; after: number }; scope?: 'thread' | 'resource' }
メッセージ履歴のセマンティック検索を有効にします。真偽値または設定オプションを含むオブジェクトを指定できます。有効にするには、ベクトルストアと Embedder の両方を設定する必要があります。topK のデフォルトは 4、messageRange のデフォルトは {before: 1, after: 1} です。
workingMemory?:
WorkingMemory
ワーキングメモリ機能の設定。
{ enabled: boolean; template?: string; schema?: ZodObject<any> | JSONSchema7; scope?: 'thread' | 'resource' }、または無効にする場合は { enabled: boolean } を指定できます。observationalMemory?:
boolean | ObservationalMemoryOptions
長いコンテキストを扱う Agent Memory として Observational Memory を有効にします。デフォルト設定を使う場合は
true、トークン予算、モデル、スコープをカスタマイズする場合は設定オブジェクトを渡します。設定の詳細は Observational Memory リファレンスを参照してください。generateTitle?:
boolean | { model: DynamicArgument<MastraLanguageModel>; instructions?: DynamicArgument<string> }
会話トランスクリプトからのスレッドタイトル自動生成を制御します。真偽値、またはカスタムモデルと指示を含むオブジェクトを指定できます。
戻り値戻り値への直接リンク
memory:
Memory
指定した設定を持つ新しい Memory インスタンス。
詳細な使用例詳細な使用例への直接リンク
src/mastra/agents/test-agent.ts
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
export const agent = new Agent({
name: 'test-agent',
instructions: 'You are an agent with memory.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
storage: new LibSQLStore({
id: 'test-agent-storage',
url: 'file:./working-memory.db',
}),
vector: new LibSQLVector({
id: 'test-agent-vector',
url: 'file:./vector-memory.db',
}),
options: {
lastMessages: 10,
semanticRecall: {
topK: 3,
messageRange: 2,
scope: 'resource',
},
workingMemory: {
enabled: true,
},
generateTitle: true,
},
}),
})
インデックス設定を使用する PostgreSQLインデックス設定を使用する PostgreSQLへの直接リンク
src/mastra/agents/pg-agent.ts
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
import { PgStore, PgVector } from '@mastra/pg'
export const agent = new Agent({
name: 'pg-agent',
instructions: 'You are an agent with optimized PostgreSQL memory.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
storage: new PgStore({
id: 'pg-agent-storage',
connectionString: process.env.DATABASE_URL,
}),
vector: new PgVector({
id: 'pg-agent-vector',
connectionString: process.env.DATABASE_URL,
}),
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
options: {
lastMessages: 20,
semanticRecall: {
topK: 5,
messageRange: 3,
scope: 'resource',
indexConfig: {
type: 'hnsw', // Use HNSW for better performance
metric: 'dotproduct', // Optimal for OpenAI embeddings
m: 16, // Number of bi-directional links
efConstruction: 64, // Construction-time candidate list size
},
},
workingMemory: {
enabled: true,
},
},
}),
})