Token-Goat Mem

Durable memory for daily AI coding · 1-second recall · Trustworthy confidence levels · Anchor-based freshness · Optional token-goat seam
Local-first, defense-in-depth memory that remembers what your AI coding agent keeps forgetting between sessions.
You tell your AI "we use pnpm not npm" and it forgets. Every session. Then it runs npm install and corrupts the lockfile. You re-explain that you prefer 2-space indentation, and the next day it defaults to tabs. These are not oversights — the agent genuinely does not see a record of these decisions after a compaction.
Mem stores them. Locally, in your own SQLite database. Each fact carries a trust level and an anchor (a read-only predicate that tests whether the fact is still true). On recall, Mem re-validates anchors and surfaces only the facts that are fresh and trustworthy, with a confidence caveat so your agent never treats a hint as ground truth when it should not.
Works with Claude Code, Copilot CLI, Copilot in VS Code, Codex, and any agent that can run a shell command — integration guides for the first four live in docs/integrations/. There is also an optional seam -- a one-way integration point, where the other tool calls mem and mem knows nothing about it -- with token-goat, a sister CLI that gives agents narrow-slice code/doc reads to cut context burn. Token-goat reads mem epoch (a cache-invalidation key) and optionally consumes memory via the published TGMEM/2 wire format, which a future host tool can adopt.
Install:
npm install -g token-goat-mem
mem --help
Building from source, requirements, and verifying the install: Install.
Built and maintained by DFK Helper. Free under PolyForm Noncommercial. If it saves your tokens, or your sanity, drop a star at the top of this page.
Install · CLI · Walkthrough · How it works · Anchors · Token-goat integration · Disclaimer & License
The problem
Your AI coding agent accumulates durable knowledge that keeps evaporating:
- Preferences — "uses pnpm not npm", "2-space indent", "no default exports", "tabs not spaces"
- Decisions — "chose Postgres over Mongo for relational queries", "auth service owns migrations"
- Project facts — "staging DB is at prod-staging-db-1", "CI env is GitHub Actions"
- Corrections — recurring "do not do X" that you repeat every session
Today this knowledge is lost at each session boundary. The agent re-asks, re-derives, or — worst — forgets and does the wrong thing. A confident wrong memory is worse than no memory at all. If Mem surfaces a stale preference as ground truth ("you use npm" three months after you switched to pnpm), your agent acts on it and corrupts your lockfile.
The defining engineering problem is not retrieval — it is correctness and staleness. Mem solves both.
What changes
| Before |
After |
| Agent re-reads the same preference every session |
stored pref (verify): uses pnpm, not npm — mem show <id> — one-line hint with confidence |
| Agent does the wrong thing because it forgot a decision |
mem remember "Postgres chosen over Mongo for relational JOIN queries" --kind decision persists it; mem recall surfaces it with provenance and age |
| Mixed signals on project setup (old README says npm, lockfile says pnpm) |
Anchor predicates test the actual state (which lockfile is newer, git history); anchor-contradicted facts are excluded from recall and flagged in mem review for human resolution, never surfaced as ground truth |
| Every session starts cold |
mem recall --hint-format embeds prior facts into your AI context at startup (~5-10 lines per session) |
| Session compaction forgets preferences |
Facts live in SQLite, outside any context window; mem pin <id> additionally exempts a fact from time-decay. Unpinned preferences decay in confidence over time if not re-affirmed |
| Stale facts invisible until they cause damage |
mem review flags anchor-contradicted facts before they become silent bugs |
How it works
- Explicit capture —
mem remember "uses pnpm not npm" --kind preference stores a fact with a source reference (source_ref: where the fact came from, such as the command or file that produced it -- not to be confused with the separate sources table mem show --json exposes, which holds a secret-screened excerpt of the raw material a fact was pulled from and is written only by mem scan-session and mem import --from-md, never by mem remember or mem suggest <text>, where your text is the fact) and timestamp. --kind is required: preference, decision, fact, or correction. Saying the same thing again reaffirms the existing fact rather than writing a second row: captured_at and confidence are refreshed, and the command prints reaffirmed instead of remembered. Matching is deterministic and conservative -- same normalized text (case folded, whitespace collapsed, one trailing period dropped), same kind, same scope binding, same subject and value (value compared case-insensitively with internal whitespace collapsed, same as contradiction detection below, though the raw value you typed is always what gets stored and shown) -- so identical text carrying a genuinely different value stays two facts for contradiction resolution to key on. Only live facts match: a pending one is never reaffirmed (that would promote it without review), a superseded one is never resurrected, and mem suggest never reaffirms at all, since derived text must not refresh a fact the user did not restate.
- Optional anchors — Add an anchor:
mem remember "uses pnpm" --kind preference --anchor 'file-newer-than pnpm-lock.yaml package-lock.json'. The anchor is a read-only predicate; on recall, Mem tests it and returns one of three verdicts: affirmed (ground truth), unverified (hint to verify), or contradicted (suppressed, flagged in review).
- Recall with trust levels —
mem recall --kind preference returns active facts annotated by trust level, freshness verdict, and age. Low-trust facts are marked "verify" so your AI never mistakes a hint for ground truth.
- Review and resolution —
mem review lists pending, contested, and anchor-contradicted facts. The two words are different mechanisms and both appear throughout: contested is fact-versus-fact (two facts disagree with each other, and neither clearly wins), while contradicted is fact-versus-world (an anchor predicate tested reality and denied the fact). A fact can be either without being the other. Contradictions (same subject + scope, different values, ambiguous winner) are never surfaced as ground truth — they appear here for you to resolve. Resolution runs both ways: once the rival is forgotten or edited into agreement, the next mem epoch --gc reinstates the survivor (back to pinned if it was pinned) instead of leaving it withheld.
- Forget and edit —
mem forget <id> soft-deletes a fact (marks it superseded, kept for audit; --reason records why); mem edit <id> updates text, subject/value, anchor, or scope, and mem edit <id> --undo walks the last edit back. Both bump an internal epoch so the token-goat seam always sees the latest state.
Install
Requirements: Node.js 22.12 or later
npm install -g token-goat-mem
Or from source:
git clone https://github.com/DFKHelper/token-goat-mem.git
cd token-goat-mem
npm install
npm run build
npm link
No daemon, no tray icon, no setup wizard. Mem is a short-lived CLI process.
A global install (and every upgrade) also wires mem's Claude Code hooks into your user-level ~/.claude/settings.json by running mem init claude-code --user, so recall works in every project without a separate step. It is idempotent -- unrelated settings are kept, and hooks an older mem wrote are refreshed -- and it never fails the install: if the mem on your PATH cannot run the hooks yet, nothing is written and mem init claude-code --user finishes the job. npm hides install-script output unless you pass --foreground-scripts; mem doctor shows hook health either way. The check of the mem on your PATH waits up to 10 seconds per probe; set TOKEN_GOAT_MEM_PROBE_TIMEOUT_MS (an integer from 100 to 600000 milliseconds) to change that for mem doctor and mem init claude-code. Set TOKEN_GOAT_MEM_SKIP_HOOKS=1 to skip it. It is skipped under sudo (anything running as someone other than your home directory's owner), so the settings file never ends up owned by root, and it gives up after two minutes rather than hang the install. Only npm runs it: after installing with pnpm, yarn or bun, or with --ignore-scripts, run mem init claude-code --user yourself. Installing from source (npm install in a clone) never touches your settings. On Windows the user-level hooks launch node on the installed bundle directly (falling back to the plain mem guard if the bundle is gone) to skip npm's slow mem shim; see docs/integrations/claude-code.md.
Verify the install
mem --version
mem epoch
mem --version prints the installed version; mem epoch prints a number (creating the database on first run), which confirms the CLI and the SQLite store are wired correctly.
Where data lives
Everything is stored in a single SQLite database at ~/.mem/mem.db. Set TOKEN_GOAT_MEM_HOME to relocate it (the test suite uses this to isolate itself from your real data). No network calls, ever.
On POSIX systems the directory is created 0700 and the database 0600, so the store is readable only by you even on a shared machine -- and an existing home created by an earlier version is tightened on the next run. Windows relies on the ACL ~/.mem inherits from your user profile, which restricts it the same way.
Backups
mem keeps snapshots of the store in ~/.mem-backups -- a sibling of the mem home, not inside it, and nowhere near ~/.claude -- so deleting either directory loses nothing. Set TOKEN_GOAT_MEM_BACKUP_DIR to keep them somewhere else (a synced folder, another disk).
- Automatic: whenever mem opens an existing store, it takes a snapshot if the newest one is at least a day old and the store has changed since. The hooks
mem init installs open the store every session, so that is roughly daily. The newest 14 automatic snapshots are kept.
- Before a schema upgrade: a
pre-migration snapshot, always.
- By hand:
mem backup takes one now; mem backup --list shows them all, newest first.
- Restore:
mem restore <name-or-path> checks the snapshot's integrity first, snapshots the store it replaces (pre-restore, so a restore can be undone), then moves the epoch forward. If the live store is not a readable database, it is moved aside to mem.db.unreadable-<timestamp> (the output says where) and the snapshot is restored into a fresh store.
Trust model: restore and import only files you trust. A crafted backup snapshot is equivalent to write access to mem.db, so mem restore should only be pointed at a snapshot mem or you wrote. mem import --from-json likewise trusts the provenance fields in the file (id, status, confidence, captured_at, source_type, source_ref) as written and does not re-derive them, so import only a file you exported yourself or otherwise trust.
Only automatic snapshots are pruned; manual, pre-migration and pre-restore ones stay until you delete them. A snapshot is a compacted VACUUM INTO copy with the same owner-only permissions as the store. A backup that cannot be written never blocks a command -- mem doctor's backups: line is where it shows up.
CLI
| Command |
What it does |
mem remember <text> |
Store a new fact. --kind preference|decision|fact|correction (required). More below. |
mem suggest <text> |
Same flags as mem remember, but always stores the fact pending (captureSuggested, not captureExplicit) — never auto-promoted, confirm via mem review --promote <id>. |
mem scan-session |
Scan a session transcript for durable-statement sentences and file each as pending — the capture half of the Claude Code seam, installed by mem init as the PreCompact hook, and run by mem reflect as the Stop hook. More below. |