Skip to content

Retrieval Modes

intermediate

Engramma 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
MethodPure cosine similarityActive Inference + semantic re-ranking
Speed2-5 ms5-12 ms
AccuracyConsistent from day 1Improves over time
Responsesimilarity scoresimilarity score (re-ranked)
Best forExact matching, prototypingProduction 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
}
Info

/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 typeRecommended modeWhy
Direct fact lookupRetrieveFast, no need for re-ranking
Chatbot context retrievalRecallBenefits from learned access patterns
Batch validation/testingRetrieveDeterministic results
Production searchRecallBest quality over time
Debugging/comparingRetrievePredictable cosine similarity

Understanding similarity scores

Both endpoints return a similarity score between 0 and 1:

Score rangeMeaningAction
0.90 - 1.00Near-exact semantic matchTrust fully
0.75 - 0.89Strong matchReliable for most use cases
0.60 - 0.74Related contentMay need user confirmation
0.40 - 0.59Weak matchLikely not what was intended
0.00 - 0.39UnrelatedDon'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:

EndpointWhat it provides
POST /v1/memory/xai/explainFull pathway analysis with head contributions and regime info
POST /v1/memory/xai/explain/layeredLayered explanation (simple/detailed/technical)
GET /v1/memory/xai/reportComprehensive XAI report
POST /v1/memory/xai/surpriseSurprise score for a pattern
GET /v1/memory/xai/headsAttention head weights
GET /v1/memory/xai/routingCurrent 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:

  1. Day 1 — Recall results are similar to retrieve (no history yet)
  2. Week 1 — The engine notices which patterns are accessed together after the same queries
  3. Week 2 — Co-access patterns start boosting related results
  4. 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/explain when you need to understand why a result was returned
  • Run consolidation periodically to accelerate the learning process
Tip

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