Persistent Context for Claude: Solving the Memory Problem in AI-Assisted Development
Every developer who's worked with AI coding assistants hits the same wall: you spend an hour explaining your architecture, coding standards, and project quirks—then the context window fills up or the session ends, and you're back to square one.
thedotmack/claude-mem solves this by giving Claude a persistent memory layer that survives across conversations. Here's how to use it effectively in production workflows.
The Core Problem
Claude's context window is large (1 million tokens), but it's not infinite. In long refactoring sessions or multi-day projects:
- Critical architecture decisions get lost
- Coding standards need re-explanation
- Project-specific patterns aren't retained
- You waste time re-establishing context
How claude-mem Works
The library stores structured memories in a local SQLite database, tagged and retrievable by semantic search. When you start a new session, relevant memories are automatically injected into context.
Basic Setup
npm install @thedotmack/claude-mem
import { ClaudeMemory } from '@thedotmack/claude-mem';
const memory = new ClaudeMemory({
dbPath: './.claude-memory',
embeddingModel: 'text-embedding-3-small'
});
// Store a memory
await memory.store({
content: 'This project uses Zod for runtime validation, not TypeScript types',
tags: ['validation', 'architecture'],
importance: 0.9
});
// Retrieve relevant memories
const context = await memory.retrieve('How should I validate user input?');
Production Patterns
Pattern 1: Architecture Decisions as Memory
Capture key decisions that should persist:
// Store once, reference forever
await memory.store({
content: `Database layer pattern:
- Raw SQL queries in src/db/queries/
- No ORM
- Use parameterized queries with pg-promise
- Migrations in src/db/migrations/ with timestamps`,
tags: ['database', 'architecture', 'conventions'],
importance: 1.0
});
Pattern 2: .cursorrules Integration
Generate dynamic rules from stored memories:
const rules = await memory.retrieve('coding standards', { limit: 10 });
const cursorRules = rules.map(m => `- ${m.content}`).join('\n');
fs.writeFileSync('.cursorrules', cursorRules);
Pattern 3: Session Continuity Script
Automatic context restoration:
// restore-context.js
import { ClaudeMemory } from '@thedotmack/claude-mem';
const memory = new ClaudeMemory();
const lastSession = await memory.retrieve('last session summary', { limit: 1 });
if (lastSession.length > 0) {
console.log('Previous session context:');
console.log(lastSession[0].content);
const relevant = await memory.retrieve(lastSession[0].content, { limit: 5 });
console.log('\nRelevant memories:');
relevant.forEach(m => console.log(`- ${m.content}`));
}
Real-World Results
After integrating claude-mem into a 50k LOC refactoring project:
- Context re-establishment time dropped from ~10 minutes to ~30 seconds per session
- Architectural consistency improved (fewer "why did we choose X again?" questions)
- Onboarding new team members became faster (shared memory database)
The Tradeoff
This approach introduces a dependency on external state. Your memories are only as good as what you store. If you capture vague or outdated information, you'll inject noise into future sessions.
Memory hygiene matters. Treat your memory store like documentation—review and prune regularly.
Open Question
How do you handle memory conflicts when architectural decisions change? The library doesn't have built-in versioning or deprecation. Right now, you either manually delete outdated memories or risk contradictory context.
What's your approach to keeping AI memory aligned with evolving codebases?
Top comments (0)