Memory for AI agents
Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.
A model answers best from what it can look up at the moment it answers:
documents it cites, what it knows about the person it talks to. nucleus
keeps that knowledge in the database itself (PLAN.md §M14.24), so it can
be updated, permissioned, audited and erased like any other data. This
guide covers one assistant, start to finish, in SQL, then from Python,
JavaScript and an agent over MCP. Runnable versions:
examples/python/memory.py and
examples/js/memory.ts. The full reference
is DOCS.md §8.26–§8.30.
nucleus server ./assistant --history 1h # citations read back for an hour
A knowledge base it cites
Documents, kept cut into chunks, each chunk with a vector and a text index, searched by both at once:
CREATE MODEL bge USING local WITH (huggingface = 'BAAI/bge-small-en-v1.5');
CREATE TABLE kb_docs (id int PRIMARY KEY, title text, body text);
CREATE TABLE kb_chunks CHUNKS OF kb_docs (body) BY markdown SIZE 200 USING bge;
ALTER TABLE kb_chunks ADD COLUMN v vector GENERATED BY EMBEDDING (chunk) USING bge;
CREATE INDEX ON kb_chunks USING text (chunk);
CREATE INDEX ON kb_chunks USING hnsw (v vector_cosine_ops);
SELECT id, position, chunk, snapshot_commit() AS as_of
FROM kb_chunks
ORDER BY rrf(score(chunk @@ $1), v <=> embed($1)) DESC
LIMIT 5;
The chunks follow every write to kb_docs in the same transaction (a
chunk whose text did not change keeps its vector); vectors are made in
the background and retried if a provider is down. Any model works:
local runs inside the database, openai, cohere and voyage call
out, hashing needs nothing (lexical, for tests).
A citation is (id, position, as_of). However the document changes
afterwards, what was cited reads back exactly, for as long as the
history reaches (--history; 72000 past it):
SELECT chunk FROM kb_chunks AS OF COMMIT 1234 WHERE id = 1 AND position = 1;
The whole database can be read as it was, too: BEGIN READ ONLY AS OF TIMESTAMP now() - interval '10 minutes'.
A memory for each user
CREATE MEMORY STORE assistant_memory USING bge;
REMEMBER 'Ann prefers short answers' FOR 'ann' IN assistant_memory AS 'preference';
REMEMBER 'Ann moved to Pécs' FOR 'ann' IN assistant_memory REPLACING '<old id>';
SELECT id, content, score, as_of FROM recall('assistant_memory', 'ann', 'how should I reply', 5);
REMEMBER merges a fact it already holds (seen counts up) and closes a
replaced one (kept as history, not deleted); EXPIRES now() + interval '30 days' forgets one by itself. recall reads only the subject’s
current memories — words and meaning fused — under a millisecond among
100 000; several questions at once go through LATERAL.
Who sees what. recall runs with the caller’s privileges and row
security: grant an application role SELECT on the store, or give each
tenant a policy (CREATE POLICY … USING (tenant = current_setting( 'app.tenant'))) — every search path, text statistics included, stays
within it.
What a reply read
SET request_id = 'reply-42';
-- … the recalls and searches that build the reply …
RESET request_id;
SELECT as_of, tables, ids, parameters, statement
FROM nc_catalog.reads WHERE request_id = 'reply-42';
Each query under a request id is recorded (in the audit log’s hash
chain) with the commit it read and the ids it returned: why a model said
what it said can be answered later, and reproduced with AS OF COMMIT.
Erasure on request
Mark the columns that say whose a row is — a memory store’s subject is
marked already — and erase a person everywhere at once:
ALTER TABLE tickets SET SUBJECT (customer);
FORGET SUBJECT 'ann'; -- erasure | rows | scrubbed | found
All of the subject’s rows or none (row security hiding one refuses it);
foreign keys followed; then every file is scrubbed of them (old
versions, index pages, free pages, the log, audit details, read
records) and searched — found = 0 is the proof. nc_catalog.erasures
keeps the record, without the subject’s value.
Backups that forget too
BACKUP TO '/backups/assistant'; -- the first: all of it; the next ones: what changed
nucleus restore /backups/assistant ./restored --subject-keys ./assistant.subject-keys
nucleus restore /backups/assistant ./yesterday --at '2026-11-14 18:00:00+01'
# or on a schedule, encrypted: the server backs up every day itself
nucleus server ./assistant --backup-to /backups/assistant --backup-every 1d \
--backup-keep 4 --backup-key ./backup.key # openssl rand -hex 32 > backup.key
A backup is taken online, without index pages (a restore rebuilds
them); a repository’s first is a full one, each next one holds only
what changed since the one before, so they can be taken often. Each row of a subject is sealed under that subject’s own key,
kept in <dir>.subject-keys — keep that file, and back it up, apart
from the backups. FORGET SUBJECT destroys the key, so every backup
taken before then restores without the subject. With a backup key the
whole backup is encrypted too (the store keeping it sees nothing of the
memories); restoring then needs --backup-key as well.
From Python and JavaScript
db = nucleus.connect("nucleus://nucleus:[email protected]:20035/nucleus") # or "file:app.db"
db.remember("assistant_memory", "ann", "Ann prefers short answers", kind="preference")
memories = db.recall("assistant_memory", "ann", "how should I reply", k=5) # dicts, with as_of
erasure, rows, scrubbed, found = db.forget_subject("ann")
const db = await connect("nucleus://nucleus:[email protected]:20035/nucleus"); // or "file:app.db"
await db.remember("assistant_memory", "ann", "Ann prefers short answers", { kind: "preference" });
const memories = await db.recall("assistant_memory", "ann", "how should I reply", 5);
const { erasure, rows, found } = await db.forgetSubject("ann");
The same programs run on a database inside the process — file:app.db
or :memory: — by changing only the string.
From an agent (MCP)
{ "mcpServers": { "nucleus": {
"command": "nucleus",
"args": ["mcp", "-h", "127.0.0.1:20035", "--memory", "--allow-erasure"] } } }
With --memory the agent gets remember, recall (with as_of),
forget (one memory) and cite (memories as they were at a commit) —
without the general write tool; --allow-erasure adds forget_subject,
for a person’s request to be forgotten. The read tools (query,
describe_table, …) are always there, read-only.