engram

Architecture

Overview

Engram is an event-sourced memory system for AI agents. It uses a two-layer design where the episode log is the source of truth and any derived structures (graphs, indices) are materialized views that can be rebuilt from scratch.

Problem Statement

Existing AI memory systems (Graphiti, Memento, etc.) couple write reliability to LLM API availability by performing entity extraction at write time. This creates fragile systems where:

Engram solves this by keeping the episode store as the foundation — no LLM in the write path. Every write succeeds if the database is up.

Layer 1: Episode Store (Core)

Data Model

INSTALL vss;
LOAD vss;

CREATE TABLE episodes (
    id VARCHAR PRIMARY KEY,
    content TEXT NOT NULL,
    name VARCHAR,
    source VARCHAR NOT NULL,
    source_model VARCHAR,
    source_description TEXT,
    group_id VARCHAR DEFAULT 'default',
    tags VARCHAR[],
    embedding FLOAT[768],
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    valid_at TIMESTAMP,
    expired_at TIMESTAMP,
    metadata JSON
);

-- Vector similarity index (HNSW)
CREATE INDEX idx_episodes_embedding ON episodes USING HNSW (embedding);

-- Standard indices for common query patterns
CREATE INDEX idx_episodes_created_at ON episodes (created_at DESC);
CREATE INDEX idx_episodes_group_id ON episodes (group_id);
CREATE INDEX idx_episodes_valid_at ON episodes (valid_at);
CREATE INDEX idx_episodes_expired_at ON episodes (expired_at);
CREATE INDEX idx_episodes_source ON episodes (source);

Write Path

  1. Receive episode text + metadata
  2. Generate embedding via the configured OpenAI-compatible server (e.g., nomic-embed-text, 768 dimensions)
  3. Insert into DuckDB
  4. If embedding service is unavailable: insert with NULL embedding, queue for retry
  5. Return success to caller immediately

Three search modes, selectable via the search_mode parameter:

All modes support additional filters:

When a search query is received in vector or hybrid mode, the query text is embedded and results are ranked by array_cosine_similarity(). If embedding generation fails (e.g., the embeddings server is down), vector search falls back to chronological ordering and hybrid degrades to keyword-only.

Embedding provenance and re-embedding

Every stored vector is stamped with the model that produced it (embedding_model column on episodes, entities, and knowledge). A row is stale when its embedding is missing (the embedding server was down at write time) or was produced by a model other than the one currently configured — both silently degrade vector search because different models occupy different vector spaces.

Engram warns at startup when stale embeddings exist and exposes POST /api/v1/admin/reembed to regenerate them asynchronously in place. Embeddings are pure derived data, so the pass never touches episode content; it is idempotent and resumable (keyset pagination, per-row failures are skipped and retried on the next run). {"force": true} regenerates every row regardless of provenance. Progress is observable via GET /api/v1/admin/reembed and /api/v1/status.

Layer 2: Derived Knowledge Graph (Future)

A periodic batch process that reads episodes and builds entity/relationship structures. Not currently implemented. The episode store alone with semantic search provides the majority of the value.

When built:

MCP Server

Go service using the official MCP SDK, exposing tools over SSE:

Tool Description LLM Required
add_memory Store a new episode No
search Semantic + temporal + tag search No
get_episodes Retrieve by time range, source, or group No
update_episode Modify metadata/tags/expiration No
get_status Health check No

Episodes can be marked as expired but not deleted. This prevents accidental memory loss.

Transport

Engram uses a server-first architecture to avoid DuckDB’s single-writer file lock:

SSE is the primary transport. Clients that support it (Cursor, Claude Code) connect directly to http://localhost:3490/mcp/sse. The stdio proxy is a compatibility shim for clients that only speak stdio.

Infrastructure

Deployment Options

Native binary: Single executable + DuckDB file. Run engram serve as a background service. See MCP Integration Guide for platform-specific instructions.

Docker container: Multi-stage build using Debian Bookworm (glibc compatibility). See the Dockerfile.

Kubernetes: Deployment with PersistentVolume for the .duckdb file. Requires ingress configuration for SSE support (no buffering, long timeouts).

Design Principles

  1. Writes never fail (if the database is up)
  2. No LLM in the write path — embeddings only, and those are retryable
  3. Episode log is source of truth — everything else is derived
  4. Rebuild over repair — if derived data is wrong, rebuild from episodes
  5. Simple over clever — vector search covers 80% of use cases without a graph
  6. Multi-tenant by default — group_id supports multiple users/contexts
  7. Observable — every episode records which client and model wrote it

Current Limitations

Future Roadmap