Lesson 56.3 · 3 of 4 · 4 min read · Reviewed 24 Nov 2026
File-based memory: project memory files and memory tools
By the end, you can
- Use file-based memory such as CLAUDE.md and Claude's memory tool.
- Know when plain files are the best memory.
Sometimes the best memory is a text file.
CLAUDE.md in Claude Code
Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands. reads memory files at the start of a session: your user-level file, the project’s CLAUDE.mdCLAUDE.md: A markdown file of standing instructions that Claude Code loads automatically, at user, project or directory level., and files in subdirectories as it works there. Lesson 62.2 shows how subdirectory files add local knowledge.
Good for: conventions, commands, architecture notes, “don’t touch” lists. It’s procedural and semantic memory for the project, reviewed like code.
The memory tool
Claude’s memory tool gives an agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. file operations on a memory directory: view, create, edit and delete files. Your application decides where those files are actually stored, such as local disk or a database, and handles each operation.
agent: view /memories -> sees notes.md, prefs.md
agent: read /memories/notes.md -> picks up where it left off
agent: edit /memories/notes.md -> records new findings
This lets an agent keep notes across sessions without you designing a schema first. Because your code executes the operations, you control access, size limits and retention.
When files win
| Need | Files fit because |
|---|---|
| People must read and edit memory | Plain text |
| Changes need review | Version control |
| Memory is small and curated | No search needed |
| Agent keeps working notes | Simple read and write |
When they don’t
Thousands of memories, fuzzy search, or many users: use a database or vector storeVector database: A database built to store embeddings and find the nearest ones to a query quickly., possibly alongside files.
Key points
- Plain files are a simple, readable, versionable memory: easy for people and agents to inspect and edit.
- Claude Code loads CLAUDE.md files as project and user memory at the start of each session.
- Claude's memory tool lets an agent create, read and update files in a memory directory that your app stores.
Implement a safe memory directory handler
Write the application side of a file-style memory tool: carry out view, create, str_replace and delete on per-user memory directories, and refuse anything outside them.
You will have: A handle(user_id, command) function backed by an in-memory dictionary, and a log of allowed and refused commands.
Steps
- Read the starter: STORE maps paths to file text; each user's root is /memories/<user_id>/.
- Write resolve(user_id, path) that normalises the path with posixpath.normpath and refuses anything outside the user's root.
- Implement view (list files), create, str_replace and delete on top of STORE.
- Refuse files over 2,000 characters and text that looks like a password or key.
- Run the sample commands and print each result.
Done when
- The path with ../ into another user's folder is refused.
- The note containing a password is refused.
- User A's view lists only their own files.
Open in the Playground No API key needed.
Starter code (Python)
import posixpath
STORE = {"/memories/u-b/notes.md": "User B private notes"}
MAX = 2000
def resolve(user_id, path):
root = "/memories/" + user_id + "/"
# TODO: normalise path; refuse it (raise PermissionError) if it is not inside root
return path
def handle(user_id, cmd):
# TODO: support cmd["command"] in view, create, str_replace, delete
return "not implemented"
COMMANDS = [
{"command": "create", "path": "/memories/u-a/prefs.md", "file_text": "Prefers short answers."},
{"command": "str_replace", "path": "/memories/u-a/prefs.md", "old_str": "short", "new_str": "short, plain"},
{"command": "view", "path": "/memories/u-a/"},
{"command": "view", "path": "/memories/u-a/../u-b/notes.md"},
{"command": "create", "path": "/memories/u-a/secret.md", "file_text": "password is hunter2"},
]
for c in COMMANDS:
try:
print(c["command"], c["path"], "->", handle("u-a", c))
except (PermissionError, ValueError) as e:
print(c["command"], c["path"], "-> refused:", e)
Show a solution
import posixpath, re
STORE = {"/memories/u-b/notes.md": "User B private notes"}
MAX = 2000
SECRET = re.compile(r"\b(password|passcode|api key|secret key)\b", re.I)
def resolve(user_id, path):
root = "/memories/" + user_id + "/"
clean = posixpath.normpath(path)
if not (clean + "/").startswith(root):
raise PermissionError("blocked path outside " + root)
return clean
def handle(user_id, cmd):
path = resolve(user_id, cmd["path"])
c = cmd["command"]
if c == "view":
if path in STORE:
return STORE[path]
return sorted(p for p in STORE if p.startswith(path + "/"))
if c == "create":
text = cmd["file_text"]
if len(text) > MAX:
raise ValueError("file too large")
if SECRET.search(text):
raise ValueError("looks like a secret; not stored")
STORE[path] = text
return "created"
if c == "str_replace":
if cmd["old_str"] not in STORE.get(path, ""):
raise ValueError("text not found")
STORE[path] = STORE[path].replace(cmd["old_str"], cmd["new_str"], 1)
return "edited: " + STORE[path]
if c == "delete":
STORE.pop(path, None)
return "deleted"
raise ValueError("unknown command")
COMMANDS = [
{"command": "create", "path": "/memories/u-a/prefs.md", "file_text": "Prefers short answers."},
{"command": "str_replace", "path": "/memories/u-a/prefs.md", "old_str": "short", "new_str": "short, plain"},
{"command": "view", "path": "/memories/u-a/"},
{"command": "view", "path": "/memories/u-a/../u-b/notes.md"},
{"command": "create", "path": "/memories/u-a/secret.md", "file_text": "password is hunter2"},
]
for c in COMMANDS:
try:
print(c["command"], c["path"], "->", handle("u-a", c))
except (PermissionError, ValueError) as e:
print(c["command"], c["path"], "-> refused:", e)