Claude Code that learns from its own sessions
Every Claude Code session starts with no recollection of the last one. Here is how we set it up to write down what it learns, load it next time and run the boring parts on its own.
A new Claude Code session knows nothing about the previous one. The only things it carries over are the files it reads at startup. So the way to make Claude Code “learn” is simple to state: get each session to write useful lessons into those files, keep the files short enough to stay useful, and automate the writing so it happens even when you forget to ask.
This post walks through the setup we use on client projects, from the files Claude reads to a hook that reviews every finished session in the background.
The files Claude Code reads at startup
Claude Code loads CLAUDE.md files when a session starts. They are plain Markdown, and every file that applies gets added to the context, from the broadest to the most specific:
| File | Scope | Shared with the team |
|---|---|---|
~/.claude/CLAUDE.md |
All your projects | No |
./CLAUDE.md or ./.claude/CLAUDE.md |
This repository | Yes, commit it |
./CLAUDE.local.md |
This repository, only you | No, add it to .gitignore |
Files in parent directories load too, and a CLAUDE.md in a subdirectory loads when Claude starts working with files in that folder. A file can pull in another one with @path/to/file, which keeps the main file short and lets you split rules by topic.
Keep each file under about 200 lines. Everything in it costs context on every single session, and a long list of rules is easier for the model to half-follow than a short one.
Auto memory: Claude takes its own notes
Since CLAUDE.md is written by you, it only grows when you take the time. Auto memory is the part Claude writes itself. It is on by default, with one folder per project:
~/.claude/projects/<project>/memory/
├── MEMORY.md # index, one line per note, loaded at start
├── deploy-needs-vpn.md # a note, loaded when relevant
└── prefers-small-prs.md
Each note is a small Markdown file with a type in its frontmatter (user, feedback, project or reference). The index is what Claude reads at the start of every session, and only its first 200 lines (or 25 KB) are loaded, so a tidy index matters more than many notes.
You can steer it directly:
- Say “remember that staging deploys need the VPN” and Claude writes a memory.
- Say “add this to CLAUDE.md” when the rule belongs to the whole team.
- Run
/memoryto see which files are loaded, open the memory folder or turn auto memory off.
The most valuable notes are corrections. When you tell Claude “no, we never mock the database in these tests” and it saves that as a feedback note, the next session gets it right the first time. Our rule of thumb: if you correct the same thing twice, it belongs in memory, and once a memory matters for everyone on the project, move it into the committed CLAUDE.md.
A hook that reviews every session
Asking Claude to remember things works, but only when you remember to ask. Hooks fix that. A hook is a shell command Claude Code runs on an event, such as SessionStart, Stop or SessionEnd, and it receives JSON on stdin with the session id, the working directory and the path to the session transcript.
That is enough to build a session reviewer. When a session ends, a hook starts a second, headless Claude that reads the transcript and updates memory with anything worth keeping.
Register the hook in ~/.claude/settings.json:
{
"hooks": {
"SessionEnd": [
{
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/review-session.sh" }
]
}
]
}
}
Then the script:
#!/usr/bin/env bash
# ~/.claude/hooks/review-session.sh
# The reviewer is a Claude session too; without this guard its own end would start another review.
[ -n "$CLAUDE_REVIEWING" ] && exit 0
input=$(cat)
transcript=$(jq -r '.transcript_path' <<<"$input")
cd "$(jq -r '.cwd' <<<"$input")" || exit 0
# Run in the background so closing the session is not held up by the review
CLAUDE_REVIEWING=1 nohup claude -p \
--allowedTools "Read,Write,Edit" \
"Read the Claude Code session transcript at $transcript.
Find the moments where the user corrected you, where a task took several
attempts, or where you learned a fact about this project that is not
written down yet. Save each lesson that will matter in future sessions
to your auto memory, or update the existing note if there is one.
Skip one-off details, anything already in CLAUDE.md, and any secrets,
tokens or customer data that appear in the transcript." \
>/dev/null 2>&1 &
A few details in there are worth explaining:
- The guard variable. The reviewer is a normal Claude session, so it fires
SessionEndtoo. The environment variable is inherited by the reviewer and its hooks, and stops the chain after one level. - Claude reads the transcript, not your script. Transcripts are JSONL files under
~/.claude/projects/, and the docs warn that the format is internal and can change between versions. Parsing it yourself would break on the next update; handing the path to Claude does not. - Narrow tools.
--allowedTools "Read,Write,Edit"lets the reviewer read the transcript and write notes without asking, and nothing else. - No
--bare. Headless mode has a--bareflag that skips hooks,CLAUDE.mdand memory, which the docs recommend for most scripts. Here we want the opposite: the reviewer needs to see the existing memory so it updates notes instead of writing duplicates.
The review costs one extra headless run per session. For short sessions you can skip it, for example by counting the lines in the transcript first and exiting below a threshold.
Skills: package what works
Memory holds facts and preferences. When a whole workflow repeats, such as “release a new version” or “add a case study page”, turn it into a skill: a folder with a SKILL.md that Claude loads when the task matches.
---
name: add-case-study
description: Add a client case study to the website. Use when asked to publish or draft a case study.
---
1. Add the case study data next to the client in src/site.js.
2. Check that the Markdown twin and llms.txt pick it up.
3. Run npm test and fix anything the build check reports.
Personal skills go in ~/.claude/skills/<name>/SKILL.md, project skills in .claude/skills/<name>/SKILL.md. Until the skill is used, only its description is in the context, so write it as the trigger: what the skill does and when to use it.
You do not need to write skills by hand. At the end of a session where a workflow finally worked, ask: “Turn what we just did into a skill.” Claude has the whole session in context and writes the steps that actually worked, including the dead ends to avoid.
Review the learning once a week
A system that only adds notes gets worse over time. Old notes go stale, two notes contradict each other, and the index grows past the part that gets loaded. Plan a short weekly review:
- Run
/insightsfor a report on your recent sessions: what you worked on, where things failed and which features you could use more. - Open the memory folder with
/memoryand delete notes that are no longer true. - Move notes that every teammate needs into the committed
CLAUDE.md, so they reach the whole team and not only your machine. - Look for skills that never get used, and either improve their
descriptionor delete them.
It takes ten minutes, and it is the step that keeps the memory trustworthy.
Things to watch
- Hooks run with your permissions. A hook is a shell command, so treat hook scripts like any other code you run: keep them in version control and read them before you install someone else’s.
- Transcripts contain everything. Pasted tokens, customer data and error dumps all end up in the transcript. The reviewer prompt above tells Claude to skip secrets; check the first few memories it writes to be sure it does.
- Transcripts expire. Claude Code deletes them after 30 days by default (
cleanupPeriodDaysin settings). Anything worth keeping has to be distilled before then, which is one more reason to review at the end of each session.
Where to start
- Write a short project
CLAUDE.md: how to run, test and deploy, plus the conventions people keep getting wrong. - Say “remember” every time you correct Claude, for a week.
- Add the
SessionEndreview hook once you trust what the memories look like. - Turn the first workflow you repeat into a skill.
After a few weeks, sessions start closer to where the last one ended, and you spend less time repeating yourself. When you want the same idea to run without your laptop at all, the next step is scheduled routines in the cloud, which we cover in Claude Code routines: automation that runs while you sleep.