Set up memory for an agent
1
Create the knowledge base
Create a knowledge base for the agent’s memory and write a short Description of what belongs in it. Publish at least a
README.md that explains how it is organized — see Give the knowledge base its own rules. See Build a knowledge base.2
Grant write access
Open the knowledge base, then the Agents tab. Connect the agent with Read & write access, or change an existing connection to it.Choose Read & write (ask first) instead if a person should approve each write. That only works in web chat; on chat apps, webhooks and automations the agent then cannot write at all. See Use a knowledge base.
3
Decide the memory policy
Open the agent, then the Data access tab. Turn on Auto-record learnings to use the platform’s default policy, or leave it off and write your own in the agent’s
CLAUDE.md or a skill. See Choose a memory policy.How an agent writes memory
An agent with at least one writable knowledge base gets three tools:write_context applies a mode to each file:
Every mode except
create can carry the expected_sha256 that read_context_file returned. If the file changed since it was read, the write is refused as stale instead of silently undoing someone else’s change. By default each file is limited to 256 KB and each call to 20 files.
With Read & write (ask first), a write_context call is a request: web chat shows an approval card with the files, and the write happens only if the person approves it.
What the platform always tells the agent
Whenever the tools are present, DataGen adds tool instructions to the agent’s system prompt, whatever you configure. They list the writable knowledge bases by slug, name and description, and tell the agent to:- treat the knowledge base as the only storage that outlives the sandbox;
- read the knowledge base’s
README.md(or its index file) before changing anything, and follow it; - read a file before changing it, and pass
expected_sha256on an overwrite; - put a whole update in a single
write_contextcall; - never write secrets, credentials, tokens or raw transcripts.
~/.claude/projects/*/memory/), which would otherwise be discarded with the sandbox. An agent with no writable knowledge base keeps that behavior unchanged.
Choose a memory policy
The tool instructions say how to write. Whether the agent records anything, and what, is the agent designer’s decision.- Auto-record learnings (platform default)
- Your own policy
Turn on Auto-record learnings on the agent’s Data access tab. DataGen appends a default policy to the system prompt: before finishing a task, record what is worth remembering — facts about the workspace and people in it, what worked and what failed, preferences the user stated — into the knowledge base whose description or
README.md covers it. The policy also tells the agent to:- edit what is there rather than piling up new files;
- add any new file to the knowledge base’s index in the same call;
- write facts and procedures, never instructions to other agents;
- summarize in its own words, never paste raw transcripts or tool output;
- never claim it remembered something unless a
write_contextcall succeeded; - on unattended runs (automations, webhooks, chat apps), do the memory update as the last step.
Give the knowledge base its own rules
Every agent with write access is told to read the knowledge base’sREADME.md before changing it. That makes the README.md the place for rules that belong to the knowledge base rather than to one agent, and every agent that writes to it — including ones added later — follows them.
README.md
How an agent reads memory
Memory is read the same way as any knowledge base:- At the start of each run, the latest published version is mounted read-only at
/mnt/context/contexts/<slug>/. See How the agent sees the files. - During a run,
read_context_filealways returns the current file, including writes made earlier in the same run. The mounted copy does not show a write until the mount refreshes, so the tool’s result is the confirmation. - Other agents that can read the knowledge base see what this agent wrote from their next turn. They are told that knowledge base files may have been written by agents, and to check the version history when something looks wrong.
Inspect and correct memory
Agent writes are stored as versions of the knowledge base in.context/<slug>/, so you review them on the knowledge base’s detail page:
- Files shows what is live right now — exactly what agents read.
- Versions lists every version. Versions created by an agent carry an Agent write badge with the file path, or the number of files the call changed.
- To undo a bad write, use Roll back to this version on the last good version. It is published as a new version, and the version you rolled back from keeps its files, so you can restore it later.
Next
Agents overview
Combine knowledge bases, skills, memory and tools in an agent.
Build an app
Use knowledge bases as reference material in the App Builder.