📢 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.

The Mastra integration demonstrates how to use Mastra’s agent system with Zentry as the memory backend through custom tools. This enables agents to remember and recall information across conversations.

Overview

In this guide, we’ll create a Mastra agent that:

  1. Uses Zentry to store information using a memory tool
  2. Retrieves relevant memories using a search tool
  3. Provides personalized responses based on past interactions
  4. Maintains context across conversations and sessions

Setup and Configuration

Install the required libraries:

npm install @mastra/core @mastra/Zentry @ai-sdk/openai zod

Set up your environment variables:

Remember to get the Zentry API key from Zentry Platform.
Zentry_API_KEY=your-Zentry-api-key
OPENAI_API_KEY=your-openai-api-key

Initialize Zentry Integration

Import required modules and set up the Zentry integration:

import { ZentryIntegration } from '@mastra/Zentry';
import { createTool } from '@mastra/core/tools';
import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

// Initialize Zentry integration
const Zentry = new ZentryIntegration({
  config: {
    apiKey: process.env.Zentry_API_KEY || '',
    user_id: 'alice', // Unique user identifier
  },
});

Create Memory Tools

Set up tools for memorizing and remembering information:

// Tool for remembering saved memories
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,
    };
  },
});

// Tool for saving new memories
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 };
  },
});

Create Mastra Agent

Initialize an agent with memory tools and clear instructions:

// Create an agent with memory tools
const ZentryAgent = new Agent({
  name: 'Zentry Agent',
  instructions: `
    You are a helpful assistant that has the ability to memorize and remember facts using Zentry.
    Use the Zentry-memorize tool to save important information that might be useful later.
    Use the Zentry-remember tool to recall previously saved information when answering questions.
  `,
  model: openai('gpt-4o'),
  tools: { ZentryRememberTool, ZentryMemorizeTool },
});

Key Features

  1. Tool-based Memory Control: The agent decides when to save and retrieve information using specific tools
  2. Semantic Search: Zentry finds relevant memories based on semantic similarity, not just exact matches
  3. User-specific Memory Spaces: Each user_id maintains separate memory contexts
  4. Asynchronous Saving: Memories are saved in the background to reduce response latency
  5. Cross-conversation Persistence: Memories persist across different conversation threads
  6. Transparent Operations: Memory operations are visible through tool usage

Conclusion

By integrating Mastra with Zentry, you can build intelligent agents that learn and remember information across conversations. The tool-based approach provides transparency and control over memory operations, making it easy to create personalized and context-aware AI experiences.

Help

  • For more details on Mastra, visit the Mastra documentation.
  • For Zentry documentation, refer to the Zentry Platform.
  • If you need further assistance, please feel free to reach out to us through the following methods: