Claude Code Memory System
Every time a Claude Code session ends, the context is cleared.
Sometimes we have to tell Claude every time "use pnpm instead of npm", "our indentation is 2 spaces"?Memory System exists precisely for this.
What is Claude Code's memory system?
Claude Code has no automatic memory across sessions - each new session starts with a completely fresh context window.
The memory system uses two complementary mechanisms to allow knowledge topersist across sessions, and be automatically loaded at the start of each conversation:
| Mechanism | Who writes it | What it's suitable for |
|---|---|---|
| CLAUDE.md files | Written manually by you (the developer) | Project standards, team conventions, build commands, etc. |
| Auto Memory | Automatically written by Claude | Experience accumulated from your corrections and preferences |
Both mechanisms are loaded into the context at the start of each session. Claude treats them asreference context, not mandatory configuration - the more specific and concise the instructions, the more consistently Claude follows them.
The hierarchy of memory files
Claude Code uses afour-level memory hierarchy, with priority from highest to lowest:
1. 企业级配置(Enterprise policy) ← 最高优先级,只读
2. 用户级 CLAUDE.md ← ~/.claude/CLAUDE.md,对所有项目生效
3. 项目级 CLAUDE.md ← 项目根目录,随 Git 提交共享给团队
4. 子目录级 CLAUDE.md ← src/、api/、tests/ 等子目录,按上下文加载
The most specific rules take priority: A subdirectory's CLAUDE.md overrides similar rules in the parent directory.
CLAUDE.md file in detail
What is CLAUDE.md?
CLAUDE.md is a Markdown file placed in the project root directory (or a subdirectory). At each new session startup, Claude Code willautomatically inject it into the system prompt. It is long-term memory that you can configure.
Creating CLAUDE.md
Method 1: Use the /init command to generate automatically
# Execute in Claude Code session
/init
Claude analyzes your directory structure and automatically generates a CLAUDE.md skeleton tailored to your tech stack. For example, running /initin a Node.js project, Claude automatically detects frameworks, test tools, build commands, etc., and generates an 80% complete initial file within 30 seconds.
Method 2: Create manually
touch CLAUDE.md
Recommended CLAUDE.md structure
# 项目约定
## 技术栈
- 前端:Next.js 15、TypeScript 5.7、Tailwind CSS 4
- 后端:Node.js 22、Prisma 6
- 测试:Vitest 3.2
## 代码规范
- 始终使用函数式 React 组件
- 文件名使用 kebab-case
- 测试文件与源码放在同一目录
## Common Commands
- 构建:`pnpm build`
- 测试:`pnpm test`
- 启动开发服务器:`pnpm dev`
## API 约定
- 所有 API 路由以 `/api/v1/` 开头
- 错误响应格式:`{ error: string, code: number }`
Golden rules for writing a good CLAUDE.md
✅ Do it like this:
- Use imperative sentences and short lists, not narrative paragraphs
- Include specific version numbers and commands
- Add code examples (a 5-line example is worth 50 words of explanation)
- Keep it within 200 lines(anything beyond that will not be loaded at session start)
❌ Avoid writing like this:
- Vague instructions like "follow best practices" or "write clean code"
- Too many generic rules (only include conventions unique to this project)
- Outdated information (review monthly)
Subdirectory CLAUDE.md
my-project/
├── CLAUDE.md # 全局项目规范
├── src/
│ └── CLAUDE.md # 仅在处理 src/ 文件时加载
├── api/
│ └── CLAUDE.md # API 特定约定
└── tests/
└── CLAUDE.md # 测试特定规则
Claude Code only loads a subdirectory's CLAUDE.md when working on files in that directory, saving tokens while providing more precise context.
Auto Memory in detail
How does it work?
Auto Memory allows Claude toaccumulate knowledge on its own across sessions, without you having to write anything manually. Claude automatically saves notes as it works, including:
- Build commands and debugging tips
- Architecture decision notes
- Code style preferences
- Workflow habits
Claude does not save content every time; it judges which information will beuseful in future sessionsbefore writing it down.
The file structure of Auto Memory
~/.claude/projects//memory/
├── MEMORY.md # 简洁的索引文件,每次会话开始时加载(前 200 行)
├── debugging.md # 调试模式的详细笔记
├── api-conventions.md # API 设计决策
└── ... # Claude 创建的其他主题文件
MEMORY.md is an index of the entire memory directory, which Claude uses to track what is stored in each file.
Triggering Auto Memory
When you tell Claude certain things, it automatically saves them to memory:
你:始终使用 pnpm,不要用 npm
你:记住 API 测试需要本地运行 Redis 实例
你:我们的日期格式统一用 ISO 8601
Want to save to CLAUDE.md instead of Auto Memory? State it explicitly:
你:把这条加到 CLAUDE.md
Enable / disable Auto Memory
Method 1: Toggle via the /memory command(see the next section)
Method 2: Configure in project settings
// .claude/settings.json
{
"autoMemoryEnabled": false
}
Method 3: Environment variable
export CLAUDE_CODE_DISABLE_AUTO_MEMORY=1
blockquote>
Note: Auto Memory isat the local machine level; all worktrees and subdirectories of the same Git repository share one memory directory, but itdoes not sync across machines or cloud environments。
/memory Command usage guide
/memory is the core command for managing the memory system.
/memory Functions of the command
Enter in a Claude Code session /memory, with which you can:
- CheckList all CLAUDE.md and rule files loaded in the current session
- switch The on/off status of Auto Memory
- Open The Auto Memory folder link
- Select any fileOpen and edit in the editor
# Shortcut - quickly add a memory
This is a hidden productivity gem:
# 始终在函数参数中使用具名参数(named parameters)
Press # key, type what you want to remember, and press Enter - Claude Code automatically writes it to the corresponding CLAUDE.md file. Great for:
- Recording project conventions
- Saving commonly used Bash commands
- Noting code style details
FAQ and troubleshooting
Claude ignores CLAUDE.md instructions?
- Run
/memoryConfirm the CLAUDE.md file has been loaded - Ensure Claude Code is running in the directory where CLAUDE.md resides (or a subdirectory)
- Check whether the instructions are specific enough - "follow best practices" is too vague, while "use named imports (tree-shaking compatibility)" is more effective
- Check whether the file exceeds 200 lines (anything beyond is not loaded)
CLAUDE.md is context, not enforced execution
Claude Readand tries its best to follow CLAUDE.md, but there is no guarantee of strict compliance, especially when instructions are vague or conflicting. Think of it as a "work guide for Claude" rather than an "unbreakable rule".
Real workflow examples
Scenario 1: Initializing a new project
# 1. 在项目根目录启动 Claude Code
cd my-project
claude
# 2. Generate memory file skeleton
/init
# 3. Review and refine the generated CLAUDE.md
/memory # 打开文件编辑
# 4. Start working, Claude will automatically accumulate memory
Scenario 2: Recording debugging findings
你:记住,运行集成测试前必须先启动 Docker Compose
Claude:好的,我已将这条记录到 Auto Memory 中。
Next session, Claude will automatically know about this dependency.
Scenario 3: Team collaboration
Commit the project root's CLAUDE.md to the Git repository - everyone's Claude assistant on the team will read the same standards, achieving a consistent AI-assisted experience.
git add CLAUDE.md
git commit -m "feat: add Claude Code memory configuration"
Summary of best practices
| Scenario | Recommended practices |
|---|---|
| Team-shared standards | Project root's CLAUDE.md, committed to Git |
| Personal preferences | ~/.claude/CLAUDE.md(user level) |
| Module-specific rules | Subdirectory CLAUDE.md |
| Let Claude teach itself | Enable Auto Memory and state preferences verbally |
| Temporary context | @docs/filename.md Reference as needed; don't cram it into CLAUDE.md |
| Task tracking | Use in Markdown files [ ] Checkbox |