Memory ArchitecturePipeline
Relevance Scoring
The composite scoring formula combining cosine similarity, importance, and recency decay
Memory retrieval uses a weighted composite score combining three signals: semantic similarity, importance rating, and recency decay.
Scoring Formula
score = 0.5 × cosine_similarity
+ 0.3 × importance
+ 0.2 × recency_decayWhere:
- cosine_similarity =
1 - (embedding <=> query_vector)(pgvector cosine distance) - importance =
importancecolumn value (0.0–1.0) - recency_decay =
exp(-0.05 × days_since_last_access)
Component Weights
| Signal | Weight | Purpose |
|---|---|---|
| Cosine similarity | 0.5 | Semantic relevance to the query |
| Importance | 0.3 | User/system-assigned priority |
| Recency decay | 0.2 | Freshness preference with exponential decay |
Full SQL Query
SELECT
id, agent_id, content, memory_type, importance,
created_at, last_accessed_at,
(0.5 * (1 - (embedding <=> $query::vector)))
+ (0.3 * importance)
+ (0.2 * exp(-0.05 * EXTRACT(EPOCH FROM (now() - last_accessed_at)) / 86400))
AS score
FROM agent_memories
WHERE agent_id = $agent_id
AND embedding IS NOT NULL
AND status = 'active'
ORDER BY score DESC
LIMIT $top_kRecency Decay Curve
The decay function exp(-0.05 × days) produces:
| Days since access | Decay value |
|---|---|
| 0 (today) | 1.000 |
| 1 | 0.951 |
| 7 | 0.705 |
| 14 | 0.497 |
| 30 | 0.223 |
| 60 | 0.050 |
Memories accessed within the last week retain strong recency scores. After 30 days, recency contributes minimally.
pgvector Indexes
The schema uses HNSW (Hierarchical Navigable Small World) indexes for approximate nearest neighbor search:
CREATE INDEX ON agent_memories
USING hnsw (embedding vector_cosine_ops);
CREATE INDEX ON team_memories
USING hnsw (embedding vector_cosine_ops);The <=> operator computes cosine distance (not similarity), so the formula uses 1 - distance to get similarity.
Deduplication vs. Search
| Operation | Threshold | Purpose |
|---|---|---|
| Dedup check | 0.92 | Prevent near-identical memories |
| Search retrieval | N/A (scored) | Rank by composite relevance |
| Consolidation clustering | 0.80 | Group related memories |
See operations/configuration for all threshold values.
Related Pages
- schema/types — Table schemas with embedding columns
- pipeline/context-assembly — How search results feed into context
- operations/configuration — Threshold values and model settings