Build This Now
Build This Now
キーボードショートカットステータスラインガイド
CLAUDE.md, Skills, Subagents, Hooks: When to Use WhichClaude Code Subagents: The 3 to 5 Agent Sweet SpotCLAUDE.md Best Practices: The File That Makes Claude Code ReliableHow to Fix Claude Code Running Out of Context
speedy_devvkoen_salo
Blog/Toolkit/CLAUDE.md Best Practices: The File That Makes Claude Code Reliable

CLAUDE.md Best Practices: The File That Makes Claude Code Reliable

CLAUDE.md best practices: keep it under 200 lines, write it by hand, and use hooks when you need real enforcement, not advice.

設定をやめて、構築を始めよう。

AIオーケストレーション付きSaaSビルダーテンプレート。

Published Jun 23, 20267 min readToolkit hub

The best CLAUDE.md is short, written by a human, and covers only what Claude Code cannot already learn by reading your code. A lean file under 200 lines produces more reliable behavior than the long auto-generated files most teams commit. But CLAUDE.md is advice, not a rule Claude must obey, so for behavior that can never break you need a hook, not a longer file.


設定をやめて、構築を始めよう。

AIオーケストレーション付きSaaSビルダーテンプレート。


What CLAUDE.md actually is

CLAUDE.md is a plain text file Claude Code reads at the start of a session. Think of it as a sticky note you hand to a new contractor before they touch your project. It tells them how this specific project works.

Here is the part most people miss. CLAUDE.md is injected as a user message, the same kind of message you type. It is not a system directive that Claude is forced to follow. Claude tries hard to honor it, but compliance slips as the chat gets longer and the file gets bigger. One reported pattern is instruction compliance starting above 95% at the top of a session and falling to between 20% and 60% by the sixth to tenth message.

So treat CLAUDE.md as strong guidance for preferences, and reach for a hook when something must happen every single time.

Why this matters to you

If your Claude Code keeps ignoring your rules, the file itself is often the problem. A bloated CLAUDE.md does not make Claude more obedient. It makes things worse. A February 2026 ETH Zurich study (300 tasks from SWE-bench Lite, a standard coding benchmark) reported that auto-generated /init output, committed straight to the repo, cut task success rates by about 3% and raised inference costs by over 20%. Human-written files improved performance by about 4%. The lesson is direct: run /init to get a draft, then rewrite it before you commit. Never ship the raw output.

What to put in CLAUDE.md

Put in only what Claude cannot infer from the code in front of it:

  • Build and test commands. The exact line to run your tests, your linter, your dev server.
  • Conventions that break common defaults. If your team does something unusual, say so. Claude assumes the common pattern otherwise.
  • Hard constraints. For example, "never use the service role key in client code" if you run row-level security on every table.
  • Where the important stuff lives. Entry points, the main config file, the folder that holds business logic.

Leave out the tech stack, generic best practices, and anything already obvious from the files. Claude can read package.json. It does not need you to retype it.

The four scopes (and the table that ends the confusion)

Most developers cram everything into one giant root CLAUDE.md. There are actually four separate scopes plus two related systems, and each one is the right tool for a different job. The table below lets you pick at a glance instead of defaulting to CLAUDE.md for everything.

ToolWhen it loadsWho writes itEnforcementToken costBest for
CLAUDE.md (project)Every sessionYou (committed to git)AdvisoryAlways loadedStable team conventions, build commands
.claude/rules/ (path-scoped)Only when Claude touches matching filesYouAdvisoryLazy, near zero until triggeredRules tied to one folder or file type
Hooks (PreToolUse / PostToolUse)On every matching tool callYou (shell scripts)DeterministicNone (runs outside the model)Things that must never happen
CLAUDE.local.md (personal)Every sessionYou (gitignored)AdvisoryAlways loadedYour own overrides, not shared
Auto memory (MEMORY.md)First 200 lines each sessionClaude itselfAdvisoryFirst 200 linesCross-session discoveries Claude makes

The full list of scopes, from widest to narrowest:

  1. Managed policy sits at the organization level (a macOS system path) and applies to everyone in a company.
  2. User level lives at ~/.claude/CLAUDE.md and follows you across every project on your machine.
  3. Project level is ./CLAUDE.md, committed to the repo so the whole team shares it.
  4. Personal project level is ./CLAUDE.local.md, added to .gitignore so it stays on your machine only.

Keep it lean: the lost-in-the-middle problem

Long context hurts. Language models show a reported accuracy drop of more than 30% on instructions buried in the middle of a long context, a pattern researchers call "lost in the middle." Your most important rules should sit at the top and the bottom of the file, not in the middle of a wall of text.

Practical limits:

  • Aim for under 200 lines, the official recommendation. High-signal teams keep root files under 60 lines.
  • Past roughly 300 lines, compliance drops and costs climb with no real gain in accuracy.
  • Use HTML block comments (<!-- like this -->) for notes to your teammates. They are stripped before the file reaches Claude, so they cost zero tokens.

