MemLibMemLib

prepare()

Context-aware memory synthesis — generate a briefing for your system prompt

Signature

mem.prepare(options: PrepareOptions): Promise<PrepareResult>

What It Does

prepare() is the killer feature for AI agents. Instead of dumping raw memories into your system prompt, it:

  1. Analyzes the conversation to understand what the user is trying to do
  2. Generates 2–4 targeted search queries based on intent
  3. Runs parallel semantic searches across those queries
  4. Deduplicates and ranks all candidate memories
  5. Synthesizes a concise briefing paragraph — only including relevant facts

The output is a ready-to-use context paragraph you can inject directly into your system prompt.


Basic Usage

const { context, memoriesUsed, tokenCount } = await mem.prepare({
  messages: [
    { role: "user", content: "Can you help me plan dinner tonight?" },
  ],
});

console.log(context);
// "The user is severely allergic to peanuts and loves sushi and Japanese food.
//  They live in Berlin and prefer dining out on weekdays."

console.log(memoriesUsed);          // ["mem_1", "mem_2", "mem_3"]
console.log(tokenCount);            // ~42

Injecting Into Your System Prompt

const systemPrompt = `You are a helpful assistant.

About this user:
${context}`;

// Pass to your LLM of choice
const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "system", content: systemPrompt },
    { role: "user", content: "Can you help me plan dinner tonight?" },
  ],
});

Options

OptionTypeRequiredDefaultDescription
messagesMessage[]Conversation messages to analyze for context
namespacestringconstructor defaultOverride namespace
entitystringconstructor defaultOverride entity
maxCandidatesnumber20Max memories to consider before synthesis
maxTokensnumberGuide the output token budget
categorystringFilter candidates by category

Message Format

interface Message {
  role: "user" | "assistant" | "system";
  content: string;
}

Only the last 6 messages are used for intent analysis — you don't need to worry about trimming.


Return Type

interface PrepareResult {
  /** Synthesized context paragraph, ready to inject into a system prompt */
  context: string;
  /** IDs of memories that were used in the synthesis */
  memoriesUsed: string[];
  /** Number of candidate memories considered */
  candidatesConsidered: number;
  /** Estimated token count of the context */
  tokenCount: number;
}

If no relevant memories are found, context will be an empty string and memoriesUsed will be empty.


Pipeline

Always 2 LLM calls regardless of how many memories exist:

  1. Intent analysis — generates targeted search queries from the conversation
  2. Synthesis — filters irrelevant memories and produces a concise briefing

Examples

Multi-turn conversation

const ctx = await mem.prepare({
  messages: [
    { role: "user", content: "I need to prepare for my meeting tomorrow" },
    { role: "assistant", content: "Sure! What meeting is it?" },
    { role: "user", content: "The quarterly review with the product team" },
  ],
});
// context might include: "Works as engineering lead. Reports to VP of Product.
// Has been working on the search feature for the past quarter."

Limit candidates for faster responses

const ctx = await mem.prepare({
  messages: [{ role: "user", content: "Quick question about my setup" }],
  maxCandidates: 10,  // fewer candidates = faster synthesis
});

Focus on a specific category

const ctx = await mem.prepare({
  messages: [{ role: "user", content: "What should I eat?" }],
  category: "health",  // only consider health-related memories
});

When to Use prepare() vs recall()

Use CaseMethod
You want a ready-to-use context paragraph for a system promptprepare()
You want raw memory objects to process yourselfrecall()
You need to display memories in a UIrecall()
You want task-specific, filtered context with minimal tokensprepare()
You want zero LLM callsrecall()

On this page