examples/memory-with-processors/README.md
This example demonstrates how to use and create memory processors in Mastra. Memory processors allow you to filter or transform messages before they're sent to the LLM, which is useful for:
The support agent demo (pnpm run support) showcases the TokenLimiter processor, which:
The forgetful interview agent demo (pnpm run interview) showcases:
ToolCallFilter: Removes all tool calls from the conversation historyForgetfulProcessor: Replaces messages containing specific keywords ("name", "work", etc) with a message telling the agent it forgotThe token limiter stress test (pnpm run tokens) demonstrates:
pnpm install
Run the support agent token limiting demo:
pnpm run support
Run the interactive content filtering demo:
pnpm run interview
Run the extreme token limiting stress test:
pnpm run tokens
To create a custom processor, implement the MemoryProcessor interface:
import type { CoreMessage } from '@mastra/core/llm';
import type { MemoryProcessor } from '@mastra/core/memory';
class CustomProcessor implements MemoryProcessor {
process(messages: CoreMessage[]): CoreMessage[] {
// Filter or transform messages here
return filteredMessages;
}
}
Then use it when creating your Memory instance:
const memory = new Memory({
processors: [
new CustomProcessor(),
// Can be combined with built-in processors
new TokenLimiter(8000),
],
});