Skip to content

Memory Is a Read-and-Write Policy

By Maksym Kuzmitskyi
6 min read
Memory Is a Read-and-Write Policy

Memory Is a Read-and-Write Policy

The user asks for a shorter answer once. The assistant stores "always be brief" and keeps applying it to explanations that now need detail.

The database has done its job. A sentence was written and retrieved. The remembered preference is still wrong for the next task.

This is why I would start an agent's memory design with admission and retrieval rules, not a choice between files and a vector database. Persistence can preserve a mistake very reliably.

The model isn't the storage system

For this discussion, external memory means information stored outside one model invocation and made available to a later invocation. The application may keep it in files, relational tables, a key-value store or another service.

The model does not automatically see that storage. Something selects records and brings them into context, the input available for the current response. A row that is never selected is persistent data, not useful context for that call.

Conversely, placing a fact in the current request doesn't guarantee it will be available tomorrow. That requires an explicit path through storage and retrieval. Anthropic's discussion of structured note-taking describes this separation: notes live outside the context window and are read back when needed.

I would keep that mechanism separate from model training. Writing a preference record doesn't adjust model parameters. It changes what the application may supply later.

Decide what kind of thing is being remembered

A conversation event, a task decision, a user preference and an authoritative product fact deserve different treatment. They have different owners and lifetimes.

You may see the labels episodic, semantic and procedural memory. Roughly, those distinguish remembered events, facts and procedures. They are useful design categories, not three special databases inside every LLM. A procedure can be a versioned instruction file; an event can be a row; a fact can be a source-linked record.

For our fictional documentation assistant, a user's preferred answer length should not become release policy. A task note should not become a permanent preference. A generated interpretation of a document should not silently replace the document as the authority.

The write path needs to preserve those differences. Otherwise the next request may receive a neat collection of sentences whose authority has been lost.

Make admission inspectable

Here is a small in-memory store for demonstrating a read-and-write policy. It outlives individual calls in the same process, but it does not survive a process restart. Durable storage requires a persistence adapter, schema validation when loading records and an appropriate retention policy.

The confirmed flag below must be supplied by trusted application logic after the appropriate user or source check. It is not a field a model can set to approve its own suggestion.

type MemoryRecord = { key: string; kind: 'preference' | 'fact'; value: string; sourceId: string; confirmed: boolean; }; class MemoryStore { private readonly records = new Map<string, MemoryRecord>(); remember(ownerId: string, record: MemoryRecord): boolean { if (!ownerId.trim() || !record.key.trim() || !record.value.trim()) return false; if (!record.confirmed || !record.sourceId.trim()) return false; const key = JSON.stringify([ownerId, record.key]); this.records.set(key, structuredClone(record)); return true; } recall(ownerId: string, key: string): MemoryRecord | undefined { const record = this.records.get(JSON.stringify([ownerId, key])); return record ? structuredClone(record) : undefined; } }

The owner identifier comes from the authenticated application, not from a model-proposed namespace. The compound key uses structured serialization so punctuation in an owner or record key cannot accidentally create the same joined key as another pair.

A returned record is copied. If a request formatter changes the copy, the store does not silently acquire a new preference. That is a small implementation detail with a fairly direct effect on what gets remembered.

Separate a suggestion from an accepted memory

This offline fixture first rejects a generated suggestion, then stores an explicitly confirmed preference:

const memory = new MemoryStore(); const suggestion: MemoryRecord = { key: 'answer-length', kind: 'preference', value: 'brief', sourceId: 'conversation-turn-12', confirmed: false, }; const rejected = memory.remember('reader-a', suggestion); const accepted = memory.remember('reader-a', { ...suggestion, sourceId: 'confirmed-preference-1', confirmed: true, }); const preference = memory.recall('reader-a', 'answer-length'); const otherReader = memory.recall('reader-b', 'answer-length');

The rejected result is false, accepted is true, and the other reader has no record. These tests establish application behavior. They do not establish that the confirmation was meaningful or that a model will apply the preference appropriately.

In a real integration I would not expose remember directly as a tool accepting arbitrary confirmed values. The application owns that transition. The useful question is which authority accepted the memory, not whether the generated JSON contains the word true.

Recall should be narrower than storage

The example retrieves one known key. That is often enough for a preference. It doesn't need semantic search.

A larger memory collection may need retrieval by task, project, time or semantic similarity. Similarity can help find candidates, but it should not decide ownership, consent or which source outranks another. Those are application rules applied independently of the ranking mechanism.

I would also avoid loading every remembered preference into every request. A language preference may matter for an answer; an unrelated project decision probably doesn't. Context selection still has a budget and a purpose even when the data is called memory.

For durable storage, I would persist the accepted record and its provenance, meaning where it came from and why it was accepted. An opaque sentence copied from a summary is difficult to correct because nobody knows which earlier claim it represents.

Useful memory remains editable

Our small store omits versioning, expiry and conflict handling. Replacing a value in a map is not a sufficient lifecycle for facts that can become stale, and I wouldn't present it as one.

Those omissions are the next design problem, not an excuse to add a more elaborate database immediately. The store could be durable and still have the same missing policy.

What I want from memory is less repeated explanation without silently accumulating new rules. A preference should help the current task, remain attributable and yield when the user corrects it. Remembering everything is not a particularly useful target.

The next step is to make correction, expiry and forgetting explicit, including the awkward case where an older value returns after its replacement expires.

Which remembered sentence would you need to trace back to its source before trusting it? That is a good candidate for a provenance field rather than another prompt instruction.

A note from the author

I’m Maksym Kuzmitskyi, a senior software developer working mainly with React and TypeScript. I build frontend applications and design workflows for working with AI in software delivery. If this article connects with something you’re working on, I’d love to hear from you.

Telegram

More than a blog post

I share frontend news and the reasoning behind it throughout the day. Pick the language that feels natural to you.

Need to discuss your project? Get in touch.