Observational Memory
新增於: @mastra/memory@1.1.0
Observational Memory(OM)是 Mastra 用於長上下文 Agent 記憶的記憶系統。Observer 會監察對話並建立觀察結果;Reflector 則透過合併相關項目及濃縮整體模式,重新整理這些觀察結果。兩者共同維護一份觀察記錄,並在記錄增長時取代原始訊息歷史。
用法用法 的直接連結
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'my-agent',
name: 'my-agent',
instructions: 'You are a helpful assistant.',
model: 'openai/gpt-5-mini',
memory: new Memory({
options: {
observationalMemory: true,
},
}),
})
設定設定 的直接連結
observationalMemory 選項接受 true、設定物件或 false。設為 true 會啟用 OM,並以 google/gemini-2.5-flash 作為預設模型。傳入設定物件時,請在頂層設定 model,或在 observation.model 及/或 reflection.model 設定;如省略所有模型欄位,OM 會回退至 google/gemini-2.5-flash。
Observer 輸入支援多模態。OM 會在為 Observer 建立的文字記錄中保留 [Image #1: screenshot.png] 一類文字佔位符,並在可行情況下同時傳送底層圖片部分。這同時適用於單一 thread 觀察及批次多 thread 觀察。非圖片檔案只會顯示為佔位符。
OM 會使用快速的本機 token 估算來判斷閾值。文字使用 tokenx;類圖片輸入則使用能識別 Provider 的啟發式方法,並在 metadata 不完整時採用確定性的回退方式。
enabled?:
true。只有 enabled: false 會明確停用此功能。model?:
observation.model 或 reflection.model 同時使用;如兩者皆有設定,系統會擲回錯誤。如這個欄位及 observation.model/reflection.model 均省略,OM 會回退至 google/gemini-2.5-flash。使用 "default" 可明確指定預設模型(google/gemini-2.5-flash)。scope?:
'thread' 按 thread 分別保存觀察結果。'resource'(實驗功能)會在某項資源的所有 thread 之間共享觀察結果,從而啟用跨對話記憶。activateAfterIdle?:
observation.messageTokens 亦會啟用。接受如 300_000 的毫秒數值、如 "5m" 或 "1hr" 的時長字串、代表可感知 Provider 的 prompt 快取 TTL 的 "auto",或以 false 停用繼承的觀察閒置啟用。反思不會繼承此設定;使用 reflection.activateAfterIdle 可選擇讓反思採用閒置啟用。activateOnProviderChange?:
reflection.activateOnProviderChange 可選擇讓反思在 Provider 變更時啟用。temporalMarkers?:
retrieval?:
recall Tool,讓 Agent 可以瀏覽這些訊息。true 預設啟用跨 thread 瀏覽。{ vector: true } 亦會使用 Memory 的向量儲存及 embedder 啟用語意搜尋。{ scope: 'thread' } 將 recall Tool 限制於目前 thread。預設範圍為 'resource'。{ instructions: '...' } 會在 Mastra 內置的檢索指示後附加應用程式專用的 recall 指引。hooks?:
observe()/reflect() API、由 turn 驅動的同步觀察,以及發出後不等待結果的非同步緩衝。callback 會收到 threadId/resourceId/trigger 呼叫上下文('manual' | 'turn-sync' | 'async-buffer');結束 hook(onObservationEnd/onReflectionEnd)亦會收到 OM 模型呼叫的 token usage 及 providerMetadata,讓應用程式毋須以 middleware 包裝 Observer/Reflector 模型,也可計算 OM 模型開支(AI Gateway 等 Provider 會在此報告每次呼叫的成本)。非同步緩衝週期即使失敗亦不會擲回錯誤,而會透過結束 hook 的 error 欄位報告。這些 hook 擲回的錯誤會被捕捉及記錄,絕不會令週期失敗。observation?:
model?:
model,則不可設定此項。如這個欄位及頂層 model 均未設定,便回退至 reflection.model。instruction?:
threadTitle?:
true 時,Observer 會建議簡短的 thread 標題,並在對話主題出現實質變化時更新標題。此功能須選擇啟用,預設為停用。extract?:
manageWorkingMemory?:
WorkingMemoryExtractor、將 workingMemory.agentManaged 預設為 false,並將 workingMemory.useStateSignals 預設為 true。請參閱更新工作記憶。observeAttachments?:
true(預設)會轉送所有附件;false 會捨棄所有附件,但仍顯示佔位符。'auto' 使用 Provider 功能登記資料來決定:Observer 模型支援多模態輸入時轉送附件,否則捨棄;如沒有該模型的功能資料,亦會轉送。陣列是不區分大小寫的 mimeType 允許清單,支援完全匹配('application/pdf')、萬用字元子類型('image/*'),以及代表所有類型的 '*'。當 Observer 模型只支援文字(例如部分 DeepSeek endpoint),而主要 Agent 使用多模態模型時,此選項相當實用。Tool 結果附件亦按相同規則篩選。messageTokens?:
tokenx 估算;可行情況下,圖片部分會使用可感知模型的啟發式方法計算,圖片 metadata 不完整時則採用確定性的回退方式。如上載內容已正規化為檔案,類圖片的 file 部分亦以相同方式計算。maxTokensPerBatch?:
modelSettings?:
maxOutputTokens: 100_000 預設值只會在選用預設模型時套用(未設定模型、使用 "default",或使用 ModelByInputTokens 選擇器)。自訂模型沒有 maxOutputTokens 預設值。temperature?:
maxOutputTokens?:
100000 預設值只會在選用預設模型時套用;自訂模型沒有預設值。providerOptions?:
bufferTokens?:
0 至 1 之間的值是 messageTokens 的比例:0.25 表示每達閾值的 25% 便緩衝一次(預設 30k 時為 7.5k token)。1 或以上的值是絕對 token 數量:5000 表示每 5k token 緩衝一次。緩衝的觀察結果會一直儲存,直至達到 messageTokens 閾值,屆時無需阻塞式 LLM 呼叫即可立即啟用。計算結果必須小於 messageTokens。設為 false 可停用所有非同步緩衝(包括觀察及反思)。bufferOnIdle?:
bufferTokens 不同。設為 true,即可緩衝短暫閒置的 turn,而毋須等待下一個 turn 或達到 messageTokens 閾值。bufferActivation?:
0 至 1 之間的值是要移除的 messageTokens 比例:0.8 會移除約 80% 的訊息記錄並保留約 20%(預設 30k 時為 6k token)。1000 或以上的值是要保留的 token 數量:4000 會在啟用後保留約 4k 訊息 token。請留意方向相反:比例愈高,移除的記錄愈多;token 數量愈高,保留的內容愈多。activateAfterIdle?:
"auto",或 false。如未設定,觀察會使用頂層 activateAfterIdle 值。設為 false 可停用觀察的頂層閒置設定。目前只在使用獨立 ObservationalMemory 類別時套用;new Memory(...) 只會套用頂層 activateAfterIdle。activateOnProviderChange?:
activateOnProviderChange 值。目前只在使用獨立 ObservationalMemory 類別時套用;new Memory(...) 只會套用頂層 activateOnProviderChange。blockAfter?:
1 至小於 100 的值是 messageTokens 的倍數:1.2 會在閾值的 120% 強制執行阻塞式觀察(預設 30k 時為 36k token)。100 或以上的值是絕對 token 數量,而且必須大於 messageTokens。在 messageTokens 與 blockAfter 之間,只會執行非同步緩衝及啟用;緩衝啟用仍會保留最少的剩餘上下文(1000 token 或保留下限,取較小者)。只在設定了 bufferTokens 時適用。啟用非同步緩衝時,預設為 1.2。previousObserverTokens?:
0 可完全省略先前觀察結果,設為 false 則明確停用截斷。reflection?:
model?:
model,則不可設定此項。如這個欄位及頂層 model 均未設定,便回退至 observation.model。instruction?:
extract?:
observationTokens?:
modelSettings?:
maxOutputTokens: 100_000 預設值只會在選用預設模型時套用(未設定模型、使用 "default",或使用 ModelByInputTokens 選擇器)。自訂模型沒有 maxOutputTokens 預設值。temperature?:
maxOutputTokens?:
100000 預設值只會在選用預設模型時套用;自訂模型沒有預設值。providerOptions?:
bufferActivation?:
observationTokens 的比例(0 至 1)指定何時開始背景反思:0.5 表示觀察結果達到閾值的 50% 時開始背景反思(預設 40k 時為 20k token)。達到完整閾值後,已緩衝的反思會取代其涵蓋的觀察結果,同時保留該範圍後新增的任何觀察結果。activateAfterIdle?:
"auto",或 false。反思不會繼承頂層 activateAfterIdle;請明確設定此項,選擇讓反思採用閒置啟用。目前只在使用獨立 ObservationalMemory 類別時套用;透過 new Memory(...) 使用時,此設定沒有效果。activateOnProviderChange?:
activateOnProviderChange;請明確設定此項,選擇讓反思在 Provider 變更時啟用。目前只在使用獨立 ObservationalMemory 類別時套用;透過 new Memory(...) 使用時,此設定沒有效果。blockAfter?:
1 至小於 100 的值是 observationTokens 的倍數:1.2 會在閾值的 120% 強制執行阻塞式反思(預設 40k 時為 48k token)。100 或以上的值是絕對 token 數量,而且必須大於 observationTokens。在 observationTokens 與 blockAfter 之間,只會執行非同步緩衝及啟用。只在設定了 bufferActivation 時適用。啟用非同步反思時,預設為 1.2。Token 估算 metadata 快取Token 估算 metadata 快取 的直接連結
OM 會持久保存 token payload 估算,讓重複計算可重用先前的 token 估算結果。
- 部分層級快取:
part.providerMetadata.mastra。 - 字串內容回退快取:沒有任何部分時使用訊息層級 metadata。
- 如快取版本或 tokenizer 來源不相符,系統會忽略快取項目並重新計算。
- 每則訊息及每段對話的額外開銷一律在執行階段重新計算,不會快取。
- 系統會略過
data-*及reasoning部分,亦不會為它們建立快取項目。
Extractor APIExtractor API 的直接連結
Extractor 定義 OM 應在觀察或反思期間擷取的值。current-task、suggested-response 及 thread-title 等內置 OM 值,會使用與自訂值相同的 extractor pipeline。
import { Memory, Extractor } from '@mastra/memory'
import { z } from 'zod'
const memory = new Memory({
options: {
observationalMemory: {
model: 'openai/gpt-5-mini',
observation: {
extract: [
new Extractor({
name: 'User profile',
instructions: 'Extract stable user profile facts that should be remembered.',
schema: z.object({
name: z.string().optional(),
timezone: z.string().optional(),
}),
}),
],
},
},
},
})
name:
slug:
name 衍生的唯讀屬性,並非 constructor 選項。這是為持久保存值及 XML tag 產生的穩定識別碼。slug 使用小寫英文字母、數字及連字號。自訂 extractor 不可使用內置 slug 及保留的 XML tag。instructions:
schema?:
includePreviousExtraction?:
false。metadataKeyPath?:
false 可完全略過 OM metadata 持久保存。onExtracted?:
擷取行為擷取行為 的直接連結
- 擷取值會儲存在 thread OM metadata 的
om.extracted下。 - 內置 extractor 值亦會鏡像至相容性 metadata 欄位
currentTask、suggestedResponse及threadTitle。 - 只有啟用
observation.threadTitle時,thread-title才會更新 thread 標題。 observation.extract在觀察期間執行;reflection.extract在反思期間執行。- 有 schema 的 extractor 會加入後續結構化輸出請求。
- 無 schema 的 extractor 是直接在 Observer 或 Reflector 輸出中發出的 inline 字串 extractor。
- 動態 extractor 函式會收到 runtime 上下文,包括可用的
source、threadId、resourceId、mainAgent、memory及requestContext。 WorkingMemoryExtractor使用一般 extractor pipeline,透過使用中的Memoryinstance 更新工作記憶。工作記憶有 JSON schema 時,它會使用結構化擷取,並略過 OM metadata 持久保存,避免工作記憶 payload 在 OM 擷取 metadata 下重複出現。observationalMemory.observation.manageWorkingMemory會加入WorkingMemoryExtractor,並將workingMemory.agentManaged預設為false。啟用工作記憶時,它會將workingMemory.useStateSignals預設為true。- 擷取失敗會在 OM marker data 中報告,而且不會捨棄其他成功擷取的值。
範例範例 的直接連結
更新工作記憶更新工作記憶 的直接連結
如 OM 應更新工作記憶,請使用 observationalMemory.observation.manageWorkingMemory。
import { Memory } from '@mastra/memory'
const memory = new Memory({
options: {
workingMemory: {
enabled: true,
},
observationalMemory: {
enabled: true,
observation: {
manageWorkingMemory: true,
},
},
},
})
如主要 Agent 仍應接收工作記憶 Tool 及指令注入,請設定 workingMemory.agentManaged: true。
使用自訂閾值的 resource scope(實驗性)使用自訂閾值的 resource scope(實驗性) 的直接連結
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'my-agent',
name: 'my-agent',
instructions: 'You are a helpful assistant.',
model: 'openai/gpt-5-mini',
memory: new Memory({
options: {
observationalMemory: {
model: 'google/gemini-2.5-flash',
scope: 'resource',
observation: {
messageTokens: 20_000,
},
reflection: {
observationTokens: 60_000,
},
},
},
}),
})
共用 token 預算共用 token 預算 的直接連結
啟用 shareTokenBudget 後,總預算為 observation.messageTokens + reflection.observationTokens(本例為 100k)。如觀察結果只使用 30k tokens,訊息最多可擴展至使用 70k。如訊息較短,觀察結果在觸發反思前會有更多空間。
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'my-agent',
name: 'my-agent',
instructions: 'You are a helpful assistant.',
model: 'openai/gpt-5-mini',
memory: new Memory({
options: {
observationalMemory: {
shareTokenBudget: true,
observation: {
messageTokens: 20_000,
bufferTokens: false, // required when using shareTokenBudget (temporary limitation)
},
reflection: {
observationTokens: 80_000,
},
},
},
}),
})
自訂模型自訂模型 的直接連結
在設定中傳入 model,即可使用 Mastra model router 中的任何模型。
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'my-agent',
name: 'my-agent',
instructions: 'You are a helpful assistant.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
options: {
observationalMemory: {
model: 'openai/gpt-5-mini',
},
},
}),
})
為每個 Agent 使用不同模型為每個 Agent 使用不同模型 的直接連結
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'my-agent',
name: 'my-agent',
instructions: 'You are a helpful assistant.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
options: {
observationalMemory: {
observation: {
model: 'google/gemini-2.5-flash',
},
reflection: {
model: 'openai/gpt-5-mini',
},
},
},
}),
})
自訂指令自訂指令 的直接連結
提供自訂指令,以自訂 Observer 及 Reflector 的關注重點:
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'health-assistant',
name: 'health-assistant',
instructions: 'You are a health and wellness assistant.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
options: {
observationalMemory: {
model: 'google/gemini-2.5-flash',
observation: {
// Focus observations on health-related preferences and goals
instruction:
'Prioritize capturing user health goals, dietary restrictions, exercise preferences, and medical considerations. Avoid capturing general chit-chat.',
},
reflection: {
// Guide reflection to consolidate health patterns
instruction:
'When consolidating, group related health information together. Preserve specific metrics, dates, and medical details.',
},
},
},
}),
})
非同步緩衝處理非同步緩衝處理 的直接連結
非同步緩衝處理預設為啟用。隨着對話內容增加,它會在背景預先計算觀察結果:達到 messageTokens 閾值時,已緩衝的觀察結果會立即啟用,毋須等待會造成阻塞的 LLM 呼叫。
其生命週期依循緩衝 → 啟用 → 移除訊息 → 重複。背景 Observer 呼叫會按 bufferTokens 間距執行,每次產生一組觀察結果。達到閾值時,這些內容便會啟用:觀察結果移至日誌,而原始訊息則從上下文移除。如果緩衝處理未能跟上,blockAfter 閾值會強制改用同步後備處理。
預設設定:
observation.bufferTokens: 0.2:每累積相當於messageTokens20% 的內容便進行緩衝(例如閾值為 30k 時,每約 ~6k tokens 一次)observation.bufferActivation: 0.8:啟用時移除足夠的訊息,使餘下內容只佔閾值的 20%- 已緩衝的觀察結果包含延續提示(
suggestedResponse、currentTask),這些提示會在啟用後保留,以維持對話連貫性 reflection.bufferActivation: 0.5:觀察結果達到閾值的 50% 時,在背景開始反思
如要自訂:
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'my-agent',
name: 'my-agent',
instructions: 'You are a helpful assistant.',
model: 'openai/gpt-5-mini',
memory: new Memory({
options: {
observationalMemory: {
model: 'google/gemini-2.5-flash',
observation: {
messageTokens: 30_000,
// Buffer every 5k tokens (runs in background)
bufferTokens: 5_000,
// Activate to retain 30% of threshold
bufferActivation: 0.7,
// Force synchronous observation at 1.5x threshold
blockAfter: 1.5,
},
reflection: {
observationTokens: 60_000,
// Start background reflection at 50% of threshold
bufferActivation: 0.5,
// Force synchronous reflection at 1.2x threshold
blockAfter: 1.2,
},
},
},
}),
})
如要完全停用非同步緩衝處理:
observationalMemory: {
model: "google/gemini-2.5-flash",
observation: {
bufferTokens: false,
},
}
設定 bufferTokens: false 會同時停用觀察與反思的非同步緩衝處理。達到各自的閾值時,觀察與反思會同步執行。
scope: 'resource' 不支援非同步緩衝處理,因此在 resource scope 下會自動停用。
串流 data parts串流 data parts 的直接連結
Agent 執行期間,Observational Memory 會發出具類型的 data parts,讓 client 用於即時 UI 回饋。這些資料會連同 Agent 的回應以串流方式傳送。
讀取 extractor 結果讀取 extractor 結果 的直接連結
兩種完成事件的 data payload 均包含 extractor 輸出。extractor 欄位如下:
interface DataOmObservationEndPart {
type: 'data-om-observation-end'
data: {
/** Whether the completed work was an observation or reflection */
operationType: 'observation' | 'reflection'
/** Values extracted during this OM operation, keyed by extractor slug */
extractedValues?: Record<string, unknown>
/** Extractor failures from this OM operation. Successful extractor values are still included */
extractionFailures?: Array<{ slug: string; error: string }>
// ...other fields documented in the tables below
}
}
兩個 extractor 欄位均為選填。完成事件可包含值、失敗資料、兩者兼有,亦可兩者皆無。data-om-observation-end 報告同步完成;data-om-buffering-end 則報告已完成的背景工作,其緩衝內容仍有待啟用,但 extractor metadata 已經持久保存。DataOmBufferingEndPart 包含相同的 extractor 欄位,而兩種類型均由 @mastra/memory/processors 匯出。consumer 範例請參閱從串流讀取擷取值。
data-om-statusdata-om-status 的直接連結
每個 Agent 迴圈步驟會在模型生成前發出一次。它提供目前記憶狀態的快照,包括兩個上下文視窗的 token 使用量,以及任何非同步緩衝內容的狀態。
interface DataOmStatusPart {
type: 'data-om-status'
data: {
windows: {
active: {
/** Unobserved message tokens and the threshold that triggers observation */
messages: { tokens: number; threshold: number }
/** Observation tokens and the threshold that triggers reflection */
observations: { tokens: number; threshold: number }
}
buffered: {
observations: {
/** Number of buffered chunks staged for activation */
chunks: number
/** Total message tokens across all buffered chunks */
messageTokens: number
/** Projected message tokens that would be removed if activation happened now (based on bufferActivation ratio and chunk boundaries) */
projectedMessageRemoval: number
/** Observation tokens that will be added on activation */
observationTokens: number
/** idle: no buffering in progress. running: background observer is working. complete: chunks are ready for activation. */
status: 'idle' | 'running' | 'complete'
}
reflection: {
/** Observation tokens that were fed into the reflector (pre-compression size) */
inputObservationTokens: number
/** Observation tokens the reflection will produce on activation (post-compression size) */
observationTokens: number
/** idle: no reflection buffered. running: background reflector is working. complete: reflection is ready for activation. */
status: 'idle' | 'running' | 'complete'
}
}
}
recordId: string
threadId: string
stepNumber: number
/** Increments each time the Reflector creates a new generation */
generationCount: number
}
}
buffered.reflection.inputObservationTokens 是傳送至 Reflector 的觀察結果大小。buffered.reflection.observationTokens 是壓縮後的結果,即反思啟用時用來取代這些觀察結果的內容大小。client 可利用這兩個值顯示壓縮比例。
client 可從原始值計算百分比及啟用後的估算值:
// Message window usage %
const msgPercent = status.windows.active.messages.tokens / status.windows.active.messages.threshold
// Observation window usage %
const obsPercent =
status.windows.active.observations.tokens / status.windows.active.observations.threshold
// Projected message tokens after buffered observations activate
// Uses projectedMessageRemoval which accounts for bufferActivation ratio and chunk boundaries
const postActivation =
status.windows.active.messages.tokens -
status.windows.buffered.observations.projectedMessageRemoval
// Reflection compression ratio (when buffered reflection exists)
const { inputObservationTokens, observationTokens } = status.windows.buffered.reflection
if (inputObservationTokens > 0) {
const compressionRatio = observationTokens / inputObservationTokens
}
data-om-observation-startdata-om-observation-start 的直接連結
Observer 或 Reflector Agent 開始處理時發出。
cycleId:
operationType:
startedAt:
tokensToObserve:
recordId:
threadId:
threadIds:
config:
messageTokens、observationTokens 及 scope 的設定快照。data-om-observation-enddata-om-observation-end 的直接連結
觀察或反思成功完成時發出。
cycleId:
start 標記相符。operationType:
completedAt:
durationMs:
tokensObserved:
observationTokens:
observations?:
currentTask?:
suggestedResponse?:
extractedValues?:
extractionFailures?:
recordId:
threadId:
data-om-observation-faileddata-om-observation-failed 的直接連結
觀察或反思失敗時發出。系統會改用同步處理作為後備方案。
cycleId:
start 標記相符。operationType:
failedAt:
durationMs:
tokensAttempted:
error:
observations?:
recordId:
threadId:
data-om-buffering-startdata-om-buffering-start 的直接連結
非同步緩衝處理在背景開始時發出。緩衝處理會在達到主要閾值前預先計算觀察結果或反思。
cycleId:
operationType:
startedAt:
tokensToBuffer:
recordId:
threadId:
threadIds:
config:
data-om-buffering-enddata-om-buffering-end 的直接連結
非同步緩衝處理完成時發出。內容已儲存,但尚未在主要上下文中啟用。
cycleId:
buffering-start 標記相符。operationType:
completedAt:
durationMs:
tokensBuffered:
bufferedTokens:
observations?:
extractedValues?:
extractionFailures?:
recordId:
threadId:
data-om-buffering-faileddata-om-buffering-failed 的直接連結
非同步緩衝處理失敗時發出。達到閾值時,系統會改用同步處理作為後備方案。
cycleId:
buffering-start 標記相符。operationType:
failedAt:
durationMs:
tokensAttempted:
error:
observations?:
recordId:
threadId:
data-om-activationdata-om-activation 的直接連結
已緩衝的觀察結果或反思啟用(移至作用中的上下文視窗)時發出。這項操作會即時完成,不涉及 LLM 呼叫。
cycleId:
operationType:
activatedAt:
chunksActivated:
tokensActivated:
observationTokens:
messagesActivated:
generationCount:
observations?:
triggeredBy?:
activateAfterIdle 到期,還是模型/provider 變更而觸發。lastActivityAt?:
ttlExpiredMs?:
activateAfterIdle 的時長。previousModel?:
openai/gpt-4o)。currentModel?:
recordId:
threadId:
config:
data-om-thread-updatedata-om-thread-update 的直接連結
Observer 更新 thread 標題時發出。只會在啟用 observation.threadTitle 時發出。
cycleId:
threadId:
oldTitle?:
newTitle:
timestamp:
獨立使用獨立使用 的直接連結
大部分使用者都應使用上文的 Memory 類別。直接使用 ObservationalMemory 主要適用於效能基準測試、實驗,或需要控制它與其他 processor(例如 guardrails)之間的排序時。
ObservationalMemory 類別是核心引擎;如要將它附加至 Agent,請用 ObservationalMemoryProcessor 將它包裝起來,而該 processor 需要一個 Memory 實例來載入及持久保存訊息。請注意,storage adapter 將 stores.memory 的型別定義為可選,因此需要使用非空斷言(或在執行階段檢查):
import { ObservationalMemory, ObservationalMemoryProcessor } from '@mastra/memory/processors'
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
import { LibSQLStore } from '@mastra/libsql'
const storage = new LibSQLStore({
id: 'my-storage',
url: 'file:./memory.db',
})
const memory = new Memory({ storage })
const om = new ObservationalMemory({
storage: storage.stores.memory!,
memory,
model: 'google/gemini-2.5-flash',
scope: 'resource',
observation: {
messageTokens: 20_000,
},
reflection: {
observationTokens: 60_000,
},
})
const omProcessor = new ObservationalMemoryProcessor(om, memory)
export const agent = new Agent({
id: 'my-agent',
name: 'my-agent',
instructions: 'You are a helpful assistant.',
model: 'openai/gpt-5-mini',
inputProcessors: [omProcessor],
outputProcessors: [omProcessor],
})
獨立設定獨立設定 的直接連結
獨立的 ObservationalMemory 類別接受上文 observationalMemory 設定物件的所有相同選項,以及以下選項:
storage:
MastraStorage.stores.memory)。onDebugEvent?:
obscureThreadIds?:
Recall toolRecall tool 的直接連結
設定 retrieval(任何 truthy 值)後,系統會註冊一個 recall tool,讓 Agent 可以逐頁瀏覽觀察群組範圍背後的原始訊息。預設情況下(scope 為 'resource'),此 tool 支援列出 thread(mode: "threads")、瀏覽其他 thread(threadId),以及跨 thread 搜尋。使用 retrieval: { vector: true } 時,可進行語意搜尋(mode: "search")。設定 scope: 'thread',可將此 tool 限制為只存取目前的 thread。此 tool 會自動加入 Agent 的 tool 清單。
Mastra 亦會將能感知 scope 的使用指示注入 Agent 的情境。對於使用 vector: true 的 resource scope,這些指示涵蓋如何在 search、threads 與 messages 之間選擇路徑,包括當搜尋結果不合適時,改為探索 thread。若沒有 vector: true,指示只會涵蓋瀏覽 threads 與 messages,因此不會引導 Agent 使用尚未設定的搜尋模式。即使尚未存在任何觀察群組,resource scope 的指示亦會注入,讓 Agent 從第一則訊息起便可瀏覽其他 thread。使用 retrieval: { instructions: '...' },可在內置指示之後加入應用程式專用指引。
參數參數 的直接連結
mode?:
"messages"(預設)逐頁瀏覽訊息記錄。"threads" 列出目前使用者的所有 thread。"search" 按語意相似度在所有 thread 中尋找訊息(需要 vector store 及 embedder)。query?:
mode: "search" 的搜尋查詢。在目前使用者的所有 thread 中尋找與此文字語意相似的訊息。cursor?:
_range: \startId:endId\_ 使用 startId 或 endId)。如果直接傳入範圍字串,此 tool 會傳回提示,說明如何擷取正確的 ID。當同時省略 cursor 與 threadId,並使用 mode: "messages" 時,此 tool 會從 anchor 所設定的位置開始瀏覽目前的 thread。threadId?:
"current" 以使用使用中的 thread。請先使用 mode: "threads" 尋找 thread ID。如果提供此參數但沒有提供 cursor,便會從 thread 開頭開始讀取。anchor?:
mode: "messages",如沒有 cursor,便從 thread 開頭(最舊優先)或結尾(最新優先)開始分頁。page?:
0 視為 1。limit?:
detail?:
'low' 顯示截短的文字及附有位置索引([p0]、[p1])的 tool 名稱。'high' 顯示包括 tool 引數及結果在內的完整內容,每次呼叫最多顯示一個部分,並附有繼續提示。partType?:
mode: "messages"。toolName?:
mode: "messages"。partIndex?:
[p1] 顯示值得留意的部分時使用此參數——以 partIndex: 1 再次呼叫,即可查看完整內容,而毋須載入每個部分。before?:
mode: "threads"。篩選在此日期之前建立的 thread。接受 ISO 8601 格式(例如 "2026-03-15"、"2026-03-10T00:00:00Z")。after?:
mode: "threads"。篩選在此日期之後建立的 thread。接受 ISO 8601 格式(例如 "2026-03-01"、"2026-03-10T00:00:00Z")。傳回值(messages 模式)傳回值(messages 模式) 的直接連結
messages:
detail 層級。count:
cursor:
page:
limit:
detail:
hasNextPage:
hasPrevPage:
truncated?:
true。Agent 可透過分頁或使用 partIndex 存取餘下內容。tokenOffset?:
truncated 為 true 時,被刪減的約略 token 數目。傳回值(threads 模式)傳回值(threads 模式) 的直接連結
threads:
← current 標示。count:
page:
hasMore:
傳回值(search 模式)傳回值(search 模式) 的直接連結
results:
count:
ModelByInputTokensModelByInputTokens 的直接連結
ModelByInputTokens 根據輸入 token 數目選擇模型。它會選擇能涵蓋實際輸入大小的最小閾值所對應的模型。
建構函式建構函式 的直接連結
new ModelByInputTokens(config)
其中 config 是一個含有 upTo 鍵的物件,這些鍵會將 token 閾值(數字)對應至目標模型。
範例範例 的直接連結
import { ModelByInputTokens } from '@mastra/memory'
const selector = new ModelByInputTokens({
upTo: {
10_000: 'google/gemini-2.5-flash', // Fast for small inputs
40_000: 'openai/gpt-5-mini', // Stronger for medium inputs
1_000_000: 'openai/gpt-5.6-sol', // Most capable for large inputs
},
})
行為行為 的直接連結
- 閾值會在內部排序,因此設定物件中的次序並不重要。
inputTokens ≤ smallest threshold→ 使用該閾值的模型inputTokens > largest threshold→resolve()會拋出錯誤。如果在 OM Observer 或 Reflector 執行期間發生此情況,OM 會透過 TripWire 中止,因此呼叫者會收到空白的text結果或串流的tripwire,而非正常的 assistant 回應。- OM 會計算 Observer 或 Reflector 呼叫的輸入 token 數目,並直接解析相符的模型層級