Memory with Upstash
This example demonstrates how to use Mastra’s memory system with Upstash as the storage backend.
Prerequisites
This example uses the openai model and requires both Upstash Redis and Upstash Vector services. Make sure to add the following to your .env file:
OPENAI_API_KEY=<your-api-key>
UPSTASH_REDIS_REST_URL=<your-redis-url>
UPSTASH_REDIS_REST_TOKEN=<your-redis-token>
UPSTASH_VECTOR_REST_URL=<your-vector-index-url>
UPSTASH_VECTOR_REST_TOKEN=<your-vector-index-token>You can get your Upstash credentials by signing up at upstash.com and creating both Redis and Vector databases.
And install the following package:
npm install @mastra/upstashAdding memory to an agent
To add Upstash memory to an agent use the Memory class and create a new storage key using UpstashStore and a new vector key using UpstashVector. The configuration can point to either a remote service or a local setup.
import { Memory } from "@mastra/memory";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { UpstashStore } from "@mastra/upstash";
export const upstashAgent = new Agent({
name: "upstash-agent",
instructions: "You are an AI agent with the ability to automatically recall memories from previous interactions.",
model: openai("gpt-4o"),
memory: new Memory({
storage: new UpstashStore({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!
}),
options: {
threads: {
generateTitle: true
}
}
})
});Local embeddings with fastembed
Embeddings are numeric vectors used by memory’s semanticRecall to retrieve related messages by meaning (not keywords). This setup uses @mastra/fastembed to generate vector embeddings.
Install fastembed to get started:
npm install @mastra/fastembedAdd the following to your agent:
import { Memory } from "@mastra/memory";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { UpstashStore, UpstashVector } from "@mastra/upstash";
import { fastembed } from "@mastra/fastembed";
export const upstashAgent = new Agent({
name: "upstash-agent",
instructions: "You are an AI agent with the ability to automatically recall memories from previous interactions.",
model: openai("gpt-4o"),
memory: new Memory({
storage: new UpstashStore({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!
}),
vector: new UpstashVector({
url: process.env.UPSTASH_VECTOR_REST_URL!,
token: process.env.UPSTASH_VECTOR_REST_TOKEN!
}),
embedder: fastembed,
options: {
lastMessages: 10,
semanticRecall: {
topK: 3,
messageRange: 2
}
}
})
});Usage example
Use memoryOptions to scope recall for this request. Set lastMessages: 5 to limit recency-based recall, and use semanticRecall to fetch the topK: 3 most relevant messages, including messageRange: 2 neighboring messages for context around each match.
import "dotenv/config";
import { mastra } from "./mastra";
const threadId = "123";
const resourceId = "user-456";
const agent = mastra.getAgent("upstashAgent");
const message = await agent.stream("My name is Mastra", {
memory: {
thread: threadId,
resource: resourceId
}
});
await message.textStream.pipeTo(new WritableStream());
const stream = await agent.stream("What's my name?", {
memory: {
thread: threadId,
resource: resourceId
},
memoryOptions: {
lastMessages: 5,
semanticRecall: {
topK: 3,
messageRange: 2
}
}
});
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}