To keep the root file small without losing detail, move folder-specific rules into .claude/rules/. Add a paths: glob in the file's YAML header (a glob is a pattern like src/api/**). That rule then loads only when Claude touches a matching file. A rule about your API folder never wastes token budget on a CSS task.

Imports, AGENTS.md, and auto memory

You can pull other files into CLAUDE.md with an @path/to/file import. This is good for organizing a modular instruction set. Be honest about what it does though. Imports expand recursively up to four levels deep and still load at launch. They cut visual clutter in the root file. They do not cut total token cost.

If your monorepo uses several AI tools, the AGENTS.md bridge pattern keeps them consistent. Put shared instructions in AGENTS.md, import it with @AGENTS.md, then add Claude-specific notes below.

Auto memory is a separate system (Claude Code version 2.1.59 and later). Claude writes its own notes across sessions to a MEMORY.md file, and the first 200 lines load each session. Let Claude maintain its own memory for things it discovers about your project. Keep CLAUDE.md for stable conventions you curate by hand. Mixing both jobs into one file is the most common cause of bloat.

If you want a faster start, the Build This Now $29 Code Kit ships a ready-made harness for Claude Code with CLAUDE.md, scoped rules, hooks, and a production SaaS skeleton (auth, Stripe payments, and row-level security on every table) already wired together, so you are not assembling this setup from scratch.

FAQ

What should I put in my CLAUDE.md file?

Put only what Claude cannot infer from code: build and test commands, project conventions that break common defaults, hard constraints (for example, never use the service role key client-side), and where the key entry points live. Leave out the tech stack and anything already documented in the codebase.

How long should a CLAUDE.md file be?

Under 200 lines, per the official recommendation. Strong teams keep root files under 60 lines and push the rest to .claude/rules/. Past 300 lines, compliance drops and token costs rise with no matching gain in accuracy.

Does CLAUDE.md actually work?

Partially. CLAUDE.md is advisory context, not enforced configuration, and reported instruction compliance falls from above 95% at the start of a session to between 20% and 60% by message six to ten. The February 2026 ETH Zurich study found auto-generated files hurt performance more than having no file at all. Human-written, minimal files do help. For guarantees, use hooks.

What is the difference between CLAUDE.md and hooks in Claude Code?

CLAUDE.md tells Claude what to prefer. Hooks enforce what Claude must do or cannot do. A CLAUDE.md line saying "never run destructive migrations" can be overridden in a long session. A PreToolUse hook that exits with code 2 blocks the Bash tool call every time, no matter what Claude decides. Pair the two: CLAUDE.md for guidance, hooks and Claude Code subagents for the hard limits.

More in Toolkit

  • CLAUDE.md, Skills, Subagents, Hooks: When to Use Which
    Claude Code skills vs subagents vs hooks vs CLAUDE.md: a plain mental model for picking the right primitive, with token costs and examples.
  • Claude Code Subagents: The 3 to 5 Agent Sweet Spot
    Claude code subagents work best at 3-5 concurrent agents. Here is why that ceiling exists, how to set them up, and what to use past it.
  • How to Fix Claude Code Running Out of Context
    Claude Code running out of context is a session design problem. Fix it with /compact, lean CLAUDE.md, skills, and subagents, not a bigger window.
  • キーボードショートカット
    Claude Codeのkeybindings.jsonを設定する: 17のコンテキスト、キーストローク構文、コードシーケンス、修飾キーの組み合わせ、デフォルトショートカットを即座に無効化する方法。
  • ステータスラインガイド
    Claude Code のステータスラインにモデル名、gitブランチ、セッションコスト、コンテキスト使用量を表示する方法。settings.json の設定、JSON入力、bash、Python、Node.js スクリプトを解説。
  • AIによるSEOとGEO最適化
    Generative Engine Optimizationの解説: Googleで上位表示されるだけでなく、ChatGPT、Claude、Perplexityの回答内でコンテンツが引用されるようにする方法。

設定をやめて、構築を始めよう。

AIオーケストレーション付きSaaSビルダーテンプレート。

Claude Code Subagents: The 3 to 5 Agent Sweet Spot

Claude code subagents work best at 3-5 concurrent agents. Here is why that ceiling exists, how to set them up, and what to use past it.

How to Fix Claude Code Running Out of Context

Claude Code running out of context is a session design problem. Fix it with /compact, lean CLAUDE.md, skills, and subagents, not a bigger window.

On this page

What CLAUDE.md actually is
Why this matters to you
What to put in CLAUDE.md
The four scopes (and the table that ends the confusion)
Keep it lean: the lost-in-the-middle problem
Imports, AGENTS.md, and auto memory
FAQ
What should I put in my CLAUDE.md file?
How long should a CLAUDE.md file be?
Does CLAUDE.md actually work?
What is the difference between CLAUDE.md and hooks in Claude Code?

設定をやめて、構築を始めよう。

AIオーケストレーション付きSaaSビルダーテンプレート。