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
- user turna question arrives
- recallprofile + facts for this turn, within a budget
- your modelany provider; facts go in the system prompt
- replygrounded in what is true now
- 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.
const completion = await openai.chat.completions.create({ model: 'gpt-4.1-mini', messages: [{ role: 'system', content: system }, { role: 'user', content: question }], }); const reply = completion.choices[0].message.content;
const message = await anthropic.messages.create({ model: 'claude-sonnet-5', max_tokens: 1024, system, messages: [{ role: 'user', content: question }], });
To let Claude manage memory itself rather than you injecting it, give it the MCP server and it will call recall and remember as tools.
Ollama, LM Studio, vLLM and other OpenAI-compatible servers work the same way, both for your own calls and for Memnest's extraction. Nothing has to leave the machine:
export MEMNEST_PROVIDER=ollama MEMNEST_COMPLETION_MODEL=llama3.1:8b
See Models and providers.
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:123orcustomer:acmeis what memory belongs to; the conversation is thecustomId. - Budget deliberately.
tokenBudgetcaps what recall can add to your prompt.trace.budget.usedis 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.