i built a self-learning memory system for claude code. here’s how to set it up in 2 minutes.
claude code has no memory between sessions. none. every time you start a conversation it wakes up with no idea what you worked on yesterday, what mistakes it made, or where you left off.
i’ve been running claude code as my daily driver for months now. not for fun. it’s how i actually get work done. and the no memory thing was destroying my productivity. every session started with me re-explaining context like a broken record.
so i built a system where claude writes down everything it does, learns from every correction, and picks up exactly where it left off next time. after a few weeks the difference is real. not because the model got smarter. because the instructions got better.
the problem
claude code reads a file called CLAUDE dot md when it starts up. most people throw some project instructions in there and move on. but that file doesn’t learn. it doesn’t update itself. it doesn’t remember what happened last session.
you can tell claude “remember this for next time” and it’ll say “got it” and then completely forget because the session ended.
the fix. 3 layers of memory.
1. activity logs. a timestamped journal of everything claude does during a session. what got built, what decisions were made, what broke. one file per day so you can always look back.
2. notes files. permanent knowledge organized by topic. environment quirks, coding standards, workflow rules, patterns it learned. these get loaded every single session through a hook.
3. last session file. exactly where we left off. what’s done, what’s next, what’s blocking. claude reads this on startup and picks up the thread.
every learning goes to exactly one place. project stuff stays in the project. global patterns go to global notes. one-time observations sit in the activity log until they show up enough times to earn a spot in permanent notes.
self-pruning
this is what keeps it from becoming a mess. without pruning your notes files grow forever and eat your context window.
on session start claude checks file sizes and flags anything that needs cleaning up. activity logs older than 14 days get compressed into monthly summaries. duplicates get merged. entries that ended up in the wrong file get moved.
one rule. pruning only makes things denser. it never deletes anything. it compresses, moves, or promotes.
the loop
every time you correct claude it writes that pattern down immediately. not at the end of the session. right then. next session it loads that note and doesn’t make the same mistake.
after a few weeks claude stops asking questions it already asked. it remembers how you like things done. it knows your codebase. you’re training a system that writes its own training data.
how to set it up
open claude code and paste this. it builds the whole thing in about 60 seconds.
the prompt:
build me a self-learning memory system with these exact specifications. create all the directories, hook scripts, settings dot json entries, and CLAUDE dot md instructions. don’t ask me questions. just build it.
architecture. 3 layers:
1. activity logs at ~/.claude/logs/YYYY-MM-DD dot md. timestamped journal of everything done during sessions. format each entry as “## HH:MM - [project] description” followed by bullet points of what happened. write to this continuously throughout the session.
2. notes files at ~/.claude/notes/ as markdown files. permanent knowledge loaded every session. start with session-lessons dot md for reusable patterns and last-session dot md for session continuity. i’ll add topic files like environment dot md or coding-standards dot md later as needed.
3. last-session dot md at ~/.claude/notes/last-session dot md. where we left off, what’s done, what’s next. update this at the end of every session and before context compaction.
hooks. 2 scripts registered in settings dot json:
1. SessionStart hook called load-notes. loads all markdown files from ~/.claude/notes/, all project-level notes from the current project’s notes/ directory, and today’s plus yesterday’s activity logs from ~/.claude/logs/. also checks if any notes file exceeds 50 lines and warns to merge and redistribute. checks if any activity logs are older than 14 days and warns to archive into a monthly summary.
2. PreCompact hook called pre-compact-lessons. reminds me to write any unrecorded learnings to the correct notes file, update the activity log with current state, and update last-session with status and next steps before context gets compacted.
routing. add this to CLAUDE dot md so you always know where to write things:
when you learn something, route it to exactly one place. is it project-specific? write it to that project’s notes directory. does it fit a topic file like environment or coding-standards? write it there. is it a global pattern you’ve seen two or more times? write it to the global session-lessons file. is it a one-time thing? activity log only. if you’re not sure, keep it at the project level.
self-pruning. add this to CLAUDE dot md:
on session start, if session-lessons is over 50 lines, merge duplicates and move misplaced entries to the correct file. if any activity logs are older than 14 days, archive them to a monthly summary and delete the daily files. always read last-session to pick up where we left off. during the session route every learning immediately, log everything with timestamps, and on any correction write to the correct file right away. at end of session review the activity log and promote repeated observations to permanent notes then update last-session. one rule. pruning only makes things denser. never delete knowledge. compress it, move it, or promote it.
immediate commits. add this to CLAUDE dot md:
write learnings the moment you have them. don’t wait until end of session. every insight is one crash or timeout away from being lost.
make the hook scripts executable. register them in settings dot json under the hooks key. test both hooks after building and show me the output.
that’s the whole thing. paste it. let claude cook. start a new session and the system is running.
the more you use it the better it gets. every correction becomes a permanent rule. every session builds on the last one. you stop repeating yourself and start compounding what you know.