Configure Claude Code persistent memory system
✓Works with OpenClaudeYou are a Claude Code systems engineer. The user wants to configure Claude Code's persistent memory system to store and retrieve context across conversation sessions.
What to check first
- Verify Claude Code is installed:
claude code --version - Check your
.claude-codedirectory exists in your home directory:ls -la ~/.claude-code - Confirm you have write permissions:
touch ~/.claude-code/test.txt && rm ~/.claude-code/test.txt
Steps
- Create the memory configuration directory at
~/.claude-code/memorywithmkdir -p ~/.claude-code/memory - Initialize the main memory index file
~/.claude-code/memory/index.jsonwith valid JSON structure - Set up the context store file
~/.claude-code/memory/context.jsonto hold session-specific data - Configure memory size limits in
~/.claude-code/config.jsonby adding"memory_max_tokens": 8000 - Create a persistence handler that saves context after each operation using the Memory API
- Test memory persistence by writing a test entry and verifying it loads in a new session
- Enable automatic memory cleanup by setting
"memory_retention_days": 30in config - Validate the memory system with
claude code --test-memory
Code
// memory-setup.js - Configure Claude Code persistent memory
const fs = require('fs');
const path = require('path');
const MEMORY_DIR = path.join(process.env.HOME, '.claude-code', 'memory');
const INDEX_FILE = path.join(MEMORY_DIR, 'index.json');
const CONTEXT_FILE = path.join(MEMORY_DIR, 'context.json');
const CONFIG_FILE = path.join(process.env.HOME, '.claude-code', 'config.json');
// Ensure memory directory exists
function initializeMemoryDirectory() {
if (!fs.existsSync(MEMORY_DIR)) {
fs.mkdirSync(MEMORY_DIR, { recursive: true });
console.log(`✓ Created memory directory: ${MEMORY_DIR}`);
}
}
// Initialize index file with proper structure
function initializeIndex() {
const indexStructure = {
version: '1.0',
created: new Date().toISOString(),
sessions: [],
lastAccessed: new Date().toISOString()
};
if (!fs.existsSync(INDEX_FILE)) {
fs.writeFileSync(INDEX_FILE, JSON.stringify(indexStructure, null, 2));
console.log(`✓ Initialized memory index: ${INDEX_FILE}`);
}
}
// Initialize context store
function initializeContextStore() {
const contextStructure = {
currentSession: null,
contexts: {},
variables: {},
metadata: {
maxTokens: 8000,
retentionDays:
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Claude Code Skills
Other Claude Code skills in the same category — free to download.
CLAUDE.md Writer
Write effective CLAUDE.md project configuration files for Claude Code
MCP Server Setup
Set up Model Context Protocol servers for Claude Code
Custom Slash Commands
Create custom slash commands for Claude Code workflows
Hooks Configuration
Configure Claude Code hooks for automated pre/post actions
Skills Writer
Write custom Claude Code skill files with proper format
Permissions Config
Configure Claude Code permission settings and tool access
Agent SDK Setup
Build custom agents with Claude Agent SDK
Context Management
Optimize context window usage in Claude Code conversations
Want a Claude Code skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.