Memnest
Build with it

Add memory to an agent

The recall-then-remember loop, with any model and any framework.

Memory is a loop around your model call: recall before, remember after. Memnest does not wrap your model or lock you to a provider — it stores what was learned and gives you the current facts, whichever model you call.

The loop

  1. user turna question arrives
  2. recallprofile + facts for this turn, within a budget
  3. your modelany provider; facts go in the system prompt
  4. replygrounded in what is true now
  5. add()hand the turn back; extraction runs in the background

A complete example

This works with the embedded engine or the HTTP client, unchanged.

import { scopeOf } from '@memnest/core';

export async function answer(userId: string, conversationId: string, question: string) {
  const scope = scopeOf(`user:${userId}`);

  // 1. What do we know, in general and about this question?
  const [profile, recalled] = await Promise.all([
    memnest.profile(scope),
    memnest.search(question, scope, { tokenBudget: 400 }),
  ]);

  const system = [
    'You are a helpful assistant.',
    profile.text,
    recalled.memories.length ? 'Relevant memories:' : '',
    ...recalled.memories.map((m) => `- ${m.memory.content}`),
  ].filter(Boolean).join('\n');

  // 2. Your model, whichever it is.
  const reply = await callYourModel({ system, question });

  // 3. Hand the exchange back. Returns once indexed; facts are extracted in a job.
  await memnest.add({
    containerTag: scope.containerTag,
    customId: conversationId,     // the whole conversation is extracted as one session
    content: [
      { role: 'user', content: question },
      { role: 'assistant', content: reply },
    ],
  });

  return reply;
}

With your provider

const { text } = await generateText({ model, system, prompt: question });

Anything that takes a system prompt works: the memories are just text you prepend.

Or give the model the tools

Instead of injecting memories, expose them as tools and let the model decide. That is exactly what the MCP server does, and any MCP-capable agent framework can load it. Define them yourself in three lines each: recall(query) → memnest.search, remember(fact, supersedes?) → memnest.addMemories.

Injecting is cheaper and predictable; tools let the model ask follow-up questions of memory. Many agents do both: inject the profile every turn, expose recall for anything deeper.

Worth knowing

  • Pass a customId. One extraction call per conversation instead of one per message, and later turns resolve against earlier ones.
  • Pick a container per subject, not per conversation. user:123 or customer:acme is what memory belongs to; the conversation is the customId.
  • Budget deliberately. tokenBudget caps what recall can add to your prompt. trace.budget.used is exactly what you spent.
  • add() does not block on the model. It returns once indexed; extraction happens in a job, so your response time is unaffected.
  • Read the trace when an answer looks wrong. It names every candidate and why it was dropped, which is usually faster than guessing at the prompt.