WorkingMemory
The WorkingMemory is an input processor that injects working memory data as a system message. It retrieves persistent information from storage and formats it as instructions for the LLM, enabling the agent to maintain context about users across conversations.
Usage exampleUsage example的直接链接
import { WorkingMemory } from '@mastra/core/processors'
const processor = new WorkingMemory({
storage: memoryStorage,
scope: 'resource',
template: {
format: 'markdown',
content: `# User Profile
- **Name**:
- **Preferences**:
- **Goals**:
`,
},
})
Constructor parametersConstructor parameters的直接链接
options:
Options
Configuration options for the working memory processor
Options
storage:
MemoryStorage
Storage instance for retrieving working memory data
template?:
WorkingMemoryTemplate
Template defining the format and structure of working memory
WorkingMemoryTemplate
format:
'markdown' | 'json'
Format of the working memory content
content:
string
Template content defining the structure of working memory data
scope?:
'thread' | 'resource'
Scope of working memory. 'thread' scopes to current thread, 'resource' shares across all threads for the resource
useVNext?:
boolean
Use the next-generation instruction format with improved guidelines
readOnly?:
boolean
When true, working memory is provided as read-only context. The data is injected into the conversation but without the updateWorkingMemory tool or update instructions. Useful for agents that should reference working memory without modifying it.
templateProvider?:
{ getWorkingMemoryTemplate(args: { memoryConfig?: MemoryConfig }): Promise<WorkingMemoryTemplate | null> }
Dynamic template provider for runtime template resolution
logger?:
IMastraLogger
Optional logger instance for structured logging
ReturnsReturns的直接链接
id:
string
Processor identifier set to 'working-memory'
name:
string
Processor display name set to 'WorkingMemory'
defaultWorkingMemoryTemplate:
string
The default markdown template used when no custom template is provided
processInput:
(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>
Retrieves working memory and adds it as a system message to the message list
Extended usage exampleExtended usage example的直接链接
src/mastra/agents/personalized-agent.ts
import { Agent } from '@mastra/core/agent'
import { WorkingMemory, MessageHistory } from '@mastra/core/processors'
import { PostgresStore } from '@mastra/pg'
const storage = new PostgresStore({
id: 'pg-storage',
connectionString: process.env.DATABASE_URL,
})
export const agent = new Agent({
id: 'personalized-agent',
name: 'personalized-agent',
instructions: 'You are a helpful assistant that remembers user preferences',
model: 'openai/gpt-5.6-sol',
inputProcessors: [
new WorkingMemory({
storage,
scope: 'resource',
template: {
format: 'markdown',
content: `# User Information
- **Name**:
- **Location**:
- **Preferences**:
- **Communication Style**:
- **Current Projects**:
`,
},
}),
new MessageHistory({ storage, lastMessages: 50 }),
],
outputProcessors: [new MessageHistory({ storage })],
})
JSON format exampleJSON format example的直接链接
import { WorkingMemory } from '@mastra/core/processors'
const processor = new WorkingMemory({
storage: memoryStorage,
scope: 'resource',
template: {
format: 'json',
content: JSON.stringify({
user: {
name: { type: 'string' },
preferences: { type: 'object' },
goals: { type: 'array' },
},
}),
},
})
BehaviorBehavior的直接链接
Input processingInput processing的直接链接
- Retrieves
threadIdandresourceIdfrom the request context - Based on scope, fetches working memory from either:
- Thread metadata (
scope: 'thread') - Resource record (
scope: 'resource')
- Thread metadata (
- Resolves the template (from provider, options, or default)
- Generates system instructions based on mode:
- Normal mode: Includes guidelines for storing and updating information, along with the template structure and current data
- Read-only mode (
readOnly: true): Includes only the current data as context without update instructions
- Adds the instruction as a system message with
source: 'memory'tag
Working memory updatesWorking memory updates的直接链接
Working memory updates happen through the updateWorkingMemory tool provided by the Memory class, not through this processor. The processor only handles injecting the current working memory state into conversations.
Default templateDefault template的直接链接
If no template is provided, the processor uses a default markdown template with fields for:
- First Name, Last Name
- Location, Occupation
- Interests, Goals
- Events, Facts, Projects