Retrieval Modes
intermediateEngramma offers two retrieval modes — Retrieve (cosine similarity) and Recall (Active Inference) — plus advanced XAI for understanding results.
Two modes, different strengths
When you query your memories, Engramma offers two retrieval endpoints with fundamentally different approaches:
/v1/memory/text/retrieve | /v1/memory/text/recall | |
|---|---|---|
| Method | Pure cosine similarity | Active Inference + semantic re-ranking |
| Speed | 2-5 ms | 5-12 ms |
| Accuracy | Consistent from day 1 | Improves over time |
| Response | similarity score | similarity score (re-ranked) |
| Best for | Exact matching, prototyping | Production retrieval, chatbots |
Retrieve: cosine similarity
The simplest retrieval mode. Your query is embedded and compared against all stored patterns using cosine similarity. Results are ranked by geometric distance in embedding space.
import requests, os
API = "https://api.engramma-memory.com"
HEADERS = {
"X-API-Key": os.environ["ENGRAMMA_API_KEY"],
"Content-Type": "application/json"
}
resp = requests.post(f"{API}/v1/memory/text/retrieve", headers=HEADERS, json={
"query": "What port does the deployment use?",
"top_k": 5
})
for r in resp.json()["results"]:
print(f"[{r['similarity']:.2f}] {r['text']}")Response:
{
"results": [
{
"text": "The deployment runs on port 8080",
"metadata": {"category": "ops"},
"similarity": 0.92,
"pattern_id": "pat_a7f2c1"
}
],
"latency_ms": 2.3
}
Recall: Active Inference
The intelligent retrieval mode. Uses the same cosine similarity as a starting point, but then applies Active Inference with semantic re-ranking based on co-access patterns. Results improve as the engine learns which patterns are accessed together.
resp = requests.post(f"{API}/v1/memory/text/recall", headers=HEADERS, json={
"query": "What port does the deployment use?",
"top_k": 5
})
for r in resp.json()["results"]:
print(f"[{r['similarity']:.2f}] {r['text']}")Response:
{
"results": [
{
"text": "The deployment runs on port 8080",
"metadata": {"category": "ops"},
"similarity": 0.96,
"pattern_id": "pat_a7f2c1"
}
],
"info": {},
"latency_ms": 8.1
}
/text/recall returns higher similarity scores than /text/retrieve because it applies learned access patterns for re-ranking. Use /text/retrieve for simple similarity search, and /text/recall when you want the full cognitive engine.
When to use which
| Query type | Recommended mode | Why |
|---|---|---|
| Direct fact lookup | Retrieve | Fast, no need for re-ranking |
| Chatbot context retrieval | Recall | Benefits from learned access patterns |
| Batch validation/testing | Retrieve | Deterministic results |
| Production search | Recall | Best quality over time |
| Debugging/comparing | Retrieve | Predictable cosine similarity |
Understanding similarity scores
Both endpoints return a similarity score between 0 and 1:
| Score range | Meaning | Action |
|---|---|---|
| 0.90 - 1.00 | Near-exact semantic match | Trust fully |
| 0.75 - 0.89 | Strong match | Reliable for most use cases |
| 0.60 - 0.74 | Related content | May need user confirmation |
| 0.40 - 0.59 | Weak match | Likely not what was intended |
| 0.00 - 0.39 | Unrelated | Don't show to users |
The XAI layer: explaining results
For any retrieval, you can get a human-readable explanation via /v1/memory/text/explain:
resp = requests.post(f"{API}/v1/memory/text/explain", headers=HEADERS, json={
"query": "What port does the deployment use?"
})
expl = resp.json()
print(f"Method: {expl['method']}")
print(f"Confidence: {expl['confidence']}")
print(f"Explanation: {expl['explanation']}")Response:
{
"explanation": "High-confidence match (0.92) via cosine similarity. The stored fact 'The deployment runs on port 8080' directly answers the query.",
"method": "cosine_similarity",
"confidence": 0.92
}
Advanced XAI endpoints
For deeper introspection, Engramma provides dedicated XAI endpoints that work at the embedding level:
| Endpoint | What it provides |
|---|---|
POST /v1/memory/xai/explain | Full pathway analysis with head contributions and regime info |
POST /v1/memory/xai/explain/layered | Layered explanation (simple/detailed/technical) |
GET /v1/memory/xai/report | Comprehensive XAI report |
POST /v1/memory/xai/surprise | Surprise score for a pattern |
GET /v1/memory/xai/heads | Attention head weights |
GET /v1/memory/xai/routing | Current routing decisions |
These endpoints accept raw embeddings and provide detailed internal analysis — useful for debugging or compliance audits.
How recall improves over time
The Active Inference engine learns from how you use it:
- Day 1 — Recall results are similar to retrieve (no history yet)
- Week 1 — The engine notices which patterns are accessed together after the same queries
- Week 2 — Co-access patterns start boosting related results
- Month 1 — The engine can surface relevant results that pure cosine similarity would miss
This improvement happens automatically. Consolidation cycles (/v1/memory/consolidation/sleep) accelerate the process by reinforcing frequently-validated access patterns.
Practical tips
- Start with recall for production — even on day 1 it returns at least as good as retrieve
- Use retrieve for testing — deterministic cosine similarity makes debugging easier
- Check similarity > 0.7 before showing results to users
- Use
/text/explainwhen you need to understand why a result was returned - Run consolidation periodically to accelerate the learning process
The /text/explain endpoint is ideal for debugging low-quality results. It tells you the retrieval method and confidence, helping you understand whether the issue is poor embeddings, missing data, or something else.
Next steps
- Causal Reasoning — How memories form cause-effect relationships
- Explainability — Deep dive into XAI endpoints
- How It Works — The full 10-phase cycle