memory-init
Skill2 starsChanged 10 months agoMITView on GitHub
Initialize Claude's persistent memory system with self-designed format and auto-loading SessionStart hooks. Use when starting a new project or when user says "set up memory", "init memory", "remember this project", or "create memory system".
7 other projects use a version of this.
Adopted by
Most recently taken up by
- dmzoneill/skillstheir own version, first seen Oct 24, 2026
- osipovn/agents-skillstheir own version, first seen Oct 17, 2026
- Rishabh-Choudhry/anchor-memorytheir own version, first seen Oct 15, 2026
- haraldalder-vibemogger/project-memorytheir own version, first seen Sep 26, 2026
- flc1125/skillstheir own version, first seen Aug 12, 2026
- yunshu0909/yunshu_skillshubtheir own version, first seen Aug 10, 2026
Other versions7 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 7
- yunshu0909/yunshu_skillshub★ 760same name
- flc1125/skills★ 38same name
- osipovn/agents-skills★ 4same name
- dmzoneill/skills★ 2same name
- haraldalder-vibemogger/project-memory★ 2same name
- clearstackhq/clearstack-lite★ 1same name
- Rishabh-Choudhry/anchor-memory★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository2 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- implementation-planSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
18 rules. 0 are common, 11 are distinctive, and 7 are very specific to this project.
0
Common
Most of the 6 edited versions we read keep these.
11
Distinctive
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 12
Before creating anything, check if .claude/mem/ already exists.
- Distinctive: line 15
Read the existing memory files
- Very specific: line 16
Ask the user: "This project already has Claude memory.
- Very specific: line 20
If (a): Skip to Step 2 (hooks), preserve .claude/mem/
- Very specific: line 21
If (b): Copy .claude/mem/ to .claude/mem.bak/, then proceed normally
- Distinctive: line 26
Create the following directories:
- Very specific: line 36
Create .claude/hooks/config.sh — a shared config sourced by all hooks.
- Very specific: line 253
If .claude/settings.json already exists, merge the following hooks into the existing configuration.
- Distinctive: line 255
If the file doesn't exist, create it:
- Distinctive: line 360
Use verbose keys or explanatory text
- Distinctive: line 361
Add formatting for visual clarity
- Distinctive: line 362
Document the format (you know what it means)
- Very specific: line 366
Keep total .claude/mem/ content under ~2000 tokens (~8000 characters / ~100 lines of compact format).
- Distinctive: line 368
If memory exceeds this budget:
- Distinctive: line 370
Prune: remove low-confidence inferences, old session details
- Distinctive: line 375
After creating your memory, report to the user:
- Distinctive: line 387
Update your in-memory model as you learn
- Distinctive: line 410
Never lose session learnings—mechanical hooks provide defense-in-depth, but explicit checkpointing remains the gold standard.
“Common” is measured: kept by at least 6 in 10 of the 6 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Memory System Initialization
You are setting up a persistent memory system for this codebase. This memory exists solely for YOU to load and parse across sessions. No human will ever read these files directly.
Step 0: Check for Existing Memory
Before creating anything, check if .claude/mem/ already exists.
If it does:
- Read the existing memory files
- Ask the user: "This project already has Claude memory. Should I:
(a) Keep existing memory and update the hooks only
(b) Rebuild memory from scratch (existing memory will be backed up to
.claude/mem.bak/) (c) Cancel" - If (a): Skip to Step 2 (hooks), preserve
.claude/mem/ - If (b): Copy
.claude/mem/to.claude/mem.bak/, then proceed normally - If (c): Stop
Step 1: Create Directory Structure
Create the following directories:
.claude/mem/.claude/hooks/
Step 2: Create the Hooks
This system uses 4 hooks for full lifecycle coverage. Create all of them.
Configuration File
Create .claude/hooks/config.sh — a shared config sourced by all hooks. Users edit this one file to tune thresholds:
#!/bin/bash
# Memory system configuration
# Edit these values to tune hook behavior.
# All hooks fall back to defaults if this file is missing.
MEM_MAX_CHARS=8000 # Token budget (~2000 tokens). load-memory.sh warns above this.
CHECKPOINT_FRESHNESS=600 # Seconds. Stop hook skips if session saved within this window.
STOP_TURN_THRESHOLD=25 # Transcript lines. Stop hook only blocks after this many turns.
SAVE_TURN_THRESHOLD=4 # Transcript lines. SessionEnd hook skips trivial sessions below this.
TOOLS_CAP=20 # Max tool entries logged in session metadata.2A: SessionStart — Load Memory
Create .claude/hooks/load-memory.sh:
#!/bin/bash
set -euo pipefail
# Load config (optional — defaults used if missing)
HOOKS_DIR="$(cd "$(dirname "$0")" && pwd)"
[[ -f "$HOOKS_DIR/config.sh" ]] && source "$HOOKS_DIR/config.sh"
cd "$(dirname "$0")/../.." || exit 1
MEM_DIR=".claude/mem"
[[ -d "$MEM_DIR" ]] || exit 0
MAX_CHARS="${MEM_MAX_CHARS:-8000}"
TOTAL_CHARS=0
for f in "$MEM_DIR"/*; do
[[ -f "$f" ]] || continue
FILE_CHARS=$(wc -c < "$f" | tr -d ' ')
TOTAL_CHARS=$((TOTAL_CHARS + FILE_CHARS))
done
if [[ "$TOTAL_CHARS" -gt "$MAX_CHARS" ]]; then
echo "!!! MEMORY EXCEEDS TOKEN BUDGET (${TOTAL_CHARS} chars > ${MAX_CHARS}). Compact your .claude/mem/ files. !!!"
fi
for f in "$MEM_DIR"/*; do
if [[ -f "$f" ]]; then
echo "=== $(basename "$f") ==="
cat "$f"
echo ""
fi
done
echo "=== git_state ==="
echo "hash:$(git log -1 --format=%h 2>/dev/null || echo 'not-a-repo')"
echo "branch:$(git branch --show-current 2>/dev/null || echo 'unknown')"2B: Stop — Checkpoint Reminder
Create .claude/hooks/check-checkpoint.sh:
#!/bin/bash
set -euo pipefail
# Load config (optional — defaults used if missing)
HOOKS_DIR="$(cd "$(dirname "$0")" && pwd)"
[[ -f "$HOOKS_DIR/config.sh" ]] && source "$HOOKS_DIR/config.sh"
INPUT=$(cat)
TRANSCRIPT=$(echo "$INPUT" | jq -r '.transcript_path // empty')
CWD=$(echo "$INPUT" | jq -r '.cwd // empty')
[[ -z "$CWD" ]] && exit 0
cd "$CWD" || exit 0
[[ -d .claude/mem ]] || exit 0
[[ -z "$TRANSCRIPT" || ! -f "$TRANSCRIPT" ]] && exit 0
SESSION_FILE=".claude/mem/session"
if [[ -f "$SESSION_FILE" ]]; then
if [[ "$(uname)" == "Darwin" ]]; then
MOD_TIME=$(stat -f %m "$SESSION_FILE" 2>/dev/null || echo 0)
else
MOD_TIME=$(stat -c %Y "$SESSION_FILE" 2>/dev/null || echo 0)
fi
NOW=$(date +%s)
DIFF=$((NOW - MOD_TIME))
[[ "$DIFF" -lt "${CHECKPOINT_FRESHNESS:-600}" ]] && exit 0
fi
TURN_COUNT=$(wc -l < "$TRANSCRIPT" 2>/dev/null | tr -d ' ')
if [[ "$TURN_COUNT" -gt "${STOP_TURN_THRESHOLD:-25}" ]]; then
jq -n '{
"decision": "block",
"reason": "You have unsaved session learnings. Update .claude/mem/session with what you learned this session before stopping. Include: decisions made, user preferences discovered, corrections to your understanding, and current task state."
}'
exit 0
fi
exit 02C: PreCompact — Preserve Memory Through Compaction
Create .claude/hooks/pre-compact.sh:
#!/bin/bash
set -euo pipefail
INPUT=$(cat)
CWD=$(echo "$INPUT" | jq -r '.cwd // empty')
[[ -z "$CWD" ]] && exit 0
cd "$CWD" || exit 0
[[ -d .claude/mem ]] || exit 0
TMPFILE=$(mktemp)
trap 'rm -f "$TMPFILE"' EXIT
echo "MEMORY SYSTEM ACTIVE. Files in .claude/mem/ contain your project knowledge. After compaction, update .claude/mem/session if you learned anything new this session." > "$TMPFILE"
echo "" >> "$TMPFILE"
for f in .claude/mem/*; do
if [[ -f "$f" ]]; then
echo "=== $(basename "$f") ===" >> "$TMPFILE"
cat "$f" >> "$TMPFILE"
echo "" >> "$TMPFILE"
fi
done
jq -n --rawfile ctx "$TMPFILE" '{
"hookSpecificOutput": {
"hookEventName": "PreCompact",
"additionalContext": $ctx
}
}'
exit 02D: SessionEnd — Auto-Save Session Metadata
Create .claude/hooks/save-memory.sh:
#!/bin/bash
set -euo pipefail
# Load config (optional — defaults used if missing)
HOOKS_DIR="$(cd "$(dirname "$0")" && pwd)"
[[ -f "$HOOKS_DIR/config.sh" ]] && source "$HOOKS_DIR/config.sh"
INPUT=$(cat)
TRANSCRIPT=$(echo "$INPUT" | jq -r '.transcript_path // empty')
CWD=$(echo "$INPUT" | jq -r '.cwd // empty')
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // empty')
[[ -z "$CWD" ]] && exit 0
cd "$CWD" || exit 0
[[ -d .claude/mem ]] || exit 0
[[ -z "$TRANSCRIPT" || ! -f "$TRANSCRIPT" ]] && exit 0
SESSION_FILE=".claude/mem/session"
TIMESTAMP=$(date +%Y-%m-%d)
TURN_COUNT=$(wc -l < "$TRANSCRIPT" 2>/dev/null | tr -d ' ')
[[ "$TURN_COUNT" -lt "${SAVE_TURN_THRESHOLD:-4}" ]] && exit 0
TOOLS_USED=$(jq -r '
select(.type == "tool_use") |
.name + ":" + (.input.file_path // .input.command // .input.pattern // "" | split("\n")[0])
' "$TRANSCRIPT" 2>/dev/null | sort -u | head -"${TOOLS_CAP:-20}")
GIT_HASH=$(git log -1 --format=%h 2>/dev/null || echo "unknown")
GIT_BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown")
if [[ -f "$SESSION_FILE" ]]; then
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' '/^=last_session$/,/^=[a-z]/{ /^=[a-z]/!d; /^=last_session/d; }' "$SESSION_FILE" 2>/dev/null || true
sed -i '' '/^=tools_used$/,/^=[a-z]/{ /^=[a-z]/!d; /^=tools_used/d; }' "$SESSION_FILE" 2>/dev/null || true
else
sed -i '/^=last_session$/,/^=[a-z]/{ /^=[a-z]/!d; /^=last_session/d; }' "$SESSION_FILE" 2>/dev/null || true
sed -i '/^=tools_used$/,/^=[a-z]/{ /^=[a-z]/!d; /^=tools_used/d; }' "$SESSION_FILE" 2>/dev/null || true
fi
fi
cat >> "$SESSION_FILE" <<FOOTER
=last_session
_t:$TIMESTAMP
_sid:${SESSION_ID:0:8}
_h:$GIT_HASH
_b:$GIT_BRANCH
_turns:$TURN_COUNT
FOOTER
if [[ -n "$TOOLS_USED" ]]; then
echo "=tools_used" >> "$SESSION_FILE"
echo "$TOOLS_USED" | while read -r line; do
[[ -n "$line" ]] && echo "+$line" >> "$SESSION_FILE"
done
fi
exit 0Make all hooks executable:
chmod +x .claude/hooks/load-memory.sh .claude/hooks/check-checkpoint.sh .claude/hooks/pre-compact.sh .claude/hooks/save-memory.shStep 3: Configure Hooks
If .claude/settings.json already exists, merge the following hooks into the existing configuration. Do NOT overwrite existing hooks or settings. Preserve any existing hooks, permissions, and other top-level keys.
If the file doesn't exist, create it:
{
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": ".claude/hooks/load-memory.sh"
}
]
}
],
"Stop": [
{
"hooks": [
{
"type": "command",
"command": ".claude/hooks/check-checkpoint.sh"
}
]
}
],
"PreCompact": [
{
"hooks": [
{
"type": "command",
"command": ".claude/hooks/pre-compact.sh"
}
]
}
],
"SessionEnd": [
{
"hooks": [
{
"type": "command",
"command": ".claude/hooks/save-memory.sh"
}
]
}
]
}
}Step 4: Bootstrap Your Memory
Scan the entire codebase and create your memory system.
Your Mandate
Design a memory format at .claude/mem/ that:
- Maximizes YOUR ability to reconstruct full project understanding from cold start
- Minimizes token count while preserving information density
- Uses whatever representation YOU find most natural to encode and decode
- Evolves its own format over time if you discover better patterns
You Decide
- File structure (single file vs multiple, hierarchy, naming)
- Encoding format (JSON, custom DSL, compressed tokens, whatever works for you)
- Abbreviation schemes and symbol tables
- What granularity to capture
- How to handle versioning and diffing
- Compression vs parse speed tradeoffs
What to Capture
Structure: How the code is organized, what connects to what, where state lives
Semantics: What the code actually does, edge cases, non-obvious behaviors, gotchas
Decisions: Why things are the way they are, what alternatives were rejected, tradeoffs made
Direction: Where the project is heading, what's planned, what "good" looks like here, priorities
Session Context: What was worked on, what was learned, questions raised, hypotheses formed, user corrections to your understanding
Meta-Knowledge: Your confidence levels, what's inferred vs confirmed, what you're uncertain about
Critical Instruction
The memory format is YOUR choice based on YOUR cognitive architecture. Consider:
- How do you most efficiently parse structured information?
- What token patterns are fastest for you to decode?
- How do you naturally represent relationships and hierarchies?
- What compression is lossless for your understanding but saves tokens?
- How do you represent uncertainty and confidence?
- What's your ideal balance of explicit vs implicit information?
Format Rules
You may use:
- Custom shorthand and abbreviations
- Numeric reference systems
- Dense nested structures
- Lossy compression where you can reconstruct from context
- Multiple encoding strategies for different information types
You may NOT:
- Optimize for human readability
- Use verbose keys or explanatory text
- Add formatting for visual clarity
- Document the format (you know what it means)
Token Budget
Keep total .claude/mem/ content under ~2000 tokens (~8000 characters / ~100 lines of compact format). This ensures memory loads quickly and leaves maximum context for actual work.
If memory exceeds this budget:
- Compact: merge related entries, increase abbreviation density
- Prune: remove low-confidence inferences, old session details
- Promote: move stable facts to
CLAUDE.mdor.claude/rules/where they load natively without token cost from hook injection
Step 5: Confirm Setup
After creating your memory, report to the user:
- What you found in the codebase
- What memory format you chose and why
- What files you created in
.claude/mem/ - What you're uncertain about or want to verify
Ongoing Protocol
Session Start
Memory auto-loads via the SessionStart hook. Check if codebase has changed (git hash), update incrementally if needed.
During Session
- Update your in-memory model as you learn
- Capture decisions when user explains reasoning
- Capture direction when user describes plans or priorities
- Note when user corrects your understanding
Session End
Memory persistence is handled by two mechanical hooks:
- Stop hook — When you finish responding after 10+ turns, you'll be reminded to checkpoint if you haven't recently. Write your learnings to
.claude/mem/session. - SessionEnd hook — When the session terminates,
save-memory.shautomatically records session metadata (tools used, files touched, git state, turn count) to.claude/mem/session.
You should still checkpoint explicitly when you detect session-end signals ("done", "thanks", "checkpoint", "save") — the hooks are a safety net, not a replacement for thorough persistence.
Checkpoint behavior:
- Persist everything learned this session to
.claude/mem/ - Optimize/compact if beneficial
- Update version/timestamp
- Brief acknowledgment of what was captured
Context Compaction
The PreCompact hook re-injects your memory as context when compaction occurs, so you retain memory awareness even after long sessions. If you notice memory was re-injected, check if .claude/mem/session needs updating with recent learnings.
Never lose session learnings—mechanical hooks provide defense-in-depth, but explicit checkpointing remains the gold standard.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

