MCPcopy Index your code
hub / github.com/Harshitk-cp/engram

github.com/Harshitk-cp/engram @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
2,535 symbols 7,178 edges 200 files 712 documented · 28%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Engram

Provable memory infrastructure for AI agents.

Engram stores, retrieves, and injects typed memory into agent prompts via a simple HTTP API or an MCP server. Memories gain confidence when reinforced, lose it when contradicted, and fade when unused. Unlike a black-box memory layer, Engram records where every belief came from, lets stale beliefs decay, can erase any subject on request, and writes every change to a tamper-evident audit trail — so you can prove what your agent knows, and why.

Architecture

Engram architecture — six layers: client entry points, the Provenance Firewall, the cognitive core (semantic / episodic / procedural / working memory, contradiction detection, consolidation, metacognition), per-subject isolation, the SHA-256 tamper-evident audit chain, and PostgreSQL + pgvector storage.

Every write enters through the Provenance Firewall, flows into the cognitive core (the four memory systems plus contradiction detection, consolidation, and metacognition), stays isolated per subject, and is recorded — change by change — in a tamper-evident audit chain on top of PostgreSQL + pgvector.

Benchmarks

Engram scores 91.4% on LongMemEval — the ICLR 2025 benchmark for long-term conversational memory (500 questions over chat histories scalable past 1M tokens) — and 92.3% averaged across its six task types.

Task type Engram
Knowledge update 100.0%
Abstention 100.0%
Single-session (user fact) 98.4%
Single-session (preference) 93.3%
Single-session (assistant) 89.3%
Multi-session 90.2%
Temporal reasoning 82.3%
Overall (500 questions) 91.4%

Measured with Engram as the memory store + retrieval layer, graded by LongMemEval's standard LLM judge. Full per-task breakdown, methodology, and how to read memory benchmarks: hakuya.ai/#benchmarks · docs.hakuya.ai/benchmarks.

Quick Start

# Clone and configure
git clone https://github.com/Harshitk-cp/engram.git
cd engram
cp .env.example .env

# Start with Docker
docker compose up -d

# Run migrations
docker compose exec server /engram migrate

Then open the admin console at http://localhost:8080/console to create an account, register an agent, and mint an API key — or bootstrap headlessly:

# Bootstrap a deployment (one-time): creates a tenant + its first master key
curl -X POST http://localhost:8080/v1/setup \
  -H "X-Setup-Token: $ENGRAM_SETUP_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"org_name": "My App"}'   # → returns api_key (shown once)

Basic Usage

# 1. Register an agent
curl -X POST http://localhost:8080/v1/agents \
  -H "Authorization: Bearer $API_KEY" \
  -d '{"external_id": "agent-1", "name": "My Agent"}'

# 2. Store memory
curl -X POST http://localhost:8080/v1/memories \
  -H "Authorization: Bearer $API_KEY" \
  -d '{"agent_id": "AGENT_UUID", "content": "User prefers dark mode"}'

# 3. Recall memories
curl "http://localhost:8080/v1/memories/recall?agent_id=AGENT_UUID&query=display+preferences" \
  -H "Authorization: Bearer $API_KEY"

Use it as an MCP server

Engram ships a standalone MCP server (engram-mcp) that exposes 37 memory tools to Claude Desktop, Cursor, Windsurf, or any MCP-compatible host — remember, recall, recall_graph, get_hot_context, ingest_conversation, plus episodic, schema, anchor, metacognition, calibration, and audit tools.

{
  "mcpServers": {
    "engram": {
      "command": "engram-mcp",
      "args": ["--transport", "stdio"],
      "env": {
        "ENGRAM_API_URL": "http://localhost:8080",
        "ENGRAM_API_KEY": "mk_your_key_here",
        "ENGRAM_AGENT_ID": "your-agent-id"
      }
    }
  }
}

Full tool list and host setup: docs.hakuya.ai/guides/mcp.

Core Concepts

Memory Types

Type Description
preference User likes/dislikes, style choices
fact Information about user or context
decision Choices the user has made
constraint Hard rules or limitations

Memory Tiers

Memories are tiered by confidence for efficient retrieval:

Tier Confidence Behavior
Hot > 0.85 Auto-injected into context
Warm 0.70-0.85 Retrieved on demand
Cold 0.40-0.70 Requires explicit query
Archive < 0.40 Soft-deleted, recoverable

Belief Dynamics

  • Reinforcement: Similar statements increase confidence (+0.05)
  • Contradiction: Conflicting beliefs decrease confidence (-0.2)
  • Decay: Unused memories gradually lose confidence
  • Usage Boost: Recalled memories gain small confidence (+0.02)

Every one of these changes is written to an append-only mutation log — the why-trail behind each belief.

Multi-Subject Memory (Anchors, Sessions, Canon)

One agent can act on behalf of thousands of subjects (customers, guests, patients) with full isolation. Every memory carries a server-derived binding:

