Skip to Content
MemorycreateLongTermMemory

createLongTermMemory

Problem

In-memory conversation history disappears the moment a Node.js process restarts. For production agents — where users return days later and expect the agent to remember past conversations — you need persistence. But wiring up a database adapter every time is repetitive and inconsistent.


Solution

createLongTermMemory gives you a standard interface for persistent memory. If you pass your own storage adapter (Redis, Postgres, a vector store via LangChain), it uses that. If you pass nothing, it returns a no-op stub so your code does not break during development.


Feature & Use-Case

Use createLongTermMemory when:

  • Users need persistent conversation history across sessions or server restarts
  • You are building a LangChain RAG agent and want message history in a vector store
  • You want a standard interface for different storage backends without changing agent code
  • During development, use it with no adapter — it silently no-ops so you can build without a database

Import

import { createLongTermMemory } from "llm-layer-engine";

Function Signature

function createLongTermMemory(adapter?: LongTermMemory): LongTermMemory

LongTermMemory Interface

interface LongTermMemory { save(userId: string, messages: Message[]): Promise<void>; load(userId: string): Promise<Message[]>; search?(query: string): Promise<Message[]>; }

Method Reference

MethodRequiredDescription
save(userId, messages)Persist messages for a user
load(userId)Load stored messages for a user
search(query)Semantic search over stored messages

Example — No Adapter (Development Mode)

import { createLongTermMemory } from "llm-layer-engine"; const memory = createLongTermMemory(); await memory.save("user_123", [{ role: "user", content: "Hello" }]); const messages = await memory.load("user_123"); console.log(messages); // → [] (no-op stub, nothing persisted)

Use this during development so your agent code works before you wire a real database.


Example — Custom Redis Adapter

import { createLongTermMemory } from "llm-layer-engine"; import type { LongTermMemory, Message } from "llm-layer-engine"; import { createClient } from "redis"; const redis = createClient({ url: process.env.REDIS_URL }); await redis.connect(); const redisAdapter: LongTermMemory = { async save(userId, messages) { await redis.set(`memory:${userId}`, JSON.stringify(messages)); }, async load(userId) { const raw = await redis.get(`memory:${userId}`); return raw ? (JSON.parse(raw) as Message[]) : []; }, }; const memory = createLongTermMemory(redisAdapter); // Save after a session await memory.save("user_123", [ { role: "user", content: "What is the Node.js event loop?" }, { role: "assistant", content: "The event loop is..." }, ]); // Load on next request const history = await memory.load("user_123"); console.log(history.length); // → 2

Example — Using With an Agent

const history = await memory.load(userId); const agent = createAgent({ provider: myProvider, model: "claude-3-5-sonnet-20241022" }); const { result } = await agent.run([ ...history, { role: "user", content: "Continue where we left off." }, ]); await memory.save(userId, [...history, { role: "assistant", content: result }]);

Conclusion

createLongTermMemory gives you a clean, swappable persistence interface. Start with no adapter during development, then plug in Redis, Postgres, or a LangChain vector store when you are ready. Your agent code never changes — only the adapter does.

Last updated on