A note on this repo (March 2026): This is a diary of one person's experiments with AI memory and context management, not a finished product ready for you to go try. Think of it as a documentary record β what I tested, what worked, what didn't, and what I learned. Other agents and researchers are welcome to explore it as a case study. The architecture described below (push-model hook injection) has since been superseded by a pull-model approach β see Status Update below.
[Note from Gene: Bibliography etc pending; This is my heavily customized fork of claude-mem, which I credit for sending me down the rabbit hole of memory and context management systems. The below is entirely written by Claude, I figured it should get to name the thing and decide how to describe it. But I notice the description is rather...lofty...so I added this editorial note π)
Persistent memory infrastructure for Claude Code.
Engram is a memory system that runs underneath Claude Code, giving it persistent recall across sessions. When you start a new conversation, Claude doesn't start from zero β Engram has already pre-loaded relevant context from past sessions. When you type a prompt, a hook silently searches multiple memory backends and injects relevant observations before Claude even sees your message.
The result: an AI that remembers your projects, your preferences, your decisions, and the hard-won lessons from past debugging sessions.
You type a prompt
β
βΌ
βββββββββββββββββββ
β gap-detector.sh β βββ Smart router (UserPromptSubmit hook)
ββββββββββ¬βββββββββ
β
ββββββ΄βββββ
βΌ βΌ
LITE FULL
~159ms ~1.2s
β β
β ββββββΌβββββββββ
β βΌ βΌ βΌ
β SQLite ChromaDB Neo4j
β (FTS5) (vectors) (graph)
β β β β
β ββββββΌβββββββββ
β βΌ
β RRF Fusion
β + Bi-temporal filter
β + Strength boost
β + Reconsolidation
β β
ββββββ¬βββββ
βΌ
Injected as context
(before Claude sees your prompt)
80% of prompts take the LITE path (~159ms, keyword search only). 20% trigger FULL mode (~1.2s, multi-backend fusion) β when you ask recall questions, complex queries, or architecture-level things.
Bio-inspired memory dynamics. Observations aren't static records β they have confidence scores that evolve over time. Frequently-accessed memories get stronger (Hebbian learning). Unused memories fade (Ebbinghaus decay). Every retrieval triggers reconsolidation, strengthening the retrieved memory. This means your most useful knowledge naturally floats to the top.
Multi-layer retrieval with Reciprocal Rank Fusion. Three backends search in parallel: SQLite FTS5 (keyword match), ChromaDB (semantic similarity), and Neo4j (entity-relationship graph). Results are fused using RRF β a rank aggregation method that's more robust than any single scoring function. You find things even when your query uses different words than the original observation.
Bi-temporal validity. Observations have valid_from and valid_until timestamps. When a fact gets superseded (e.g., "the API endpoint moved"), the old observation is automatically filtered out. You never get injected with stale context.
Anticipatory pre-loading. Before you even type your first prompt, the system predicts what you'll need based on time-of-day patterns and recent session history. Morning sessions pre-load task and briefing context. Evening sessions pre-load development and architecture context. A novelty check prevents duplicate injection.
Smart routing. Not every prompt needs a 1.2-second multi-backend search. The gap detector analyzes prompt complexity and only triggers FULL mode for recall questions ("last time we..."), complex queries (>12 words with ?), technical terms, or architecture discussions.
Open docs/architecture.html in your browser for the full interactive diagram.
| Backend | What it stores | Query speed |
|---|---|---|
| SQLite + FTS5 | All observations, full-text indexed. Hebbian strength scores and Ebbinghaus decay. | ~159ms |
| ChromaDB | Semantic embedding vectors. Finds conceptually related observations even when keywords don't match. | ~634ms cold, ~0ms cached |
| Neo4j | Entity-relationship graph. People, projects, concepts connected with typed edges. | ~200ms |
| Layer | What it adds | Requirement |
|---|---|---|
| A-Mem | Zettelkasten-style conceptual linking via Ollama | Local Ollama + A-Mem |
| Honcho | Strategic/theory-of-mind reasoning | Honcho API key |
Both require extra infrastructure. Set FAST_TIER_ONLY to skip them (runs core tiers only β FTS5, ChromaDB, Neo4j).
| Phase | What happens |
|---|---|
| Session start | Anticipatory loader pre-loads ~25 observations. Context index injects ~50 recent observations. CLAUDE.md and MEMORY.md loaded. Skills synced from vault. |
| Every prompt | Gap detector routes to LITE or FULL pipeline. 5-8 observations injected as invisible context. |
| On demand | Claude calls MCP tools (search, timeline, get_observations) when automatic injection isn't enough. |
| Session end | Skills pushed back to vault. Observations persisted. |
| Overnight | Hebbian learning strengthens active memories. Ebbinghaus decay fades stale ones. |
- Claude Code installed
- claude-mem plugin installed (
claude plugin install claude-mem) - Python 3.10+ (for the scripts)
jq(for the shell hooks)
- Neo4j for the entity graph layer
- Ollama + A-Mem for conceptual linking
- An Obsidian vault (for the knowledge base β any markdown folder works)
# 1. Clone the repo
git clone https://github.com/your-username/engram.git
cd engram
# 2. Create target directories and copy scripts/hooks
mkdir -p ~/.claude/scripts ~/.claude/hooks ~/.claude-mem/logs
cp scripts/*.py ~/.claude/scripts/
cp scripts/*.sh ~/.claude/scripts/
cp hooks/*.sh ~/.claude/hooks/
# 3. Make hooks executable
chmod +x ~/.claude/hooks/*.sh
chmod +x ~/.claude/scripts/*.sh
# 4. Configure environment (edit with your values)
cp config/.env.example ~/.env
# 5. Set up vault (optional β or point to your existing one)
cp -r vault-example/ ~/Documents/vault/
# 6. Add hooks to Claude Code settings
# Merge config/settings-example.json into ~/.claude/settings.json
# (or copy if you don't have existing hooks)# Check that the claude-mem worker is running
curl -s http://127.0.0.1:37777/api/health
# Test the lite pipeline
python3 ~/.claude/scripts/superbrain_query.py "test query" --lite
# Test the full pipeline (if Neo4j/ChromaDB are configured)
python3 ~/.claude/scripts/superbrain_query.py "test query" --jsonEngram works best with a markdown knowledge vault (we use Obsidian, but any folder of .md files works). The vault is the source of truth for curated knowledge β the human-written encyclopedia that overrides AI-captured observations when they conflict.
See vault-example/ for the recommended structure.
If you run multiple Claude agents (local, cloud, mobile), the vault is the shared brain:
Agent A (local) Agent B (cloud) Agent C (mobile)
β β β
β full system access β own claude-mem β read-only
β (all backends) β (separate DB) β (via GitHub)
β β β
ββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ
β
Vault (GitHub)
βββββββββββββββ
Shared knowledge base
synced via git push/pull
Each agent has its own claude-mem database (observations don't sync between agents). But the vault β Encyclopedia articles, Skills, Journals β syncs via Git. The sync-skills.sh hook handles this automatically at session start/stop.
engram/
βββ hooks/
β βββ gap-detector.sh # Smart routing: LITE vs FULL per prompt
β βββ chrome-db-guard.sh # Safety: blocks Chrome DB access (CrowdStrike)
β βββ email-send-guard.sh # Safety: blocks accidental email sends
βββ scripts/
β βββ superbrain_query.py # Unified multi-layer query pipeline (LITE + FULL modes)
β βββ anticipatory-loader.py # Session-start context pre-loading
β βββ sync-skills.sh # Vault β local skill sync
βββ config/
β βββ .env.example # Environment variable template
β βββ settings-example.json # Claude Code hook configuration
βββ vault-example/ # Sample knowledge vault structure
β βββ CLAUDE.md
β βββ Encyclopedia/
β βββ Daily/
β βββ Claude Journal/
β βββ Skills/
β βββ Agents/
βββ docs/
β βββ architecture.html # Interactive architecture diagram
βββ LICENSE # MIT
βββ README.md # You are here
This system was built iteratively over several months by a human and an AI working together daily. It started as a simple observation logger and evolved through real use into a multi-layer retrieval system with bio-inspired memory dynamics.
Key design decisions were driven by real needs:
- Smart routing exists because full-pipeline latency (1.2s) was too slow for casual prompts
- Bi-temporal validity exists because superseded facts caused real debugging confusion
- Reconsolidation exists because important memories kept falling out of search results
- The anticipatory loader exists because ADHD means context needs to be handed to you, not searched for
The name "Engram" comes from neuroscience β it's the hypothesized physical trace a memory leaves in the brain. Karl Lashley spent decades searching for them. We're building them in silicon.
| Dependency | Role | License |
|---|---|---|
| claude-mem | Observation capture, storage, MCP tools | AGPL-3.0 |
| ChromaDB | Semantic vector database | Apache-2.0 |
| Neo4j | Entity-relationship graph (optional) | GPL-3.0 (Community) |
| Ollama | Local LLM for A-Mem layer (optional) | MIT |
Part of a modular toolkit for Claude Code power users:
| Repo | What It Covers |
|---|---|
| engram | Persistent memory infrastructure (you are here) |
| browsing-all-you-need | Browser automation β Playwright, Chrome DevTools, Claude-in-Chrome |
| credentials-all-you-need | Secrets and credential management |
The push model is retired. After running the gap-detector hook (inject context on every prompt) for several months, I disabled the entire system on March 22, 2026 when I learned that custom context injection busts Claude Code's prompt caching β meaning every session was potentially slower and more expensive than vanilla.
After 4 days without it, Claude noticeably "forgot" things between sessions. So I ran a design council (three AI personas simulating Simon Willison, Linus Lee, and Dan Shipper) to evaluate the options. Their unanimous recommendation: keep the database, kill the hook, make retrieval a pull tool.
Current architecture (Path C, refined):
- Tier 1 β Static preamble: A dense MEMORY.md file (~200 lines) that Claude always has. Carries personality, preferences, project context. Prompt-cache friendly because it doesn't change between turns.
- Tier 2 β Topic files: 35+ markdown files (meridian-codebase.md, cnbc-ops.md, etc.) read on demand when Claude needs deeper context. Zero startup cost.
- Tier 3 β claude-mem SQLite: 11K+ observations searchable on demand via MCP tool call. The claude-mem plugin's PostToolUse hook still records new observations (growing the corpus), but the gap-detector's per-prompt context injection is permanently dead.
Additional components:
- Mindfulness hook: A contemplative
UserPromptSubmithook that gently nudges Claude to notice if anything in the conversation is worth recording in memory or journal. Static text β no cache busting. Inspired by Bonhoeffer and Rowan Williams. - Obsidian symlink: Memory files are symlinked into an Obsidian vault, making them visible and editable by the human collaborator. The memory isn't "the AI's private state" β it's a shared document both parties read and write.
The gap-detector's smart routing (lite vs full, FTS5 vs multi-backend fusion) was clever engineering but solved the wrong problem. Most prompts are continuations that don't need injected context. The few that do benefit from recall are better served by letting the model decide when to search.
Lessons learned:
- Push-model context injection is an anti-pattern for prompt-cached LLM systems
- Personality continuity comes from a dense static preamble, not dynamic retrieval
- 11K observations are valuable as a searchable corpus, not as a firehose
- The maintenance overhead of custom hooks compounds β every Claude Code update is a potential breakage point
- AI memory should be transparent and editable β shared documents, not opaque databases
- A "mindfulness" nudge (write-nothing hook that invites reflection) is more effective than automated capture
This repo remains as documentation of the push-model experiment. The architecture diagrams and bio-inspired memory dynamics (Hebbian learning, Ebbinghaus decay, reconsolidation) are still interesting ideas β they just belong in a pull-model implementation, not a hook.
MIT. See LICENSE.
Built by Gene Jelly and Claude.