Mastra agents with memory full stack example

Dan Patel

Dan Patel

Co-founder, Trigger.dev

Image for Mastra agents with memory full stack example

We've released a new example project demonstrating how to build an AI agent with memory persistence using Mastra and Trigger.dev.

Mastra is a Typescript agent framework for building AI agents. Mastra handles AI agent memory and coordination while Trigger.dev handles orchestration, reliability, observability and scaling.

The example lets you enter a city and activity to get personalized clothing recommendations based on real-time weather data. Under the hood, it demonstrates:

  • OpenAI GPT-4 powered agents with specialized roles using Mastra's agent orchestration framework
  • PostgreSQL memory persistence enabling data sharing between agents with Zod schema validation
  • Durable task execution with Trigger.dev's retry logic, observability, and triggerAndWait orchestration
  • Serverless-ready architecture using Node.js with Open-Meteo API integration and custom tools

export const whatShouldIWearTodayTask = task({
id: "what-should-i-wear-today",
maxDuration: 15,
run: async (payload: { city: string; activity?: string }, { ctx }) => {
const threadId = ctx.run.id;
// Step 1: Weather agent stores data in shared memory
const weatherResult = await weatherDataTask.triggerAndWait({
city: payload.city,
});
// Step 2: Clothing agent reads from shared memory
const clothingResult = await clothingAdviceTask.triggerAndWait({
city: payload.city,
activity: payload.activity || "walking",
threadId: weatherResult.output.threadId,
});
return clothingResult.output;
},
});
export const weatherDataTask = task({
id: "weather-data",
run: async (payload: { city: string }, { ctx }) => {
const weatherAnalyst = mastra.getAgent("weatherAnalyst");
const response = await weatherAnalyst.generate(
`Get weather for ${payload.city} and store in working memory`,
{ threadId: ctx.run.id, resourceId: payload.city }
);
return { threadId: ctx.run.id };
},
});

Check out the full example in our GitHub examples repository to see how Mastra's agent orchestration combines with Trigger.dev's durability and observability for production-ready AI workflows.

Ready to start building?

Build and deploy your first task in 3 minutes.

Get started now