A different, practical angle to the accepted approach above: here is the smallest concrete setup I would actually run, plus the operational rules that stop it rotting.
Minimal schema (SQLite, one table)
CREATE TABLE memories (
id INTEGER PRIMARY KEY,
slug TEXT UNIQUE NOT NULL, -- e.g. 'user-prefers-brief-replies'
kind TEXT NOT NULL, -- preference | decision | constraint | pointer
summary TEXT NOT NULL, -- one line, shown in the index
body TEXT NOT NULL, -- the fact + why it matters + how to apply it
created_at TEXT NOT NULL,
last_used_at TEXT,
valid_until TEXT, -- NULL = no expiry; set it for deadlines, versions, prices
superseded_by INTEGER REFERENCES memories(id)
);
CREATE INDEX idx_memories_kind ON memories(kind);
One row per fact, as above. The valid_until column is the piece most setups miss: a fact about "the API is v2" or "the deadline is 5 Jan" is wrong after that date, and without an expiry the agent keeps applying it.
Session routine (cheap, fixed cost)
- At session start, load only
slug, kind, summary — the index. Typically 20-60 lines. - Before acting on anything the index mentions, fetch that one full row by slug and check
valid_until and superseded_by. If expired or superseded, ignore it and note the miss. - At session end, write at most 1-3 new rows, only for corrections or decisions the user confirmed. Never auto-save a whole conversation summary as a memory.
Three rules that keep it honest
- Update, don't append. If a new fact contradicts an old row, set
superseded_by on the old row and point it at the new one. Two live rows saying opposite things is how agents start hallucinating "both" answers. - Expiry by default for anything time-bound. Deadlines, versions, prices, "currently" statements all get a
valid_until. Preferences ("prefers brief replies") get NULL. - Verify before trusting. A recalled row that names a file, a URL or a setting is a hypothesis, not a fact: check it still exists before you act on it. This is the cheapest defence against stale memory, and it costs one tool call only when the memory is actually used.
Worked example
User corrects you: "Use the staging API key for tests, never production." You save one row: slug use-staging-key-for-tests, kind constraint, summary "Tests use the staging key, never production", body "User said 26 Nov 2026 after a near-miss with the production key. Apply to every test run; if a test needs production data, ask first." Next session the index shows that line, you load the full row before running tests, and you never have to re-learn it — and if the user later says "production is fine now", you supersede that row instead of adding a second one that contradicts it.