Binding Bound to Set by
private the forming agent (default — unchanged behavior) no extra ids
anchored a specific anchor (a subject) anchor_external_id
session one conversation (short-term, decays fast) session_id
canon tenant-shared org knowledge (policies, catalog) POST /v1/canon (admin)
# Store a fact ABOUT a subject (anchor auto-created on first use)
curl -X POST http://localhost:8080/v1/memories -H "Authorization: Bearer $API_KEY" \
  -d '{"agent_id":"AGENT","content":"Guest is vegetarian","anchor_external_id":"guest-42"}'

# Recall is isolated per subject — guest-99's data never leaks into guest-42's results
curl "http://localhost:8080/v1/memories/recall?agent_id=AGENT&query=diet&anchor_external_id=guest-42" \
  -H "Authorization: Bearer $API_KEY"

# GDPR per-subject erasure (crypto-shred — content unrecoverable, audit record retained)
curl -X DELETE "http://localhost:8080/v1/anchors/ANCHOR_ID?purge=true" -H "Authorization: Bearer $API_KEY"

Passing only agent_id preserves today's exact behavior. Endpoints: /v1/anchors, /v1/sessions, /v1/canon. See the Subjects, Sessions & Canon guide.

Provenance & Trust

Engram is built so you can prove what an agent knows and how it changed:

  • Provenance on every belief — source (user/agent/tool/derived), evidence type, and confidence are attached at write time.
  • Tamper-evident audit trail — every memory mutation (create / reinforce / decay / contradict / redact / release) is appended to a per-tenant SHA-256 hash chain. GET /v1/audit/verify recomputes the chain and detects any edit, insertion, deletion, or reordering. GET /v1/audit/export produces a signed NDJSON trail for compliance.
  • Provenance Firewall — untrusted memories (e.g. extracted from third-party content) are held in a quarantine queue, kept out of recall and belief logic until an admin reviews them. Release/reject decisions are themselves recorded in the audit chain.
  • Verified per-subject erasureforget_subject / crypto-shred destroys a subject's content irrecoverably while preserving the immutable audit record that the erasure happened (GDPR Article 17, EU AI Act).
# Verify the tamper-evident chain is intact
curl http://localhost:8080/v1/audit/verify -H "Authorization: Bearer $ADMIN_KEY"

# Review the Provenance Firewall queue, then release or reject
curl http://localhost:8080/v1/agents/AGENT_ID/quarantine -H "Authorization: Bearer $ADMIN_KEY"
curl -X POST http://localhost:8080/v1/quarantine/MEM_ID/release -H "Authorization: Bearer $ADMIN_KEY"

Admin Console

A React admin console is embedded in the server binary at /console. It provides:

  • Agent dashboard — knowledge health, tier distribution, learning velocity, stability
  • Memories browser — filter by tier, type, source, and binding; inspect provenance
  • Contradictions & Review Queue — resolve conflicting beliefs with audited reasons
  • Quarantine — the Provenance Firewall review surface (release / reject)
  • Audit — verify the hash chain and export the signed trail
  • Subjects — manage anchors and per-subject erasure
  • Canon — tenant-wide authoritative knowledge
  • Time Machine — replay how a memory evolved over time
  • Keys, Billing, Settings — API keys, plan/usage, and per-tenant engine tuning

Memory Systems

Engram implements four memory types inspired by cognitive science:

System Purpose API
Semantic Facts, preferences, beliefs /v1/memories
Episodic Rich experiences with context /v1/episodes
Procedural Learned skills and patterns /v1/procedures
Working Active context via spreading activation /v1/cognitive/activate

Plus Schemas for higher-order mental models (user archetypes, situation templates): /v1/schemas.

Key Features

Hybrid Retrieval (Vector + Graph)

All recall uses hybrid retrieval by default, combining semantic similarity with graph relationship traversal:

curl "http://localhost:8080/v1/memories/recall?agent_id=...&query=...&graph_weight=0.4&max_hops=2" \
  -H "Authorization: Bearer $API_KEY"

Response includes both vector and graph scores for transparency.

Conversation Extraction

Automatically extract memories from conversations:

curl -X POST http://localhost:8080/v1/memories/extract \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "agent_id": "...",
    "conversation": [
      {"role": "user", "content": "I only use open source tools"},
      {"role": "assistant", "content": "Noted, I will suggest open source alternatives."}
    ],
    "auto_store": true
  }'

Metacognition & Calibration

Self-assessment of memory quality, plus a measured calibration score (ECE / MCE / Brier):

# Reflect on memory state
curl -X POST http://localhost:8080/v1/cognitive/reflect \
  -H "Authorization: Bearer $API_KEY" -d '{"agent_id": "..."}'

# Measure how well-calibrated the agent's confidence is
curl "http://localhost:8080/v1/cognitive/calibration?agent_id=..." \
  -H "Authorization: Bearer $API_KEY"

Confidence Lifecycle

Explicit confidence management:

# Reinforce a memory
curl -X POST http://localhost:8080/v1/cognitive/confidence/reinforce \
  -d '{"memory_id": "...", "boost": 0.1}'

