Skip to main content

Memory traces for AI agents - Think like human

Project description

engram

Memory traces for AI agents — Think like humans.

PyPI Tests Python License

Dual-memory AI system combining episodic (vector) + semantic (graph) memory with LLM reasoning. Entity-gated ingestion ensures only meaningful data is stored. Enterprise-ready with multi-tenancy, auth, caching, observability, and Docker deployment.

Exposes CLI, MCP (Claude Code / OpenClaw), HTTP API (/api/v1/), and WebSocket (/ws) interfaces.

pip install engram-mem

Features

Core Memory

  • Episodic Memory — Qdrant vector store (embedded or server), semantic similarity search, Ebbinghaus decay, activation-based scoring, topic-key upsert
  • Semantic Graph — NetworkX MultiDiGraph, typed entities and relationships, SQLite (default) or PostgreSQL backend, weighted edges
  • Reasoning Engine — LLM synthesis (Gemini via litellm), dual-memory context fusion, constitution-guarded prompts
  • Recall Pipeline — Query decision, temporal+pronoun entity resolution, parallel multi-source search, dedup, composite scoring
  • Entity-Gated Ingestion — Only stores messages with extracted entities; skips noise (system prompts, trivial messages)
  • Auto Memory — Detect and persist save-worthy messages automatically, poisoning guard for injection prevention
  • Meeting Ledger — Structured meeting records with decisions, action items, attendees, topics
  • Feedback Loop — Confidence scoring (+0.15/-0.2), importance adjustment, auto-delete on 3x negative feedback
  • Graph Visualization — Interactive entity relationship explorer with dark theme, search, click-to-inspect (vis-network)

Intelligence Layer

  • Temporal Resolution — 28 Vietnamese+English date patterns resolve "hom nay/yesterday" to ISO dates before storing
  • Pronoun Resolution — "anh ay/he/she" to named entity from graph context, LLM-based fallback
  • Fusion Formatter — Group recall results by type [preference]/[fact]/[lesson] for structured LLM context
  • Memory Consolidation — Jaccard clustering + LLM summarization reduces redundancy

Enterprise

  • Multi-Surface — CLI (Typer), MCP Server (stdio), HTTP API (FastAPI), WebSocket, Web UI
  • Authentication — JWT + API keys with RBAC (ADMIN, AGENT, READER), optional, disabled by default
  • Multi-Tenancy — Isolated per-tenant stores, contextvar propagation, row-level PostgreSQL isolation
  • Caching — Redis-backed result caching with per-endpoint TTLs
  • Rate Limiting — Sliding-window per-tenant limits, fail_open option
  • Audit Trail — Structured before/after JSONL log for every episodic mutation
  • Resource Tiers — 4-tier LLM degradation (FULL > STANDARD > BASIC > READONLY), 60s auto-recovery
  • Data Constitution — 3-law LLM governance (namespace isolation, no fabrication, audit rights), SHA-256 tamper detection
  • Consolidation Scheduler — Asyncio background tasks (cleanup daily, consolidate 6h, decay daily), tier-aware
  • Key Rotation — Failover/round-robin for embedding API keys (GEMINI_API_KEY + GEMINI_API_KEY_FALLBACK)
  • Observability — OpenTelemetry + JSONL audit logging (optional)
  • Deployment — Docker Compose, Kubernetes-ready, health checks
  • Backup/Restore — Memory snapshots, point-in-time recovery
  • Benchmark Suite — p50/p95/p99 latency measurements for all endpoints

Architecture

flowchart TD
    CLI["CLI (Typer)"]
    MCP["MCP (stdio)"]
    HTTP["HTTP API /api/v1/"]
    WS["WebSocket /ws"]

    CLI & MCP & HTTP & WS --> Auth["Auth Middleware\n(JWT + RBAC, optional)"]
    Auth --> Tenant["TenantContext (ContextVar)"]
    Tenant --> Recall["Recall Pipeline\n(decision > resolve > search > feedback)"]
    Recall --> Episodic["EpisodicStore\n(Qdrant)"]
    Recall --> Semantic["SemanticGraph\n(NetworkX + SQLite/PG)"]
    Episodic & Semantic --> Reasoning["Reasoning Engine\n(Gemini via litellm)"]
    Episodic --> Cache["Redis Cache (optional)"]
    WS --> EventBus["Event Bus\n(push events)"]

Quick Start

# Install from PyPI
pip install engram-mem

# Or from source
git clone https://github.com/docaohieu2808/Engram-Mem.git
cd engram && pip install -e .

# Initialize config
engram init

# Set API key
export GEMINI_API_KEY="your-key"

# Start daemon (background HTTP server + watcher)
engram start

# Store a memory
engram remember "Deployed v2.1 to production at 14:00 - caused 503 spike"

# Search memories
engram recall "production incidents"

# Browse all data (episodic + semantic)
engram dump

# Reason across all memory
engram think "What deployment issues have we had?"

Requirements: Python 3.11+, GEMINI_API_KEY for LLM reasoning and embeddings. Basic storage works without it.


Integrations

Claude Code (MCP)

Add to ~/.claude.json:

{
  "mcpServers": {
    "engram": {
      "command": "engram-mcp",
      "env": { "GEMINI_API_KEY": "your-key" }
    }
  }
}

OpenClaw

Install the engram skill, then enable session watcher in ~/.engram/config.yaml:

capture:
  openclaw:
    enabled: true
    sessions_dir: ~/.openclaw/workspace/sessions

HTTP API

# Start server
engram serve --port 8765

# Store memory
curl -X POST http://localhost:8765/api/v1/remember \
  -H "Content-Type: application/json" \
  -d '{"content": "Deployed v1.0", "memory_type": "fact", "priority": 8}'

# Search
curl "http://localhost:8765/api/v1/recall?query=deployment&limit=5"

# Reason
curl -X POST http://localhost:8765/api/v1/think \
  -H "Content-Type: application/json" \
  -d '{"question": "What deployment issues have we had?"}'

# Meeting ledger
curl -X POST http://localhost:8765/api/v1/meeting-ledger \
  -H "Content-Type: application/json" \
  -d '{"title": "Sprint Review", "decisions": ["Ship v2"], "action_items": ["Update docs"]}'

CLI Reference

Memory Operations

# Store with options
engram remember <content> [--type fact|decision|preference|todo|error|context|workflow|meeting_ledger]
                          [--priority 1-10] [--tags tag1,tag2] [--expires 2h|1d|7d]
                          [--topic-key unique-key]

# Search
engram recall <query> [--limit 5] [--type <type>] [--tags tag1,tag2]
              [--resolve-entities] [--resolve-temporal]

# Smart query (auto-routes to recall or think)
engram ask <question>

# Reason across all memory
engram think <question>
engram summarize [--count 20] [--save]

Semantic Graph

engram add node <name> --type <NodeType>
engram add edge <from_key> <to_key> --relation <relation>
engram remove node <key>
engram query [<keyword>] [--type <NodeType>] [--related-to <name>] [--format table|json]
engram autolink-orphans [--apply] [--min-co-mentions 3]
engram graph                          # Open interactive graph visualization

Browse & Export

engram status                         # Summary counts
engram dump                           # Rich tables: all memories, nodes, edges
engram dump --format json             # Full JSON export
engram decay [--limit 20]             # Ebbinghaus decay report

System

engram init                           # Initialize config
engram start                          # Start daemon (HTTP server + watcher)
engram stop                           # Stop daemon
engram serve [--host 0.0.0.0] [--port 8765]  # Foreground server
engram watch [--daemon]               # Watch inbox + OpenClaw sessions
engram health                         # Full system health check
engram resource-status                # Resource tier (FULL/STANDARD/BASIC/READONLY)
engram queue-status                   # Embedding queue status
engram scheduler-status               # Background task schedule
engram constitution-status            # 3-law governance + SHA-256 hash

Maintenance

engram cleanup                        # Delete expired memories
engram consolidate [--limit 50]       # LLM-driven memory consolidation
engram ingest <file.json> [--dry-run] # Ingest chat JSON
engram backup                         # Export memory snapshot
engram restore <file>                 # Import snapshot
engram config show / get <key> / set <key> <value>
engram feedback <id> --positive|--negative

MCP Tools

Tool Description
engram_remember Store memory with type, priority, tags, namespace
engram_recall Search episodic memories (compact format by default)
engram_think Reason across episodic + semantic memory via LLM
engram_status Show memory statistics
engram_get_memory Retrieve full memory content by ID or prefix
engram_timeline Chronological context around a memory
engram_add_entity Add entity node to knowledge graph
engram_add_relation Add relationship edge between entities
engram_query_graph Query knowledge graph
engram_ingest Dual ingest: extract entities + store memories
engram_meeting_ledger Record structured meeting (decisions, action items)
engram_feedback Record positive/negative feedback on memories
engram_auto_feedback Auto-detect feedback from conversation
engram_cleanup Delete all expired memories
engram_cleanup_dedup Deduplicate similar memories by cosine similarity
engram_summarize Summarize recent N memories via LLM
engram_session_start Begin new conversation session
engram_session_end End active session with optional summary
engram_session_summary Get summary of completed session
engram_session_context Retrieve memories from active session
engram_ask Smart query — auto-routes to recall or think

Configuration

Config file: ~/.engram/config.yaml — Priority: CLI flags > env vars > YAML > defaults

episodic:
  mode: embedded              # embedded (Qdrant in-process) or server
  path: ~/.engram/qdrant
  namespace: default

embedding:
  provider: gemini
  model: gemini-embedding-001
  key_strategy: failover      # failover or round-robin

semantic:
  provider: sqlite            # or postgresql
  path: ~/.engram/semantic.db

