Continuously Relevant Information
While conversation history and semantic recall help agents remember conversations, working memory allows them to maintain persistent information about users across interactions within a thread.
Think of it as the agent’s active thoughts or scratchpad – the key information they keep available about the user or task. It’s similar to how a person would naturally remember someone’s name, preferences, or important details during a conversation.
This is useful for maintaining ongoing state that’s always relevant and should always be available to the agent.
Quick Start
Here’s a minimal example of setting up an agent with working memory:
import { Agent } from "@mastra/core/agent";
import { Memory } from "@mastra/memory";
import { openai } from "@ai-sdk/openai";
// Create agent with working memory enabled
const agent = new Agent({
name: "PersonalAssistant",
instructions: "You are a helpful personal assistant.",
model: openai("gpt-4o"),
memory: new Memory({
options: {
workingMemory: {
enabled: true,
use: "tool-call", // Recommended setting
},
},
}),
});
How it Works
Working memory is a block of Markdown text that the agent is able to update over time to store continuously relevant information:
Custom Templates
Templates guide the agent on what information to track and update in working memory. While a default template is used if none is provided, you’ll typically want to define a custom template tailored to your agent’s specific use case to ensure it remembers the most relevant information.
Here’s an example of a custom template. In this example the agent will store the users name, location, timezone, etc as soon as the user sends a message containing any of the info:
const memory = new Memory({
options: {
workingMemory: {
enabled: true,
template: `
# User Profile
## Personal Info
- Name:
- Location:
- Timezone:
## Preferences
- Communication Style: [e.g., Formal, Casual]
- Project Goal:
- Key Deadlines:
- [Deadline 1]: [Date]
- [Deadline 2]: [Date]
## Session State
- Last Task Discussed:
- Open Questions:
- [Question 1]
- [Question 2]
`,
},
},
});
If your agent is not properly updating working memory when you expect it to, you can add system instructions on how and when to use this template in your agents instruction
setting.