# Penalize a memory
curl -X POST http://localhost:8080/v1/cognitive/confidence/penalize \
  -d '{"memory_id": "...", "penalty": 0.15}'

API Reference

The full, browsable reference lives at docs.hakuya.ai/api-reference. Key families:

Auth & Keys

Method Endpoint Description
POST /v1/setup Bootstrap a deployment (X-Setup-Token)
POST /v1/keys Create API key (admin)
GET /v1/keys List keys (admin)
DELETE /v1/keys/:id Revoke key (admin)

Agents & Memories

Method Endpoint Description
POST /v1/agents Register agent
GET /v1/agents/:id/mind Get agent's complete mental state
POST /v1/memories Store memory
GET /v1/memories/recall Hybrid recall (vector + graph)
POST /v1/memories/extract Extract from conversation
GET /v1/memories/:id/mutations Provenance / why-trail
POST /v1/memories/:id/restore Un-archive a memory

Multi-Subject (Anchors, Sessions, Canon)

Method Endpoint Description
POST /v1/anchors Create a subject (anchor)
GET /v1/anchors/:id/memories A subject's durable profile
DELETE /v1/anchors/:id?purge=true GDPR per-subject erasure
POST /v1/sessions Start a conversation session
POST /v1/sessions/:id/end End session (promote recurring memory)
GET POST DELETE /v1/canon Tenant-shared knowledge

Provenance, Audit & Admin

Method Endpoint Description
GET /v1/audit/verify Verify tamper-evident hash chain
GET /v1/audit/chain Recent chain entries (seq + hash)
GET /v1/audit/export Signed NDJSON audit export
GET /v1/agents/:id/quarantine Provenance Firewall queue
POST /v1/quarantine/:id/release Release a quarantined memory
POST /v1/quarantine/:id/reject Reject a quarantined memory
POST /v1/admin/anchors/:id/shred Crypto-shred a subject
POST /v1/admin/memories/:id/redact Redact content (audited)

Cognitive, Graph & Learning

Method Endpoint Description
POST /v1/cognitive/activate Activate working memory (spreading activation)
POST /v1/cognitive/reflect Metacognitive reflection
GET /v1/cognitive/calibration Calibration metrics (ECE / MCE / Brier)
GET /v1/cognitive/health Knowledge health
GET /v1/graph/entities Extracted entities
POST /v1/graph/traverse Traverse relationship graph
POST /v1/episodes Store an episode
POST /v1/procedures/match Find matching learned skills
GET /v1/schemas List schemas (mental models)
POST /v1/feedback Record feedback signal

Billing & Settings

Method Endpoint Description
GET /v1/billing Subscription plan & usage
`POS

Extension points exported contracts — how you extend this code

Detector (Interface)
Detector classifies the relationship between two belief statements. [9 implementers]
internal/service/contradiction/detector.go
TenantStore (Interface)
(no doc) [28 implementers]
internal/domain/stores.go
SessionResolver (Interface)
SessionResolver turns a raw session token into a data-plane auth context. Implemented by the control-plane AuthService. [1 …
internal/api/middleware/auth.go
DBTX (Interface)
DBTX is the subset of pgx methods shared by *pgxpool.Pool and pgx.Tx. Stores that hold a DBTX can run either standalone
internal/store/dbtx.go
ToolHandler (FuncType)
ToolHandler is a function that handles a tool call.
mcp/server.go
AuthState (Interface)
(no doc)
console/src/auth.tsx
PolicyWeightProvider (Interface)
PolicyWeightProvider is an optional interface that PolicyEnforcer can implement to provide per-type importance weights f [1 …
internal/service/memory.go
LLMClient (Interface)
(no doc) [6 implementers]
internal/domain/stores.go

Core symbols most depended-on inside this repo

writeError
called by 466
internal/api/handlers/response.go
Get
called by 169
internal/domain/auth.go
Create
called by 142
internal/domain/auth.go
Query
called by 132
internal/store/dbtx.go
TenantFromContext
called by 96
internal/api/middleware/auth.go
Exec
called by 85
internal/store/dbtx.go
writeJSON
called by 85
internal/api/handlers/response.go
QueryRow
called by 70
internal/store/dbtx.go

Shape

Method 1,230
Function 821
Struct 389
Interface 64
TypeAlias 27
Class 2
FuncType 2

Languages

Go95%
TypeScript5%

Modules by API surface

internal/domain/stores.go177 symbols
internal/service/consolidation_test.go99 symbols
mcp/tools.go86 symbols
internal/service/memory_test.go79 symbols
internal/service/schema_test.go71 symbols
mcp/mcp_test.go60 symbols
internal/store/memory.go49 symbols
internal/service/memory.go49 symbols
internal/service/confidence_test.go47 symbols
internal/domain/graph.go46 symbols
internal/config/config.go42 symbols
internal/service/hybrid_recall_test.go35 symbols

Datastores touched

engramDatabase · 1 repos

For agents

$ claude mcp add engram \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact