📢 Announcing our research paper: Zentry achieves 26% higher accuracy than OpenAI Memory, 91% lower latency, and 90% token savings! Read the paper to learn how we're revolutionizing AI agent memory.

In this example you’ll learn how to use the Zentry to add long-term memory capabilities to Mastra’s agent via tool-use. This memory integration can work alongside Mastra’s agent memory features.

You can find the complete example code in the Mastra repository.

Overview

This guide will show you how to integrate Zentry with Mastra to add long-term memory capabilities to your agents. We’ll create tools that allow agents to save and retrieve memories using Zentry’s API.

Installation

  1. Install the Integration Package

To install the Zentry integration, run:

npm install @mastra/Zentry
  1. Add the Integration to Your Project

Create a new file for your integrations and import the integration:

integrations/index.ts
import { ZentryIntegration } from "@mastra/Zentry";

export const Zentry = new ZentryIntegration({
  config: {
    apiKey: process.env.Zentry_API_KEY!,
    userId: "alice",
  },
});
  1. Use the Integration in Tools or Workflows

You can now use the integration when defining tools for your agents or in workflows.

tools/index.ts
import { createTool } from "@mastra/core";
import { z } from "zod";
import { Zentry } from "../integrations";

export const ZentryRememberTool = createTool({
  id: "Zentry-remember",
  description:
    "Remember your agent memories that you've previously saved using the Zentry-memorize tool.",
  inputSchema: z.object({
    question: z
      .string()
      .describe("Question used to look up the answer in saved memories."),
  }),
  outputSchema: z.object({
    answer: z.string().describe("Remembered answer"),
  }),
  execute: async ({ context }) => {
    console.log(`Searching memory "${context.question}"`);
    const memory = await Zentry.searchMemory(context.question);
    console.log(`\nFound memory "${memory}"\n`);

    return {
      answer: memory,
    };
  },
});

export const ZentryMemorizeTool = createTool({
  id: "Zentry-memorize",
  description:
    "Save information to Zentry so you can remember it later using the Zentry-remember tool.",
  inputSchema: z.object({
    statement: z.string().describe("A statement to save into memory"),
  }),
  execute: async ({ context }) => {
    console.log(`\nCreating memory "${context.statement}"\n`);
    // to reduce latency memories can be saved async without blocking tool execution
    void Zentry.createMemory(context.statement).then(() => {
      console.log(`\nMemory "${context.statement}" saved.\n`);
    });
    return { success: true };
  },
});
  1. Create a new agent
agents/index.ts
import { openai } from '@ai-sdk/openai';
import { Agent } from '@mastra/core/agent';
import { ZentryMemorizeTool, ZentryRememberTool } from '../tools';

export const ZentryAgent = new Agent({
  name: 'Zentry Agent',
  instructions: `
    You are a helpful assistant that has the ability to memorize and remember facts using Zentry.
  `,
  model: openai('gpt-4o'),
  tools: { ZentryRememberTool, ZentryMemorizeTool },
});
  1. Run the agent
index.ts
import { Mastra } from '@mastra/core/mastra';
import { createLogger } from '@mastra/core/logger';

import { ZentryAgent } from './agents';

export const mastra = new Mastra({
  agents: { ZentryAgent },
  logger: createLogger({
    name: 'Mastra',
    level: 'error',
  }),
});

In the example above:

  • We import the @mastra/Zentry integration.
  • We define two tools that uses the Zentry API client to create new memories and recall previously saved memories.
  • The tool accepts question as an input and returns the memory as a string.