recall() or context(), MemLayer doesn’t just match keywords — it ranks every candidate memory using a hybrid score that combines three independent signals into a single float between 0.0 and 1.0. The memory most likely to be useful to your agent right now rises to the top, even if it wasn’t the closest semantic match in isolation.
The Formula
MemLayer computes a final score for every memory before returning results:- Cosine (70%) — how semantically close the memory is to your query
- Recency (20%) — how recently the memory was stored
- Importance (10%) — how important you rated the memory when storing it
Component Breakdown
| Component | Weight | Range | What drives it |
|---|---|---|---|
| Cosine similarity | 70% | 0.0 – 1.0 | Embedding distance between query and memory |
| Recency | 20% | 0.0 – 1.0 | Exponential decay based on time since storage |
| Importance | 10% | 0.0 – 1.0 | The importance float you set when storing |
Cosine Similarity (70%)
Cosine similarity measures the semantic distance between your query embedding and the stored memory embedding. MemLayer uses dense vector representations, so matches are language-agnostic and concept-aware — not keyword-dependent. A query like"where does the user live?" will match a memory containing "User is based in Lagos, Nigeria" even though none of the query words appear in the memory text. This makes recall robust across paraphrases, different languages, and varying levels of detail.
Memories with a cosine score below the min_score threshold are excluded from results entirely before the hybrid score is applied.
Recency (20%)
Recency is computed using exponential decay based on how many days have passed since the memory was stored. A memory stored right now scores1.0, and the score decreases smoothly as the memory ages. This means your agent naturally surfaces recent context without you having to filter by date manually.
Importance (10%)
Theimportance field is a float between 0.0 and 1.0 that you set when storing a memory. It defaults to 0.5. Use higher values for memories that carry lasting strategic weight — confirmed user intent, key preferences, or critical constraints — so they continue to surface even as they age.
Inspecting score_detail
Every memory returned byrecall() includes a score_detail object with the individual component scores alongside the final hybrid score. Use it to understand exactly why a memory ranked where it did.
score_detail is invaluable when tuning min_score or debugging unexpected recall results — you can see immediately which component is dragging a memory down.
Tuning min_score
Themin_score parameter sets the minimum final_score a memory must achieve to appear in results. Adjust it based on how strict you need retrieval to be:
Setting
min_score=0.0 during development lets you inspect all candidate memories and their score_detail values, making it easy to calibrate the right threshold for your use case before going to production.