Claude Code keeps notes about your past work in memory files, plus an index file (MEMORY.md) that it loads into every request. When Claude Code runs on a local 9B model, that index makes every request slow. On a 16GB MacBook (Apple M5) running qwen3.5:9b through Ollama, I stopped loading the whole index (78 memory files in my case). Instead, a hook attaches only the memory lines related to the prompt. The results:
| Full index | Relevant lines | |
|---|---|---|
| First-request input tokens (median, 50 pairs) | 11,450 | 8,030 |
| Time to finish one question (median, 50 pairs) | 90.4s | 56.4s |
| Memory questions answered correctly (45) | 20 | 37 |
| Made-up answers (45) | 11 | 3 |
A pair is one question run once on each setup. Timing uses 50 pairs: the 45 memory questions plus 5 questions unrelated to memory. A made-up answer states a specific fact about my records that contradicts the memory files. The new setup was faster in 42 of 50 pairs. On correctness, only the old setup was right on 4 questions and only the new one on 21. Both gaps are statistically significant on paired tests (Wilcoxon and McNemar, p<0.001).
Why the full index hurts
The index (MEMORY.md) is a list of links to memory files. It is long, and it rarely holds the answer itself. So the local model:
- reads about 11k tokens before your question (about 38s on a cold start in my runs), then
- still has to open files to find the actual fact, spending more turns.
What the hook does
It is a UserPromptSubmit hook, plain python3, no packages.
- Splits memory files into line chunks.
- Scores each chunk against the prompt two ways: local embeddings (
bge-m3on Ollama) and keyword search (BM25). The two rankings are merged with RRF (reciprocal rank fusion, which adds up each chunk's rank from both lists). - Attaches the best chunks with neighboring lines, up to 2,400 characters, each with the date it was written.
- Attaches nothing for short replies like "ok do it" (fewer than 3 search terms).
- If Ollama is down it falls back to keywords. If that fails it attaches nothing. It never blocks Claude Code.
Retrieval check on 45 questions never used for tuning: the expected answer appeared in the attached text for 36. My earlier keyword-only hook got 17.
How I measured
I wanted numbers I could not tune my way into.
- Criteria were written down before each run, and every change was logged with its reason.
- Questions were written by a separate agent that never saw the retrieval code. Any question set I looked at while tuning was retired. Final numbers come from sets opened once.
- Old and new setups ran each question back to back, alternating which went first (ABBA).
- Two judges (Sonnet and Opus) graded answers without knowing which setup wrote them. They agreed on 89 of 90 verdicts. For the one split, I used the stricter verdict.
- Charger and sleep state were logged for every run. Closing the lid once put the Mac to sleep mid-run, which made 3 timings wrong, so I reran those with the charger in and the lid open.
The first 20 questions went 11 → 17, but that was not significant (p=0.15). So I wrote down a follow-up plan before running it: 25 unused questions. They came out 9 → 20 (p=0.003), and the pooled 45 are in the table above.
What did not work
| Tried | Result |
|---|---|
| A rule to skip prompts unrelated to memory | Worked on one question set, failed on the next. Removed as overfitting |
| Also attaching later updates from the same file | Added unrelated lines on dev data. Not shipped |
I also checked 40 random real prompts against the memory files. The attached text was helpful for 8, neutral for 23, and possibly misleading for 9. Most misleading cases came from files where an old fact sits next to its update. The hook now shows dates and tells the model to verify old facts, but the bigger fix is on the memory side: when a fact changes, edit the old line instead of appending a new one.
Try it
Needs macOS, Ollama, Claude Code and python3.
git clone https://github.com/JustJinoIT/claude-local-kit && cd claude-local-kit ./install.sh # pulls qwen3.5:9b (64k context) and bge-m3 claude-local # Claude Code on the local model, in the current folder It does not touch your normal Claude Code settings. ./uninstall.sh moves everything to the Trash.
There is also cl. When a Claude session stops, for example at a usage limit, run cl in the same folder. It builds a handoff note from that conversation (first request, last prompts, edited files) and continues in local mode.
One setting worth changing: Ollama unloads a model after 5 minutes idle by default, and the next request stalls while it reloads.
launchctl setenv OLLAMA_KEEP_ALIVE 4h # then restart Ollama The repo's bench/ folder has the preregistration log, the scripts, and a fictional memory set, so you can rerun the retrieval test yourself.
Before you load memory into a local model
- How many tokens does your first request carry before the question?
- Does your memory index contain answers, or only pointers to files?
- When a fact changes, do you edit the old line or append a new one?
- Did you compare setups on questions you never tuned on?
Written with help from an AI assistant (Claude). I checked every number against my measurement logs.
