translation/source/documents/course/01-first-agent/16-adding-memory-to-agent.md

1.3 KiB

Adding Memory to Your Agent

Now, let's update our agent to include memory. Open your agents/index.ts file and make the following changes:

  1. Import the Memory and LibSQLStore classes:
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { Memory } from "@mastra/memory";
import { LibSQLStore } from "@mastra/libsql";
import { getTransactionsTool } from "../tools";
  1. Add memory to your agent configured by a storage instance:
export const financialAgent = new Agent({
  name: "Financial Assistant Agent",
  instructions: `ROLE DEFINITION
  // ... existing instructions ...
  `,
  model: openai("gpt-4o"),
  tools: { getTransactionsTool },
  memory: new Memory({
    storage: new LibSQLStore({
      id: "learning-memory-storage",
      url: "file:../../memory.db", // local file-system database. Location is relative to the output directory `.mastra/output`
    }),
  }), // Add memory here
});

By adding the memory property to your agent configuration, you're enabling it to remember previous conversations. The Memory class from the @mastra/memory package provides a simple way to add memory capabilities to your agent. The LibSQLStore class from @mastra/libsql persists the memory data to a SQLite database.