The memory layer¶
Long-term memory is the difference between an assistant and a chat window, and it is almost entirely an engineering problem rather than a model problem. The model does not remember anything; the surrounding system does, and the quality of what you hand back to the model at request time decides whether the assistant feels continuous or amnesiac.
Not the context window¶
The common first attempt is to keep stuffing the transcript into the prompt. That fails in three ways at once: the window has a hard ceiling, every token is paid for on every turn, and — least obvious, most damaging — retrieval quality drops as the prompt grows, because the relevant sentence is now buried in fifty thousand tokens of small talk.
What works instead is a store you query, plus a selection step that assembles maybe ten kilobytes of relevant material per request. The context window becomes a working surface, not a filing cabinet.
Four tables¶
Everything lives in one SQLite file. The shape that survived six months of edits:
conversation_chunks — the raw transcript, chunked, with a full-text index and an
embedding column. After six months this is roughly seven thousand chunks and a bit over a
hundred megabytes. It is the only table that grows without bound, and it is the one you
search when someone asks "what exactly did I say about X".
claims — short subject–predicate–object facts, each with a pointer to its evidence:
a file and a line number, a message id, a URL. The evidence pointer is the part people
skip and the part that matters. A fact without provenance is a rumour the assistant will
repeat with confidence, and a confidently wrong assistant is worse than a forgetful one.
lessons — what went wrong and what the corrected behaviour is, ranked by importance.
Written when a correction happens, read before answering. This is the table that makes the
assistant stop repeating a specific mistake, which users perceive as "it learned".
state — a small set of slow-moving dials with a decay schedule, so a burst of
interest in one topic does not permanently distort behaviour. Decay is what keeps state
from becoming a second, badly-maintained personality file.
Retrieval: hybrid, and the ordering matters¶
Full-text search alone misses paraphrase. Vector search alone confidently returns things that are topically near and factually irrelevant. Running both and merging works, but the merge is where the tuning lives:
- FTS gives exact-token recall — names, error strings, file paths. Cheap, and it is the
only thing that reliably finds
ERR_MODULE_NOT_FOUNDsix weeks later. - Vector search gives paraphrase recall.
- Deduplicate by source chunk, then rank, then truncate to a token budget you decide in advance — not "whatever fits".
The single largest quality jump I measured did not come from a better embedding model. It came from chunking on semantic boundaries rather than fixed length, so a chunk stops being half of one thought plus half of another.
Embeddings: local, deliberately¶
Vectors are the easiest thing in the world to outsource to an API, and I moved mine the other way — to a multilingual model at 768 dimensions running on the same box. Two reasons, in the order they actually mattered:
Resilience. An API key expired at three in the morning and retrieval went down with it. A local model means the memory layer keeps working when billing does not.
Exposure. Embedding every chunk of every conversation through a third party means that third party sees every chunk of every conversation — including the ones the model is never asked about.
The cost is a few hundred megabytes of RAM and a slower backfill. Both are affordable; a memory layer that goes dark at 03:00 is not.
Writing memory: the rule that made it work¶
Extraction on every message produces a store full of noise ("the user said hello"). The rule that fixed it was mechanical rather than clever: write a fact only when it has located evidence, and make re-running the writer with the same subject and predicate update the existing row instead of appending a near-duplicate. Deterministic ids turn the memory writer into something you can safely call twice.
More on the surrounding architecture — scheduler, voice, multi-agent dispatch — at avelina.ai.