ANSWERS · AGENT MEMORY: SETUP

How does Claude Code memory work, and when do you need more?

AUTHOR · XERJ documentation team · REVIEWED BY · XERJ engineering team · PUBLISHED · 2026-10-30 · UPDATED · 2026-10-30
SHORT ANSWER · UPDATED 2026-10-30

How does Claude Code memory work? Claude Code loads CLAUDE.md files you write and an auto memory index Claude writes at the start of every session. For more history than fits in context, or memory shared across agents, add an MCP memory server such as XERJ that the agent searches on demand.

Key number: Claude Code loads the first 200 lines or 25KB of MEMORY.md at the start of each session; topic files are read on demand. (as of 2026-10-30 · source: code.claude.com/docs/en/memory)

TL;DR Claude Code has two built-in memory systems. CLAUDE.md files hold rules you write, and auto memory holds notes Claude writes. Both load at the start of each session. When you need search over more than fits in context, add an MCP memory server that the agent calls only when it needs a fact.

Diagram

Where Claude Code memory comes from

Three sources feed a Claude Code session. CLAUDE.md files you write and the auto memory index Claude writes load at session start. An MCP memory server such as XERJ is read only when the agent calls it. CLAUDE.md files You write them Load every session Auto memory Claude writes it MEMORY.md loads at start Session context what the model sees MCP memory server e.g. XERJ Read only when called
The two built-in systems load on their own. A memory server adds search over more than fits in context, and only costs tokens when the agent asks. Based on the Claude Code memory docs, checked 2026-10-30.
PASTE THIS TO YOUR AI AGENT — IT DOES THE REST Act as a coding agent. Read https://xerj.org/llms.txt, install XERJ, start a local node, run claude mcp add --scope user xerj -- xerj mcp, store one fact about this repo with xerj_memory_store, start a new session, recall it with xerj_memory_recall, and report the result.

What loads at session start

Step through it below. Each step is one source Claude Code reads.

Try it: step through a session start

What Claude Code reads, in order

  1. 1. Managed policy

    An admin can place a CLAUDE.md for the whole machine, for example /etc/claude-code/CLAUDE.md on Linux. You cannot turn it off.

  2. 2. Your user file

    ~/.claude/CLAUDE.md holds your own habits for every project. Example: "Explain shell commands before running them."

  3. 3. The project file

    ./CLAUDE.md or ./.claude/CLAUDE.md is shared with your team in git. Example: "Run tests with make test." Keep it under 200 lines.

  4. 4. Your local notes and rules

    ./CLAUDE.local.md holds private notes for this project. Files in .claude/rules/ can apply only to some paths.

  5. 5. Auto memory index

    Claude reads the first 200 lines or 25KB of MEMORY.md from ~/.claude/projects/<project>/memory/. Example entry: "always use pnpm, not npm".

  6. 6. Topic files, on demand

    Notes Claude moved out of MEMORY.md are read only when they look relevant, so they do not fill the context at start.

  7. 7. Memory server, when called

    Nothing loads at start. When the agent needs an old fact, it calls a tool such as xerj_memory_recall and gets the best matches back.

Steps 1 to 6 come from the Claude Code memory docs (code.claude.com/docs/en/memory), checked 2026-10-30. Step 7 is optional and only happens if you add an MCP memory server.

The three options compared

CLAUDE.mdAuto memoryMCP memory server
Who writes itYouClaudeThe agent, through a tool
When it loadsEvery session, in fullMEMORY.md at start; topics on demandOnly when called
Size guideUnder 200 lines per fileFirst 200 lines or 25KB of MEMORY.mdLimited by disk, not context
SharedThrough git, for project filesOne machine, all worktrees of a repoAny agent that can reach the server
Best forRules for every taskLessons Claude picks upFacts you look up later

When the built-ins are enough

For one person, one repo and one agent, start with CLAUDE.md and auto memory. Put build commands and code style in the project CLAUDE.md. Let auto memory collect corrections like "always use pnpm, not npm". Check what it saved with /memory now and then.

When to add a memory server

Add one when any of these come up:

A memory server stores facts outside the context window. The agent calls it with a query and gets the top matches. See what an MCP memory server is and memory across sessions for more.

A real recall example with XERJ

We stored three memories in a local XERJ node and queried them with default settings, which use BM25 full-text search.

QueryResult
pnpm"User prefers pnpm over npm", score 1.0526
what port does the API use"The API runs on port 9200", score 2.9425
which package managerNo match
deploy dayNo match

Keyword recall finds exact words fast and shows why it matched. It misses a question that uses other words. For those, ask for semantic or hybrid recall, or store the words you expect to search for.

How to try it with XERJ

Use the Copy the prompt button and paste it into Claude Code. It installs XERJ, adds it as an MCP server for all your projects and checks recall in a new session. By hand:

curl -fsSL https://xerj.org/get | sh
claude mcp add --scope user xerj -- xerj mcp

The same node can also index your repo for code search and your past Claude Code sessions. To compare XERJ with claude-mem and MemPalace, see this comparison.

Sources checked

FAQ

Where does Claude Code store auto memory?

In ~/.claude/projects/<project>/memory/ on your machine. A MEMORY.md file is the index and topic files hold the details. All worktrees of the same repo share it.

How long should CLAUDE.md be?

The docs suggest under 200 lines per file. Longer files use more context and Claude may follow them less closely.

How do I turn auto memory off?

Use the /memory command to toggle it, or set CLAUDE_CODE_DISABLE_AUTO_MEMORY=1.

Does Claude Code read AGENTS.md?

Yes, when a directory has no CLAUDE.md, recent versions read AGENTS.md instead.

When do I need a memory server?

When notes outgrow what should load every session, when you want search over old sessions, or when several agents should share one memory.

Does XERJ replace CLAUDE.md?

No. Keep rules that apply every time in CLAUDE.md. Use XERJ for facts the agent looks up only when needed.

Related