Agent memory
Six sessions and the probes between them: transcript, scratchpad, episodic and semantic memory, with forgetting and consolidation, as facts recalled against tokens spent.
Loading the animation…
Concept
The window is gone when a session ends. Whatever the agent should know next time has to be written somewhere and read back. The animation runs 6 sessions of three research questions each. At the start of every later session the agent is probed: each question it researched comes back once, one to three sessions later, and a few neighbour questions ask about something that was in a chunk it read but was never asked. A final session holds probes only. That makes 22 probes: 18 repeats and 4 neighbours. A probe is recalled when the memory the agent puts in that call holds the answer.
- None: nothing carries over, so nothing is recalled.
- Transcript: keep every read and load it all. Every probe is recalled (22 of 22), but each call carries the whole history: 67,498 memory tokens read in total, and growing with every session.
- Scratchpad: a notes file, one "question → answer" line per question answered, loaded whole. It is small (374 tokens at the end) and recalls every repeat (18 of 18), but no neighbour: notes keep the answers to the questions that were asked, not the text around them. Capped at 128 tokens, the oldest notes are forgotten and recall falls to 14.
- Episodic: every read is an episode with a time and an importance rating. At a probe the top three by recency + importance + relevance go in the call, the scoring of Generative Agents (Park et al., arXiv:2304.03442), whose recency decays by 0.995 per hour since an episode was last retrieved. A chunk read twice is consolidated into one episode, so the store holds 18 episodes for 22 reads. It recalls 19 of 22 for 12,515 tokens. Kept to at most eight episodes (forgetting the lowest recency + importance), it recalls 12.
- Semantic: a scripted extractor turns each read into facts: the sentence that answered the question and the chunk's first sentence. At a probe the three facts closest to the question by cosine go in the call. It recalls 19 of 22, as many as the episodes, for 2,231 tokens, under a fifth as many, because a fact is a sentence and an episode is a chunk. The price is paid at write time: extraction reads every chunk once more (5,412 tokens of model calls).
The scatter under the animation puts every policy on one chart, with (items per probe) swept for the two retrieved memories: semantic memory with recalls 16 for 829 tokens; episodic with recalls 20 for 21,221. Recall rises with tokens read, and the cheapest point at a given recall is the memory that stores the smallest unit that still holds the answer.
Maths
For episodic memory each component is min-max normalised over the store at the time of the probe, , and the three are added with equal weights, as in the paper. Recency is with the hours since the episode was last retrieved, so retrieving a memory refreshes it: memories that keep being useful stay near the top. Forgetting with a cap of items evicts until the store fits.
A probe's read cost is the size of what goes in the call: the whole store for a transcript or a scratchpad ( per probe), items for retrieved memories (). Over probes the transcript's cost grows with history, which is why its total, 67,498 tokens, dwarfs the others.
Code
Episodic retrieval, cut from the vendored engine (memoryRun): recency, importance and relevance, each normalised, added, and the top taken.
const rel = store.map((it) => cosine(qv[p.q]!, cv[it.ref]!));
const rec = norm(store.map((it) => recency(now - it.t)));
const imp = norm(store.map((it) => it.imp));
const rn = norm(rel);
sc = store.map((_, i) => rec[i]! + imp[i]! + rn[i]!);