Interactive agent-memory laboratory

Memory becomes useful when meaning has a neighborhood.

Long-term agent memory is not only a storage problem. It is an organization problem. Cluster semantic vectors, move a retrieval query, and see how normalization, outliers, and the chosen number of groups change what an assistant recalls.

Embeddings encode relative geometryK-means minimizes within-cluster distanceNormalization changes the objectiveQueries route toward centroidsOutliers can drag a clusterEmbeddings encode relative geometryK-means minimizes within-cluster distanceNormalization changes the objectiveQueries route toward centroidsOutliers can drag a cluster

Partition a memory field, then ask it a question

Each sphere is a stored memory vector. Stars are learned centroids. The lime query travels to its nearest centroid, narrowing the candidate set before detailed similarity ranking.

workpersonaltravelquery
coarse3
tight25%
none2
Within-cluster inertia0.00
Topic purity0%
Routed cluster-
Candidate memories0

Clustering compresses a field of memories into a smaller map of neighborhoods.

The optimization objective

K-means alternates between two operations: assign each point to its nearest centroid, then replace each centroid with the mean of its assigned points. Each operation cannot increase the within-cluster sum of squared Euclidean distances, so the process converges to a local solution.

inertia = sum ||memory - assigned centroid||^2
embednormalizeclusterroutererank

Why normalize?

Embedding magnitude may reflect factors other than semantic direction. L2 normalization places vectors on a unit sphere, making Euclidean distance closely related to cosine similarity.

Choose K

Too few clusters merge distinct contexts. Too many fragment one topic into brittle micro-groups. Evaluate retrieval quality, stability, and operational cost rather than trusting one elbow plot.

Handle outliers

K-means must assign every point. A distant or corrupted memory can pull a centroid away from its useful neighborhood. Robust preprocessing or density methods can isolate noise.

Do not stop at routing

A centroid is a coarse index. After routing, compare the query with individual memories, apply recency and permission filters, then rerank the survivors.

What the metrics do and do not tell you

Inertia

Measures compactness in the current coordinate system. It always falls as K increases, so a lower value alone does not prove a more meaningful memory taxonomy.

Geometry, not semantics

Topic purity

Compares each learned cluster with hidden teaching labels. Real agent memories rarely have perfect labels, but sampled human judgments can estimate whether neighborhoods remain coherent.

Interpretability check

Candidate count

Shows how much work coarse routing saves before detailed retrieval. Tiny candidate sets are fast but risk missing relevant cross-cluster memories.

Recall-cost tradeoff
A practical memory pipeline

Separate organization from final relevance.

Clustering is most useful as one stage in a layered retrieval design. It provides structure and reduces search cost, but should not become the only authority over what the agent remembers.

Prepare

Chunk memories around complete events or decisions. Attach timestamps, people, permissions, provenance, and task identifiers outside the embedding. Remove duplicates and redact secrets before indexing.

  • Keep structured metadata searchable.
  • Version the embedding model.
  • Separate user-private and shared spaces.

Organize

Fit clusters on a representative sample, inspect nearest memories around each centroid, and name coherent groups only after looking at their contents. Refit when the embedding model or memory distribution changes.

  • Test several K values.
  • Measure assignment stability.
  • Watch for dominant and empty clusters.

Retrieve and verify

Route the query to one or several clusters, perform exact vector similarity within them, then apply metadata filters and reranking. Preserve source links so the agent can distinguish recalled evidence from generated interpretation.

Questions worth asking

Does K-means discover true topics?

No. It finds compact groups under Euclidean distance. Whether those groups correspond to useful topics depends on the embedding space, preprocessing, K, initialization, and memory distribution.

Why can two runs differ?

K-means converges to a local minimum determined partly by initial centroids. K-means++ initialization and multiple restarts reduce, but do not eliminate, this sensitivity.

When should I use a density method?

Density-based clustering is useful when groups have irregular shapes, the number of groups is unknown, or noise should remain unassigned. Its density thresholds introduce different assumptions.

Should an agent expose cluster names to users?

Only as an aid, not a fact. Names are interpretations layered over geometry. Let users inspect, rename, merge, split, and delete memory groups.

Give an agent memory it can inspect, route, and forget.

Explore SuperPowers workflows for message-based assistants, computer-use caching, and agents that convert explicit user goals into visible results.

Explore SuperPowersText-message AI assistantComputer-use cache