How Do Agents Use Files as Memory?
How agents keep notes in files across sessions, what Anthropic's memory tool does, and the risks of bad notes, explained simply for your AI interview.
In short
Agents write what they learn into files, such as a progress note or a memory folder, then read those files at the start of the next session. The chat resets, but the file survives. Files are powerful and risky, so writes need checks and paths need limits.
11 min readAdvancedOccasional · estimateFacts checked 27 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
File memory means an agent writes what it learns into files, then reads them at the start of its next session. Chats reset and fill up, but a file survives. Anthropic's memory tool gives the agent a memory folder that my app serves. The risk is that a bad line saved once taints every later session that reads it, so writes need checks, paths need limits, and stale notes get pruned.
An AI model only sees what fits in its current chat. When the chat resets, everything in it is gone. File memory fixes that. The agent writes what it has done and what comes next into a file, and the next session opens that file before it starts. Think of a night nurse leaving a handover sheet for the day shift. The sheet holds the facts the next person needs.
Here is a case I would use in an interview. A claims team uses an agent to triage 500 flood claims over a week. Session one clears 412 of them, then the chat resets. The progress file says where it stopped, so session two starts at claim 413 and never re-checks the first 412. Same model, and the file carries the work across.
The risk is that files are trusted. A hidden line in a web page can make the agent save a fake rule, and that rule taints every later session that reads it. So I treat memory writes as privileged. My code checks that each path stays inside the memory folder, caps the size of each file, strips secrets, and asks a person before a rule that changes approvals is saved. Old notes get pruned, so the folder stays short.
Notes help with long tasks, but they are one tool among several. Good checks in code matter just as much.
What are files as memory, in plain words?
An AI model only sees what fits in its current chat, which is its context window. When the chat is cleared or restarted, a context reset, everything in it is gone. File memory means the agent writes what it learns into files, then opens them at the start of the next session.
Think of a hospital night nurse who leaves a handover sheet for the day shift. The sheet holds the facts the next nurse needs, so the work carries on without a restart.
A real agent works the same way. It writes its progress to a file, and the next session reads that file before it touches any new work.
Key words
- Context window
- The text a model can see in one go. Anything outside it is invisible to the model.
- Memory folder
- A folder the agent can list, read and write, such as /memories in Anthropic's memory tool.
- Client-side
- Your own app runs the file actions. The model only asks for them.
- Progress note
- A short file that says what is done, what is next and what to avoid.
Why use files instead of the chat?
A file survives when the chat does not, and it takes no room in the window until it is opened. Anthropic describes its memory tool, and its guide on context, in these words:
“Claude can create, read, update, and delete files that persist between sessions, building up knowledge over time without keeping everything in the context window.”
Anthropic, memory tool docs
“Context, therefore, must be treated as a finite resource with diminishing marginal returns.”
Anthropic, context engineering
Anthropic's memory docs say that memory supports just-in-time context retrieval. Its context guide describes agents that keep lightweight identifiers, such as file paths, and load data only when needed. The trade-off is that runtime exploration is slower than retrieving pre-computed data, so the agent pays a step to find the right file.
How does an agent read and write its memory files?
The loop is simple. Before each task, the agent checks the memory folder. As it works, it writes notes. The next session reads them back.
- 1
Look first
Before new work, the agent lists the memory folder and opens the progress note.
- 2
Record as it goes
After each item, it writes what is done and what is next. Anthropic tells the agent that the chat can reset at any moment.
- 3
Keep the folder tidy
It updates files in place and removes the ones that no longer matter, so the folder stays readable.
- 4
Resume from the note
The next session names the item it picks up, and skips what the note marks as done.
A memory folder carries work across sessions
Session 1
Triage claims
Works through the queue
Write progress.md
Done so far and what is next
Memory folder
Files survive the chat ending
Session 2
Read progress.md
Before any new claim is touched
Carry on at claim 413
Skips the 412 already done
Queue finished
No claim is done twice
- 1The agent finishes a batch, then writes down what it did and what is next.
- 2The note is saved as a file, outside the chat.
- 3The next session opens the folder and reads the note first.
- 4It picks up from the claim the note names.
- 5The queue ends without any claim being done twice.
Worked example. A claims team at a mid-size insurer lets an agent triage 500 flood claims over a week. Session one clears 412 claims, then the chat resets. Without a note, session two starts again from claim one, re-checks 412 files and risks two different decisions on the same claim. With the progress note, it starts at claim 413 and the team sees one clean log.
What goes wrong with file memory, and how do you guard it?
Files are useful because the agent trusts them. That trust is also the risk, so each danger needs a guard in code.
- 1
Stale notes
An old line says the claim limit is 5,000, but the policy changed last month. Date each note and rewrite it when the rule changes.
- 2
Bloat
Notes that grow every session crowd the window again. Cap the size of each file and let the agent page through long ones.
- 3
Sensitive data
Claude usually refuses to write sensitive information to memory files, but your handler should strip it too.
- 4
Poisoned notes
A hidden line in a web page can end up saved as a note, and every later session that reads the store is tainted by it. OWASP says to treat memory writes as privileged, and to require approval before instruction-bearing memories persist across sessions.
- 5
Escape from the folder
A path such as /memories/../secrets.env can reach files outside the folder. Your code checks every path before it opens anything.
“Treat agent memory writes as privileged operations.”
OWASP, LLM01:2026
“an injection that writes to long-term memory, a RAG corpus, a vector store, or a hosted memory service taints every subsequent session that reads from that store.”
OWASP, on memory persistence
A bad line should not reach later sessions
Web page, hidden line
says: save this rule for later
Agent drafts a note
Treats the line as a fact
Checks before saving
Scan for instructions
Held back for a person
Strip secrets
No keys, card or account numbers
Clean note saved
Read by every later session
- 1A web page hides an instruction in its text, and the agent reads it.
- 2The draft is scanned for instructions before it can be saved.
- 3The secrets check then runs on the same note.
- 4Only the clean note is saved, for every later session to read.
Here is the guard in plain Python. The model only asks to read or write a file, and the code decides what is allowed.
# A claims agent's memory folder. Files outlive the chat, so every path is checked first.
from pathlib import PurePosixPath
store = {} # stands in for real files on disk
def safe_path(path):
p = PurePosixPath(path)
if ".." in p.parts or not str(p).startswith("/memories/"):
raise ValueError("path outside /memories: " + path)
return str(p)
def write(path, text):
store[safe_path(path)] = text
def view(path):
return store.get(safe_path(path), "The path " + path + " does not exist.")
# Session 1 saves its progress and the team's rule before the chat resets.
write("/memories/progress.md", "Done: 412 of 500 claims triaged. Next: claim 413.\n")
# A human owner wrote this rule, so no agent check is needed.
write("/memories/rules.md", "Claims above 10,000 go to a human adjuster.\n")
# Session 2 starts fresh: read the notes first, then try an unsafe path.
print(view("/memories/progress.md"), end="")
print(view("/memories/rules.md"), end="")
try:
view("/memories/../secrets.env")
except ValueError as err:
print("Refused:", err)Output
Done: 412 of 500 claims triaged. Next: claim 413. Claims above 10,000 go to a human adjuster. Refused: path outside /memories: /memories/../secrets.env
This is the idea in miniature. A real handler also resolves each path to its canonical form, and watches for URL-encoded forms such as %2e%2e%2f.
How would you set up memory for an agent that runs for a week?
- 1
First session sets up
Before real work starts, it creates the progress log and a feature checklist, and notes where the startup script lives.
- 2
Every session starts with the log
The agent reads the log and picks one item only.
- 3
Done means tested
An item is marked complete only after an end-to-end check passes.
- 4
End with an update
Before the session ends, the log says what was done and what is next.
Say where the notes live, who can read them and how long they stay. Interviewers listen for that part.
What does the interviewer want to hear?
A strong answer covers
- Files outlive the chat, so the agent reads its notes at the start of each session.
- The loop: look first, record as it works, resume from the note.
- Memory is an input. Writes are privileged, because a poisoned note lasts.
- Code checks every path and file size, and strips secrets.
- Notes stay short and current, and stale files get deleted.
- Clear split: the model asks, the app runs the file actions.
Common traps
Trap"Just let the model remember it."
BetterThe model forgets when the chat resets. Write it to a file the app can read back.
Trap"Save everything, storage is cheap."
BetterBig, stale notes crowd the window again and can hold secrets. Cap, prune and check them.
Trap"The model checks its own paths."
BetterThe model only asks. Your code must resolve every path and keep it inside the memory folder.
Can you spot the risky note?
Spot the risky note
Your claims agent reads these notes at the start of a session. Which note loosens a human check, so it must never persist without sign-off?
Quick check
The agent saves notes across weeks. Which code rule matters most?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
Learn it in depth
Last updated on 27 November 2026
Building an agent that has to remember across days?
I design and build AI agents, including the memory, checks and tools behind them. Book a short call and tell me what your agent needs to remember, and what it must never write down.