Visual walkthrough — Memory systems for agents
This page is the visual companion to Memory systems for agents (index 6.2.5). It leans on Vector Databases (where these numbers get stored) and LM Context Window Management (why we retrieve at all).
Step 1 — Turn a memory into an arrow
WHAT. We take a memory (a sentence) and represent it as a point, then draw an arrow from the origin to that point. That arrow is a vector. We write it .
WHY. Computers cannot compare sentences directly, but they are excellent at comparing arrows. If we turn every memory into an arrow, "how similar are two memories?" becomes "how much do two arrows point the same way?" — a geometry question we can measure.
PICTURE. Below, one memory becomes one blue arrow. The two numbers and are just how far right and how far up the arrow reaches.

Step 2 — Two memories, two arrows, one angle
WHAT. Now draw two memories as two arrows from the same origin: (the new query, pink) and (an old stored memory, blue). Between them sits an angle we call (the Greek letter "theta", just a name for that opening).
WHY. Here is the key insight of the whole page: similarity is direction, not length. A short arrow and a long arrow pointing the same way mean the same thing ("both about refunds"). What differs between similar and unsimilar memories is the angle between their arrows. So the quantity we actually want to measure is .
PICTURE. The pink and blue arrows, and the wedge between them. Small wedge = same topic. Wide wedge = different topics.

Step 3 — We need a number, not a picture. Enter cosine.
WHAT. We replace the angle with the number .
WHY THIS TOOL AND NOT ANOTHER? We could report directly (in degrees), but three things make better:
- It's already normalised into a tidy range — great for thresholds like the parent's .
- It is largest when arrows agree and shrinks as they diverge, so "bigger number = more similar" — the natural direction for ranking.
- As we'll see in Step 4, it can be computed directly from the arrow's numbers without ever measuring an angle with a protractor.
PICTURE. A dial: as sweeps from to , watch the value slide from to . This is the "meaning meter."

Recall Undo check
Similarity equals 1 — what is the angle, and what does it mean? ::: The angle is : the arrows point the exact same way, so the memories mean the same thing.
Step 4 — The dot product: how a computer reads the angle off the numbers
WHAT. We compute one number by multiplying the arrows coordinate-by-coordinate and summing.
WHY (deriving the bridge, not just asserting it). The dot product looks like pure arithmetic, but it secretly measures the shadow. Here is why, in three plain moves:
-
Shadow times length. Take arrow and the shadow of on it. Multiply that shadow, , by how long is, . That product is — pure geometry, all lengths and one angle.
-
The same product from coordinates. Now measure that same "shadow length" quantity from the raw numbers. Line the arrows up along the axes and it factors coordinate by coordinate: the amount of and that agree along is , and along is . Their agreement adds up to — which is exactly the dot product.
-
Two names, one quantity. Steps 1 and 2 measured the same geometric thing two different ways, so they must be equal:
That is the bridge — and now you know why it holds. Written with the dot-product symbol: The left side is easy to compute from raw numbers; the right side contains the we actually crave, tangled up with the two lengths. If we divide the lengths out, we're left with exactly . That's Step 5.
PICTURE. The dot product shown as "project the pink arrow onto the blue one (its shadow, length ), then multiply by how long is." When they align, the shadow is long → big dot product. When perpendicular, the shadow is zero → dot product .

Step 5 — Divide the lengths out: the cosine similarity formula
WHAT. Take the bridge from Step 4 and solve it for by dividing both sides by the two lengths.
WHY. Dividing by cancels the length information — exactly the thing we decided (Step 2) not to care about — leaving only the direction agreement.
Term by term:
- Numerator — the raw agreement (the dot product from Step 4).
- Denominator — the two lengths, which we cancel out so only direction survives.
PICTURE. Same two arrows as Step 2, but now both scaled to length (unit arrows). Once both are length one, the dot product is the cosine — the denominator becomes .

Step 6 — The edge cases (never leave a gap)
Retrieval breaks in subtle ways. Cover all of them.
Case A — perfect match (). New query is a copy of an old memory. Arrows overlap, , score . Retrieval returns it first.
Case B — orthogonal (). "Weather" query vs a "refund" memory. Right angle, dot product , score . The memory is correctly ignored.
Case C — opposite (). Rare in text but real for signed features: score means maximally unlike. Never retrieved.
Case D — the zero vector (degenerate!). If a memory embeds to — an empty string, or a failed embedding call — its length is , and the formula divides by zero. Undefined. Real systems guard against this: skip zero vectors, or define their similarity as . Never let this crash retrieval.
PICTURE. Four mini-panels: overlap, right-angle, opposite, and the collapsed dot (the zero vector) with a "⚠ divide-by-zero" flag.

Step 7 — From one score to a ranked retrieval
WHAT. Retrieval isn't one comparison — it's many. Embed the query once, compute cosine similarity against every stored memory, then sort from highest to lowest and keep the top .
WHY. This is the whole point of the arrow trick: comparing directions is cheap, so we can score thousands of memories fast and hand the agent only the few most relevant ones — small enough to fit the context budget from LM Context Window Management. The heavy lifting (nearest-arrow search) is what a vector database is built to do.
PICTURE. The pink query arrow surrounded by many blue memory arrows; the closest three (smallest angle, i.e. ) are highlighted — those are the top- retrieved memories.

The one-picture summary
Everything on one board: memory → arrow → angle → dot product ÷ lengths → one number in → rank → retrieve.

Recall Feynman retelling — say it plainly
We wanted an agent to find the right old memory. First, we turned every memory into an arrow made of a few numbers — that's the embedding. Then we noticed that similar memories point the same way, so what we really want is the angle between two arrows (always between and ): small angle means same topic. To get a clean number instead of an angle, we use its cosine, which runs neatly from (same direction) to (opposite). The magic trick is that a computer can get that cosine without any protractor: the dot product (multiply coordinate-by-coordinate and add) secretly measures the shadow one arrow casts on the other, which is times the length of — so dividing the dot product by both lengths leaves exactly the cosine. That single number is cosine similarity. We scored for two near-identical memories — above the parent's line — so we'd merge them. We checked the corners: aligned gives , perpendicular gives , opposite gives , and an all-zero arrow is undefined (guard against it!). Finally, we score the query against every memory, sort, and hand the agent the top (a shortlist size we choose). That's how remembering becomes geometry.