How I Set Up Claude Code: Context, Instructions, Memory and Skills, Backed Up Every Day
The problem
A coding agent starts every session knowing nothing about you. You tell it again that staging is shared, that commits are one line, that it should run the tests before it says “done”. It does better for an hour, then the session ends and the lesson is gone.
The fix is to write those lessons down where the agent reads them. Over a few months that file set becomes the most valuable thing on your laptop: rules, notes on each project, step-by-step procedures. Then two new problems show up. Everything you add costs tokens in every session. And all of it lives in one folder that nothing backs up.
This post is my setup for both. It is generic and every example is invented, but the structure, the numbers and the backup are what I run every day.
Four words first
Context is everything the model sees in one request: a system prompt, tool definitions, your files, the conversation so far. Part of it is fixed. It is there before you type anything, in every session. That fixed part is what this post is about, because you control it.
Instructions are CLAUDE.md files. Claude Code reads ~/.claude/CLAUDE.md for every project, plus a CLAUDE.md in the project and every folder above it, at launch. They are your standing rules. The memory docs suggest keeping each file under about 200 lines.
Memory is what Claude writes for itself. Auto memory lives in ~/.claude/projects/<project>/memory/. Only the index, MEMORY.md, is loaded at the start: its first 200 lines or 25 KB. Each line points at a note file, and Claude opens a note only when the line looks relevant.
Skills are procedures. A skill is a folder with a SKILL.md and, if needed, scripts. At the start Claude sees only each skill’s one-line description. The body loads when the skill is used. The skills docs call this progressive disclosure.
| Loaded | Written by | Good for | |
|---|---|---|---|
| CLAUDE.md | Every session, in full | You | Rules for every project |
| MEMORY.md index | Every session, first 200 lines | Claude, you review | Pointers to project facts |
| Memory note | When relevant | Claude | One gotcha, decision or resource name |
| Skill description | Every session, one line | You | Telling Claude the skill exists |
| Skill body and scripts | When used | You | A procedure with steps and commands |
Why it matters: the loop
The setup is a loop, not a document. You work, Claude gets something wrong, you correct it. Then you decide where that correction lives, so the next session starts with it:
- A rule for every project goes in
CLAUDE.md. “Run the tests and show the output before saying done.” - A fact about one project goes in memory. “The payment sandbox returns 503 on Sundays.”
- A procedure goes in a skill. “When a pod restarts, collect these four things, then read them in this order.”
- Something that must never happen goes in a permission rule, not a sentence. Instructions are read and usually followed. A
denyrule is enforced.
Each turn of the loop is small. After a few months the agent knows how you work, which commands are safe, and the traps in each project. The rest of this post builds each piece, then measures what it costs and backs it up.
What you will build
A ~/.claude folder with global instructions, two skills, project memory and permission rules. A script that measures how much context each session starts with. A sync that pushes the folder to a private GitHub repo after every session and once a day, encrypted with your GPG key, and refuses to push anything that looks like a secret.
All of it is in snigdhasambitak/claude-code-setup, with a test that runs the whole backup in a throwaway home folder.
What you need, with the versions I tested:
- Claude Code 2.1.337
- git-crypt 0.8.0 and GnuPG 2.4.8 (
brew install git-crypt gnupg) - Python 3, for the measuring script
- An empty private repo on GitHub
Part 1: instructions
Keep CLAUDE.md to rules that change what Claude does. Mine are grouped by moment: before changing anything, while doing the work, before saying it is done.
## Before saying it is done
- Define what "done" means at the start. Run the tests or the real command and show the output.
- If you could not verify something, say so in the first line.
- Mark claims you have not checked as inferred or guess.
View lines 17 to 21 of CLAUDE.md on GitHub
The last rule matters most. Without it, a guess about why a build failed reads exactly like a proven fact.
What to leave out: anything a linter, a test or the repo already enforces, and long explanations. Every line is paid for in every session. Run /context in a session to see how much your memory files take.
Part 2: memory
You do not write memory notes yourself. You tell Claude to remember something, or it decides a fact is worth keeping. What you control is the format, through a rule in CLAUDE.md. Mine asks for one fact per file, with why it matters and how to apply it:
---
name: staging-db-shared
description: The staging database is shared by three teams, so migrations only run from CI
metadata:
type: project
---
Staging Postgres is shared with the search and billing teams. A migration run from a laptop locked the orders table for 20 minutes once.
**Why:** Other teams' tests fail when our schema changes without warning.
**How to apply:** Never run `make migrate` against staging. Open a PR, CI runs it in the deploy job. See [[release-flow]].
View staging-db-shared.md on GitHub
The index has one line per note. Only this file is loaded at the start, so the text after the colon has to say enough for Claude to know when to open the note:
- [Staging DB is shared](staging-db-shared.md): never run migrations against staging from a laptop
- [Payment sandbox quirk](payment-sandbox-503.md): sandbox returns 503 on Sundays, not a bug in our retry
- [Release flow](release-flow.md): tag on main, CI builds, no manual image pushes
Two habits keep memory useful. Update a note when it turns out wrong, instead of adding a second one. And do not save what the repo already says: the code and the git log are always more current than a note.
Part 3: skills
A skill is worth writing the second time you explain the same procedure. The description is the part Claude always sees, so it says when to use the skill, not what is inside:
---
name: k8s-crashloop
description: Use when a Kubernetes pod is in CrashLoopBackOff, keeps restarting, or is OOMKilled. Gives the order of checks and a script that collects the evidence in one call.
---
View the k8s-crashloop skill on GitHub
The body names the order of checks, and a bundled script collects the evidence in one call, so Claude does not improvise five kubectl commands:
kubectl -n "$ns" get pod "$pod" -o jsonpath='{range .status.containerStatuses[*]}{.name}{"\t"}{.restartCount}{"\t"}{.lastState.terminated.reason}{"\t"}{.lastState.terminated.exitCode}{"\n"}{end}'
kubectl -n "$ns" logs "$pod" --previous --tail=50 --all-containers || true
The second example, pr-description, has no script. It is just the house format for pull requests, with an example. Most of my skills look like that.
Part 4: permissions
Rules in settings.json are checked before a tool runs. The permissions docs evaluate them in order: deny, then ask, then allow. So a deny always wins.
"permissions": {
"allow": [
"Bash(git status)",
"Bash(git diff *)",
"Bash(kubectl get *)",
"Bash(kubectl logs *)"
],
"ask": [
"Bash(git push *)",
"Bash(kubectl apply *)",
"Bash(kubectl delete *)"
],
"deny": [
"Read(./.env)",
"Read(~/.ssh/**)",
"Bash(git push --force *)",
"Bash(printenv)",
"Bash(env)"
]
}
View the full rules in settings.json on GitHub
Read-only commands run without a prompt. Anything that changes shared state asks. Reading secrets and dumping the environment are denied, because a secret printed once is in the transcript for good.
Part 5: measure what a session starts with
Every session pays for the fixed context before you type. Claude Code keeps a transcript per session in ~/.claude/projects/, and the first model response in each one records how many input tokens that first request had. This script reads that number from every session and prints the median per month:
python3 scripts/startup-tokens.py
View startup-tokens.py on GitHub
On my laptop, with about 18 KB of CLAUDE.md and 14 skills:
month sessions median min
2026-11 333 37648 30397
2026-12 84 39987 33463
The first message includes your first prompt, so treat the minimum as the floor. When the median climbs, run /context and look at what grew. It is usually CLAUDE.md or a memory index that nobody pruned.
Part 6: back it up every day, encrypted
Only allowlisted files
~/.claude also holds your login token, every prompt you typed (history.jsonl) and full session transcripts. None of that should leave the laptop. So the .gitignore ignores everything, then names what may be synced:
*
!.gitignore
!.gitattributes
!README.md
!CLAUDE.md
!settings.json
!sync/
!sync/**
!skills/
!skills/**
!projects/
!projects/*/
!projects/*/memory/
!projects/*/memory/**
!.git-crypt/
!.git-crypt/**
A new file that Claude Code adds in a later version stays local until you name it. A blocklist would sync it.
Encrypted with your GPG key
git-crypt encrypts files when Git stores them and decrypts them when you check them out. Your working copy stays plain text, and the repo only has ciphertext. Everything is encrypted except the files Git needs to read:
* filter=git-crypt diff=git-crypt
.gitattributes !filter !diff
.gitignore !filter !diff
README.md !filter !diff
.git-crypt/** !filter !diff
Then make ~/.claude a repo and give your GPG key access:
cd ~/.claude
git init -b main
git remote add origin [email protected]:<you>/claude-config.git
git-crypt init
git-crypt add-gpg-user <your GPG key fingerprint>
add-gpg-user commits a copy of the repo key, encrypted to your GPG key, under .git-crypt/. Anyone with your GPG private key can unlock the repo, and nobody else can, including GitHub.
A sync that refuses secrets
The script stages the allowlisted files, scans them for anything that looks like a secret, commits and pushes:
git add -A
if ! git diff --cached --quiet; then
SECRET_RE='(AKIA[0-9A-Z]{16}|...|-----BEGIN [A-Z ]*PRIVATE KEY|ghp_[A-Za-z0-9]{30,}|...)'
hits=$(git diff --cached --name-only -z | xargs -0 grep -lIE "$SECRET_RE" 2>/dev/null)
if [ -n "$hits" ]; then
git reset -q
echo "$(date '+%F %T') blocked, possible secret in: $(echo $hits)" >> "$LOG"
exit 0
fi
git commit -q -m "Sync memory and skills" || exit 0
fi
# Push even with nothing new, so a push that failed offline is retried.
git push -q origin "HEAD:$BRANCH" >> "$LOG" 2>&1 || echo "$(date '+%F %T') push failed, will retry next run" >> "$LOG"
View lines 14 to 28 of push.sh on GitHub
Encryption already protects the repo, so why scan? Because a key in a memory note is a problem even encrypted: it now lives in Git history on another server. The scan stops it at the door and the log says which file.
When it runs
A SessionEnd hook runs the sync when a session ends:
"hooks": {
"SessionEnd": [
{ "hooks": [{ "type": "command", "command": "$HOME/.claude/sync/push.sh", "async": true }] }
]
}
View lines 27 to 39 of settings.json on GitHub
async is not optional here. The hooks docs give all SessionEnd hooks a shared 1.5 second budget, and a push over the network can take longer. A daily launchd job at 18:00 catches the days you leave a session open:
cp ~/.claude/sync/claude-config-sync.plist ~/Library/LaunchAgents/
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/claude-config-sync.plist
Restore on a new laptop
Import your GPG private key, then:
git clone [email protected]:<you>/claude-config.git ~/.claude
cd ~/.claude && git-crypt unlock
head -3 CLAUDE.md
If head prints binary, the unlock did not happen.
Part 7: test it
make test builds a fake home folder with a throwaway GPG key and a local bare repo as the remote, and checks every promise above. It adds the files that must never leave, a fake AWS key in a memory note, and an offline push:
ok first run pushes
ok allowlisted files are synced
ok credentials, history, transcripts and local settings stay local
ok CLAUDE.md is encrypted on the remote
ok .gitignore stays readable
ok a staged AWS key blocks the sync
ok the block is logged with the file name
ok an offline push is logged, the commit stays local
ok the next run pushes it with nothing new to commit
ok a fresh clone is unreadable without the key
ok git-crypt unlock with the GPG key restores it
ok the restored memory has the latest note
ok startup-tokens reads only the first request of each session
PASS 13/13
What can go wrong
add-gpg-user fails with “‘git add’ failed”. With an allowlist .gitignore, Git ignores the .git-crypt/ folder that git-crypt tries to commit:
The following paths are ignored by one of your .gitignore files:
.git-crypt
Error: 'git add' failed
Add !.git-crypt/ and !.git-crypt/** to the allowlist.
Files committed before .gitattributes stay readable in history. git-crypt encrypts from the commit where the rule exists. If you committed CLAUDE.md first, the first commit still holds it in plain text. Add .gitattributes and run git-crypt init before the first commit, and check with git-crypt status.
A failed push waits for the next change. My first version exited early when nothing new was staged, so a push that failed on a plane was only retried after the next edit. The script now pushes on every run.
The hook is cut off. Without "async": true, the push shares the 1.5 second SessionEnd budget and is stopped mid-way.
Memory goes missing on a new laptop. Memory folders are named after the absolute project path, for example -Users-me-code-shop-api. A different username or folder layout means a different name. Rename the folders after a restore.
A permission rule is matched on text. Bash(git push *) does not match git -C . push. The docs say so plainly: rules reduce accidents, they are not a security boundary.
The scan misses a secret it has no pattern for. A regex only knows the formats you list. It is the first layer, encryption is the second, and not pasting secrets into chats is the first rule.
You lose the GPG key. Then the backup is noise. Export the private key (gpg --export-secret-keys --armor <id>) into your password manager before you need it. If you would rather not use GPG, git-crypt export-key writes a symmetric key file that works the same way with git-crypt unlock <file>.
What I would do today
Start small. A CLAUDE.md with ten rules, auto memory on, and one skill for the procedure you explain most often. Measure the startup context once so you know your baseline. Set up the backup on day one, because the setup is only worth backing up after months, and by then you have forgotten how fragile one folder is.
Undo
launchctl bootout gui/$(id -u)/local.claude-config-sync
rm ~/Library/LaunchAgents/claude-config-sync.plist
rm -rf ~/.claude/.git ~/.claude/.git-crypt
Remove the SessionEnd hook from settings.json. Your instructions, memory and skills stay where they are.