llm:
  provider: gemini
  model: gemini/gemini-2.0-flash
  api_key: ${GEMINI_API_KEY}

serve:
  host: 127.0.0.1
  port: 8765

capture:
  openclaw:
    enabled: false
    sessions_dir: ~/.openclaw/workspace/sessions
  claude_code:
    enabled: false
    sessions_dir: ~/.claude/projects

auth:
  enabled: false
cache:
  enabled: false
  redis_url: redis://localhost:6379/0
rate_limit:
  enabled: false
audit:
  enabled: false
  path: ~/.engram/audit.jsonl

API Reference

Start server: engram serve [--host 0.0.0.0] [--port 8765]

Method Endpoint Purpose
GET /health Liveness check
GET /health/ready Readiness probe
POST /api/v1/remember Store episodic memory
GET /api/v1/recall Search memories (?query=X&limit=5&offset=0)
POST /api/v1/think LLM reasoning across episodic + semantic
GET /api/v1/query Graph search (?keyword=X&node_type=Y&related_to=Z)
GET /api/v1/memories List/filter memories with pagination
GET /api/v1/memories/export Export memories as JSON
POST /api/v1/meeting-ledger Record structured meeting
POST /api/v1/ingest Extract entities + store memories
POST /api/v1/feedback Record feedback on a memory
GET /api/v1/graph/data Graph data JSON for visualization
GET /graph Interactive graph visualization UI
POST /api/v1/cleanup Delete expired memories (admin)
POST /api/v1/cleanup/dedup Deduplicate memories (admin)
POST /api/v1/summarize LLM summary of recent memories (admin)
GET /api/v1/status Memory statistics

WebSocket API

Connect via ws://host:8765/ws?token=JWT (token optional when auth disabled).

Commands:

Command Payload
remember {"content": "...", "priority": 7}
recall {"query": "...", "limit": 5}
think {"question": "..."}
feedback {"memory_id": "abc123", "feedback": "positive"}
query {"keyword": "PostgreSQL"}
ingest {"messages": [...]}
status {}

Push Events: memory_created, memory_updated, memory_deleted, feedback_recorded


Environment Variables

Variable Purpose
GEMINI_API_KEY LLM + embeddings (primary key)
GEMINI_API_KEY_FALLBACK Secondary key for key rotation
ENGRAM_NAMESPACE Memory namespace isolation
ENGRAM_AUTH_ENABLED Enable JWT auth
ENGRAM_SEMANTIC_PROVIDER sqlite or postgresql
ENGRAM_CACHE_ENABLED Enable Redis caching
ENGRAM_AUDIT_ENABLED Enable audit logs
ENGRAM_TELEMETRY_ENABLED Enable OpenTelemetry

Docker

# Quick start
docker build -t engram:latest .
docker run -e GEMINI_API_KEY="your-key" -p 8765:8765 engram:latest

# Production with PostgreSQL + Redis
ENGRAM_AUTH_ENABLED=true \
ENGRAM_SEMANTIC_PROVIDER=postgresql \
ENGRAM_SEMANTIC_DSN=postgresql://user:pass@postgres:5432/engram \
ENGRAM_CACHE_ENABLED=true \
ENGRAM_CACHE_REDIS_URL=redis://redis:6379/0 \
docker compose up

Testing

pytest tests/ -v                      # All tests
pytest tests/ --cov=src/engram        # With coverage
pytest tests/ -k "recall or feedback" # Specific suites

894+ tests, 61%+ code coverage, CI/CD via GitHub Actions.


Documentation


License

MIT — Copyright (c) Do Cao Hieu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

engram_mem-0.5.18.tar.gz (421.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

engram_mem-0.5.18-py3-none-any.whl (356.3 kB view details)

Uploaded Python 3

File details

Details for the file engram_mem-0.5.18.tar.gz.

File metadata

  • Download URL: engram_mem-0.5.18.tar.gz
  • Upload date:
  • Size: 421.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for engram_mem-0.5.18.tar.gz
Algorithm Hash digest
SHA256 ad0a67f7de739c6d7de5b8cfb8174799126a0f3c5f70601bb70f47350f50354e
MD5 6899350bf96d2b4a709d393dfe106495
BLAKE2b-256 fd85d89dbf6b24cafb324b91b3cfff81fcb9456e51381c7a81f9926512c9bddc

See more details on using hashes here.

File details

Details for the file engram_mem-0.5.18-py3-none-any.whl.

File metadata

  • Download URL: engram_mem-0.5.18-py3-none-any.whl
  • Upload date:
  • Size: 356.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for engram_mem-0.5.18-py3-none-any.whl
Algorithm Hash digest
SHA256 c278d8f4b9b0523edf36e2e86b1ef5ed3999bd09d32162d3e090c44dc1b928a9
MD5 129bd1acf38a0e0dfe405325da0cd335
BLAKE2b-256 f720b08a48474bdde3809da93da917a5d1034002046b45485ed23c7d6dabb883

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page