How I Built Persistent Memory for Claude Code Agents (and Why Your AI Forgetting Everything Is a Solved Problem)
Every Claude Code session starts blank. Your agent forgets your name, your codebase conventions, the decision you made 20 minutes ago. You end up re-explaining the same context over and over.
I got tired of this and built Cortex — a local-first memory engine that gives AI agents real long-term memory. Here's what it does and why I think it matters.
Current "memory" for AI agents is one of:
- Flat text files — grep-based, no structure, no decay, no relationships
- Cloud APIs (Mem0, etc.) — 200-500ms latency per query, $99+/mo, your data on someone else's servers
- OpenAI Memory — opaque, no export, no control
None of these give you what human memory actually does: structured recall, belief updating, relationship tracking, and forgetting things that don't matter anymore.
Cortex is a pure Rust MCP server (3.8MB binary, zero runtime deps) that runs 100% locally:
- 4 memory tiers — Working → Episodic → Semantic → Procedural (like human memory)
- Bayesian beliefs that self-correct with new evidence
- People graph with cross-platform identity resolution
- Sub-millisecond everything — 156µs ingest, 568µs search (528x faster than Mem0)
- 25 MCP tools — plug into Claude Code, Claude Desktop, or any MCP client
- AES-256-GCM encrypted sync through your own cloud storage (iCloud/GDrive/OneDrive/Dropbox)
- Zero telemetry, zero cost, forever
I use Cortex with Claude Code for my X-Auto project (automated social media + SEO). Here's what changes:
Before Cortex: Every session I had to re-explain that the project uses Gemini as its LLM provider, that we push directly to main, that tests must pass before commits, and dozens of project-specific details.
After Cortex: Claude Code remembers all of this across sessions. It knows my code style preferences, which modules I've been working on, what decisions we made last week and why. The memory_context tool gives it a token-budgeted summary of everything relevant to the current task.
We tested against the LoCoMo benchmark (ACL 2024) — 1540 QA pairs across long-term conversations:
| System | Overall |
|---|---|
| Cortex v1.7 | 73.7% |
| Mem0-Graph | 68.4% |
| Mem0 | 66.9% |
| OpenAI Memory | 52.9% |
Cortex beats Mem0 by 7 percentage points while running entirely on your machine with zero API costs.
# Install (macOS/Linux)
curl -fsSL https://github.com/ghraw/gambletan/cortex/main/install.sh | bash
# Or with npm
npx cortex-mcpAdd to your Claude Code MCP config:
{
"mcpServers": {
"cortex": {
"command": "cortex-mcp-server",
"args": ["--db-path", "~/.cortex/memory.db"]
}
}
}That's it. Claude Code now has persistent memory.
Full source: https://github.com/gambletan/cortex
If it's useful, a star helps others find it. Questions welcome.