What Is CLAUDE.md? How to Write One for Claude Code (With Template)
CLAUDE.md is a Markdown file that lives in your project. When Claude Code starts a session, it reads the file into context, so it works as a standing set of project instructions. You don't have to keep repeating "use pnpm, not npm" or "here's the test command." Write it down once.
If you're new to Claude Code, remember one thing about what to put in CLAUDE.md: only write what Claude can't guess from the code, and keep it as short as possible. Below we cover where the file goes, how to generate it, what to write, and how to check it. Exact behavior is defined by Anthropic's official documentation, and details may change between versions.
Where does CLAUDE.md go, and which files get loaded?
There are three common locations:
| Location | Scope | Good for |
|---|---|---|
CLAUDE.md in the project root | The current project; can be committed to Git and shared with your team | Build commands, code conventions, directory notes |
~/.claude/CLAUDE.md | All of your own projects | Personal preferences, such as response language or commit message style |
CLAUDE.md in a subdirectory | That subdirectory | Conventions specific to one package or module |
The general rule: starting from the current working directory, Claude looks upward through parent directories for CLAUDE.md files and reads them in. Files in subdirectories are usually loaded only when Claude reads files in that directory. You can also reference other files from inside the file with the @path syntax, which lets you move long documents out. Whether local private variants are supported, and any limits on reference depth, are covered in the memory section of the official docs and may differ between versions.
It isn't the same thing as settings.json. CLAUDE.md is natural-language guidance written for the model. settings.json controls configuration such as permissions, environment variables, and the model. See Claude Code settings.json: hierarchy, precedence, and env configuration.
How do you generate a first draft with /init?
Start Claude Code in your project directory and type:
/init
Claude scans the project and generates a draft CLAUDE.md, usually including build and test commands, the directory structure, and code style. Treat it as a starting point, not a finished file. After it's generated, I'd suggest you:
- Read it through and delete any sentence that is obviously filler.
- Check that the commands actually run, especially the test command.
- Add conventions that can't be seen in the code, such as "write database migrations by hand; don't auto-generate them."
For the full list of slash commands, see Claude Code commands cheat sheet. If you haven't installed Claude Code yet or haven't done a first run, read How to use Claude Code: from installation to your first code change first. During a session you can also use a command such as /memory to open and edit the memory files; check /help in your current version for the exact command name.
What should you write, and what should you leave out?
Worth writing:
- Common commands: the exact invocations for install, build, running a single test, and lint.
- Rules that aren't obvious from the code: naming conventions, directories that must not be modified, checks that must pass.
- Project-specific pitfalls, for example "you need to start a certain service locally before the tests pass."
- Workflow preferences: what to do before committing, how to name branches.
Not worth writing:
- A copy-paste of the whole README. Claude can read the README itself.
- Anything obvious from the code, such as "this is a React project."
- Long background documents. They take up context in every session, the longer they are the easier they are to ignore, and they cost more tokens.
- Secrets, tokens, or internal addresses. CLAUDE.md is usually committed to the repository, so don't put sensitive information in it.
A template you can edit directly
# 项目说明
## 常用命令
- 安装依赖:pnpm install
- 开发:pnpm dev
- 跑单个测试:pnpm test path/to/file.test.ts
- 提交前:pnpm lint && pnpm typecheck
## 代码约定
- TypeScript 严格模式,不使用 any。
- 新增接口放在 src/api/,不要直接改 src/generated/。
## 注意事项
- 数据库迁移手写,放在 migrations/,不要用自动生成。
- 回答和提交信息使用中文。
Note: the template above is written in Chinese as an example project. Translate it into your own language, replace the commands with your project's real ones, and delete any entries that don't apply. For reference, the English equivalent is:
# Project notes
## Common commands
- Install dependencies: pnpm install
- Dev: pnpm dev
- Run a single test: pnpm test path/to/file.test.ts
- Before committing: pnpm lint && pnpm typecheck
## Code conventions
- TypeScript strict mode; do not use any.
- Put new endpoints in src/api/; do not edit src/generated/ directly.
## Notes
- Write database migrations by hand in migrations/; do not auto-generate them.
- Use English for replies and commit messages.
How do you check that it works?
Use this checklist:
- Start a new session and ask Claude "how do I run the tests in this project?" Check whether it answers with the command you wrote.
- Deliberately ask for a small change that violates a convention, and see whether it follows the rule.
- For each rule, ask: if I deleted this, would Claude get it wrong? If not, delete it.
- If Claude keeps making the same mistake, add a matching rule and make it specific ("don't edit src/generated/" works better than "be careful not to mess with files").
- If the file grows past a hundred or two hundred lines, consider splitting it into several files referenced with
@, or pushing parts down into subdirectories.
Can you use CLAUDE.md without an official Claude account?
Yes. CLAUDE.md is just a file in your project. It has nothing to do with how you log in or which API endpoint you use. If you want to know how to use Claude Code without logging in to a Claude account, see Can you use Claude Code without a Claude account? Using a custom endpoint. For the configuration itself, see Configuring custom API endpoints for Claude Code, Codex, and OpenCode.
One side note: AGI Plan is a commercial partner of cdnping. It is a multi-provider LLM aggregation gateway with monthly subscriptions, payment in RMB, and no need for an overseas credit card. For tools like Claude Code and Codex you only change one endpoint URL, and you can use different capability groups under the same quota. It is not an officially authorized channel, and some models are marked as available only outside mainland China; see https://agiplan.dev/models for the current list. If you want to try it, start at https://agiplan.dev/startup. Whether it suits you depends on your usage and use case. You don't need to switch services just to write a CLAUDE.md.
If Claude Code is slow or won't connect, check /en/down/claude-code and /en/status/anthropic first to see whether the problem is on the server side, then troubleshoot your local configuration.
Next steps
You can do three things today: run /init in your project, trim the result using the checklist above, and verify it once in a fresh session. After that, whenever Claude repeats a mistake, add one specific rule to the file.