6.2.5 · D3AI Agents & Tool Use

Worked examples — Memory systems for agents

2,194 words10 min readBack to topic

Before any formula appears, one reminder of the two numbers everything depends on.


The scenario matrix

Every question a memory system throws at you falls into one of these cells. The worked examples below are labelled with the cell they cover.

# Cell (case class) What breaks / what to watch Example
A Fits in window (working memory, normal) budget arithmetic, positive slack Ex 1
B Overflow (working memory, degenerate) slack goes negative — must evict Ex 2
C Boundary (slack = exactly 0) zero-slack: keep or drop the last turn? Ex 3
D High similarity retrieval (cosine near 1) correct recall of a past episode Ex 4
E Orthogonal / zero similarity unrelated query, cosine = 0, must NOT recall Ex 5
F Redundancy limit (cosine > 0.95) near-duplicate → merge/delete Ex 6
G Forgetting: recency vs importance two policies disagree on same episode Ex 7
H Word problem (support agent over days) end-to-end episodic pickup Ex 8
I Exam twist (units / limiting value) tokens-per-turn → how many turns survive; Ex 9

Working-memory budget: cells A, B, C

We use one formula from the parent for all three: where is the context-window size, the system prompt, the reserved output buffer, and the history we want to carry. Slack is what is left for the new user turn. Its sign is the whole story.

Figure — Memory systems for agents

Retrieval by cosine: cells D, E, F

For episodic retrieval we embed the query and each stored episode, then rank by cosine. We use tiny 3-dimensional vectors so the arithmetic is visible.

Figure — Memory systems for agents

Forgetting policies: cell G

Figure — Memory systems for agents

Word problem end-to-end: cell H


Exam twists: cell I (units + limiting value)


Recall Self-check

A history uses 7100 tokens in an 8000 window with 1000 system + 500 buffer. Fits? ::: Slack → overflow, must evict 600+ tokens. Two embeddings have dot product 0. Recall or skip? ::: Cosine , orthogonal → skip, unrelated. Cosine of new vs old episode is 0.97. Action? ::: → merge/delete the duplicate. Recency deletes an old but starred, heavily-used episode. Right call? ::: No — run importance policy; its weighted score keeps it.


Connections: Memory systems for agents · Vector Databases · LM Context Window Management · Knowledge Graphs · Prompt Engineering · Agent Planning and Reasoning · Multi-Agent Systems