# Memory systems

Canonical URL: https://agentlearn.dev/learn/agents/memory
Author: [Hemanth HM](https://h3manth.com)
Track: agents
Reading time: 10 minutes

Memory is application-managed state. Decide what to remember, who may read it, and when it should expire.

## Three different kinds of remembering

Working state records the current run. Conversation history preserves recent interaction. Durable memory stores selected information across sessions. None is free: old facts can become false, summaries can drop important qualifiers, and stored personal data creates privacy obligations. A customer's temporary shipping address should not silently become a permanent preference.

## How it works

Store structured facts with provenance, creation time, expiration, and ownership. Separate user-provided preferences from model inferences. Retrieve only the memory needed for the current task. Provide an update and deletion path. A summary is a lossy representation, so keep the authoritative transaction record outside model-generated memory.

## A concrete example

The support assistant remembers a user's preferred language for 30 days in this fictional policy. It does not remember payment credentials. At retrieval time, it checks both owner and expiry; a vector similarity match alone would not establish either condition.

## Apply it to your assistant

Move now beyond the expiry and verify that the memory is no longer returned. Before running the exercise, predict the result. Afterward, explain which assumption changed and add one case where the system should refuse, ask for clarification, or escalate.

All exercise inputs and outputs are deterministic teaching examples. No language model is called. Run the same idea against a versioned dataset before making a production claim.


## Key takeaway

Treat memory as governed data, not an ever-growing transcript.

## JavaScript exercise: Memory systems · code experiment

Move now beyond the expiry and verify that the memory is no longer returned.

```javascript
const now = 1000;
const memories = [
  { owner: 'u1', key: 'language', value: 'Spanish', expires: 1500, source: 'user preference' },
  { owner: 'u2', key: 'language', value: 'French', expires: 2000, source: 'user preference' },
];
console.log(memories.filter(m => m.owner === 'u1' && m.expires > now));
```

## Knowledge check

What should durable memory include beyond the remembered value?

1. Ownership, provenance, and a retention policy
2. Only a high similarity score
3. Every token from all conversations

Answer: Ownership, provenance, and a retention policy

Memory needs access controls and lifecycle rules. Similarity measures relevance, not permission or freshness.

## Sources

- [Lost in the Middle: How Language Models Use Long Contexts](https://arxiv.org/abs/2307.03172) — Liu et al., 2023. Evidence that more context does not guarantee effective use of relevant information.
