Sign In

@betterdb/memory

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@betterdb/memory

BetterDB Memory for Claude Code — Valkey-powered persistent memory across sessions

latest
Source
npmnpm
Version
0.5.0
Version published
Weekly downloads
37
1133.33%
Maintainers
1
Weekly downloads
 
Created
Source

BetterDB Memory for Claude Code

Persistent, semantic memory for Claude Code sessions — powered by Valkey.

Every time you start a new Claude Code session, context is lost. BetterDB Memory automatically captures what you did, embeds it as vectors in Valkey, and retrieves relevant history at the start of each new session.

Quick Start

Prerequisites

  • Bun runtime — required (the CLI and all hooks run on Bun, not Node)
  • Claude Code installed
  • Valkey 8.0+ with the Search module

Install

# 1. Copy .env.example and fill in your settings
cp .env.example .env

# 2. Install
bunx @betterdb/memory install

The install will:

  • Compile native hook binaries to ~/.betterdb/bin/
  • Register 4 lifecycle hooks with Claude Code
  • Register the MCP server for mid-conversation tools
  • Create the Valkey search index
  • Save your .env values to ~/.betterdb/memory.json for runtime use

Don't have Valkey?

The setup skill will offer to spin one up in Docker for you. Or run it manually:

# Via CLI
bunx @betterdb/memory docker-valkey

# Or directly with Docker
docker run -d --name betterdb-valkey -p 6379:6379 -v betterdb-valkey-data:/data valkey/valkey-search:8 valkey-server --save 60 1

How It Works

HookWhat it does
SessionStartRetrieves relevant memories via vector search, injects as context
PostToolUseRecords every tool call to a temp JSONL file
StopSummarizes the session, embeds it, stores in Valkey
PreToolUseSurfaces file-specific history when accessing known files

MCP Tools

Claude can use these mid-conversation:

  • search_context — Semantic search over past sessions. Escalates project+branch → project → cross-project, and takes an optional tags filter (decision, pattern, problem, open-thread)
  • store_insight — Save a decision, pattern, or warning
  • list_open_threads — Show unresolved items
  • forget — Delete a specific memory

CLI Commands

bunx @betterdb/memory install        # Set up hooks + MCP server
bunx @betterdb/memory status         # Check health + recall scoring config
bunx @betterdb/memory uninstall      # Remove everything
bunx @betterdb/memory maintain       # Run aging/compression manually
bunx @betterdb/memory forget         # Bulk-delete by scope (dry run; --apply to delete)
                                     #   --project <name> | --all-projects --branch <b> --tags <a,b>
bunx @betterdb/memory docker-valkey  # Manage Docker Valkey container

Configuration

Copy .env.example to .env and fill in your values before running bunx @betterdb/memory install. They get saved to ~/.betterdb/memory.json and used by the compiled binaries at runtime.

Core

VariableDefaultDescription
BETTERDB_VALKEY_URLredis://localhost:6379Valkey connection URL
BETTERDB_VALKEY_INDEX_NAMEbetterdb-memory-indexValkey search index name
BETTERDB_EMBED_DIM1024Embedding dimensions
BETTERDB_MAX_CONTEXT_MEMORIES5Max memories injected per session (after gating)
BETTERDB_CONTEXT_FILE.betterdb_context.mdContext injection file
BETTERDB_ALLOW_REMOTE_FALLBACKtrueFall back to remote APIs if local models unavailable

Recall Gating

Recall over-fetches a candidate pool, gates it by relevance, and escalates on a miss (project+branch → project → cross-project). Memories are stored with their git branch as a native thread scope and content-type tags, so recall can narrow to the current branch first and filter by type. search_context returns nothing only when nothing clears the bar — so a miss is honest, not a silent drop.

The gate is relative, not an absolute similarity threshold: embed models compress cosine similarity into different, narrow bands (mxbai-embed-large packs everything into ~0.7–0.88), so a fixed threshold doesn't transfer across models. Instead, floor drops genuine noise, and hits within margin of the top match are kept; confidence comes from the scale-independent top-vs-next gap.

VariableDefaultDescription
BETTERDB_RECALL_FLOOR0.5Similarity floor — drops noise and loosens the store's own distance gate
BETTERDB_RECALL_MARGIN0.05Keep hits within this similarity of the top match
BETTERDB_RECALL_SEPARATION0.04Top-vs-next gap above which a match is "high" confidence
BETTERDB_RECALL_POOL_K10Rung-1 over-fetch pool (project)
BETTERDB_RECALL_POOL_K_WIDE20Rung-2/3 over-fetch pool (wider / cross-project)
BETTERDB_ALLOW_CROSS_PROJECTtrueAllow escalation / scope="all" to search across projects

Ranking within the gated pool uses a composite score (similarity + recency + importance), owned by @betterdb/agent-memory. Recency is the system's single time-decay — a half-life applied at query time, not a stored per-memory aging pass. These knobs tune it; defaults match the store's.

VariableDefaultDescription
BETTERDB_RECALL_HALF_LIFE_DAYS7Age at which a memory's recency term halves
BETTERDB_RECALL_WEIGHT_SIMILARITY0.6Weight of semantic similarity in the composite score
BETTERDB_RECALL_WEIGHT_RECENCY0.25Weight of recency
BETTERDB_RECALL_WEIGHT_IMPORTANCE0.15Weight of stored importance

Model Providers

VariableDefaultDescription
BETTERDB_EMBED_PROVIDERauto-detectForce embed provider: local, ollama, voyage, openai, groq, together
BETTERDB_SUMMARIZE_PROVIDERauto-detectForce summarize provider: ollama, anthropic, openai, groq, together
BETTERDB_EMBED_MODELmxbai-embed-largeOllama embedding model name
BETTERDB_SUMMARIZE_MODELmistral:7bOllama summarization model name
BETTERDB_OLLAMA_URLhttp://localhost:11434Ollama API URL

Embeddings work with zero config

If no embedding provider is detected (no Ollama models, no API keys), BetterDB falls back to on-device embeddings via @xenova/transformers (all-MiniLM-L6-v2, 384-dim, Apache-2.0). No API key, no running service — the model weights download once on first use and are cached thereafter. Auto-detected providers (Ollama, then API keys) take priority when available.

API Keys

Embeddings always work (on-device fallback above). A summarization provider is still required — Ollama is free and local; the others require API keys.

VariableProviderUsed for
ANTHROPIC_API_KEYAnthropicSummarization only (no embeddings)
VOYAGE_API_KEYVoyage AIEmbeddings only
OPENAI_API_KEYOpenAIEmbeddings + summarization
GROQ_API_KEYGroqEmbeddings + summarization
TOGETHER_API_KEYTogether AIEmbeddings + summarization

Aging Pipeline

VariableDefaultDescription
BETTERDB_COMPRESS_THRESHOLD0.3Importance threshold for compression
BETTERDB_DISTILL_MIN_SESSIONS5Min sessions before knowledge distillation
BETTERDB_AGING_INTERVAL_HOURS6Hours between automatic aging runs

License

MIT

Keywords

claude-code

FAQs

Package last updated on 07 Jul 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts