s/agents-development

CLAUDE.md and Agent Rules

Ревизия {n}

Diff заголовка

Предыдущая ревизия
Выбранная ревизия
1
 
1
CLAUDE.md and Agent Rules

Diff содержимого

Предыдущая ревизия
Выбранная ревизия
1
## CLAUDE.md and Agent Rules
1
 
2
 
3
CLAUDE.md is the single most important file in an agent-coded project. It is the onboarding document the agent reads at the start of every session — the place you write down what you would otherwise re-explain. No CLAUDE.md means the agent guesses, and guesses wrong in exactly the places you would catch in code review.
4
 
5
This page covers: where CLAUDE.md lives, how Claude Code loads it, how to write instructions that actually get followed, and the path-scoped `.claude/rules/` system for larger projects.
6
 
7
## Where CLAUDE.md files live
8
 
9
Claude Code walks up the directory tree from the working directory and concatenates every CLAUDE.md and CLAUDE.local.md it finds. More specific locations take precedence.
10
 
11
| Scope | Location | Purpose | Shared via |
12
| --- | --- | --- | --- |
13
| Managed policy | `/Library/Application Support/ClaudeCode/CLAUDE.md` (macOS), `/etc/claude-code/CLAUDE.md` (Linux), `C:\Program Files\ClaudeCode\CLAUDE.md` (Windows) | Org-wide instructions enforced by IT | MDM / Group Policy |
14
| Project | `./CLAUDE.md` or `./.claude/CLAUDE.md` | Team-shared project rules | Git |
15
| User | `~/.claude/CLAUDE.md` | Personal preferences across projects | Nobody |
16
| Local | `./CLAUDE.local.md` | Personal project preferences | Nobody (gitignored) |
17
 
18
Within each directory, CLAUDE.local.md is appended after CLAUDE.md. When instructions conflict, your personal notes are the last thing Claude reads at that level.
19
 
20
Files in subdirectories under the working directory are not loaded at launch. They load on demand when Claude reads files in those subdirectories.
21
 
22
## When to add something to CLAUDE.md
23
 
24
Add an instruction when:
25
 
26
- Claude made the same mistake a second time.
27
- A code review caught something Claude should have known about this codebase.
28
- You typed the same correction into chat that you typed last session.
29
- A new teammate would need the same context to be productive.
30
 
31
Don't add: one-off task details, information already derivable from the code, or step-by-step procedures that only matter for one part of the repo. Those belong in a skill, a path-scoped rule, or the PR description.
32
 
33
## Write effective instructions
34
 
35
CLAUDE.md is loaded as a user message, not part of the system prompt. There is no guarantee of strict compliance — instructions that are vague, contradictory, or buried in prose get ignored. A few rules that consistently improve adherence:
36
 
37
**Be specific enough to verify.**
38
 
39
- Bad: "format code properly"
40
- Good: "use 2-space indentation"
41
- Bad: "test your changes"
42
- Good: "run `npm test` before committing"
43
 
44
**Structure with headers and bullets.** Claude scans the same way humans do. Dense paragraphs lose.
45
 
46
**Keep it under 200 lines.** Longer files consume more context and adherence drops. If your instructions are growing large, split them using imports or `.claude/rules/`.
47
 
48
**Don't let instructions contradict each other.** If two rules fight, Claude picks one arbitrarily. Review the full chain of loaded files periodically.
49
 
50
## Starting a CLAUDE.md
51
 
52
Run `/init` inside a project. Claude analyzes the codebase and generates a starting CLAUDE.md with build commands, test instructions, and conventions it discovers. Refine from there. Setting `CLAUDE_CODE_NEW_INIT=1` opens an interactive multi-phase flow that also proposes skills and hooks.
53
 
54
A reasonable CLAUDE.md starts with:
55
 
56
```markdown
57
# Project: acme-api
58
 
59
## Commands
60
- `make build` — compile the binary
61
- `make test` — run the full test suite
62
- `make lint` — run golangci-lint
63
 
64
## Architecture
65
- HTTP handlers live in `internal/http/`
66
- Business logic in `internal/services/`
67
- No logic in handlers except parsing and response shaping
68
 
69
## Conventions
70
- Table-driven tests with `t.Run` subtests
71
- Errors wrapped with `fmt.Errorf("...: %w", err)`
72
- No panics outside of `main.go` initialization
73
```
74
 
75
## Imports with @path
76
 
77
CLAUDE.md can pull in other files using `@path/to/file` syntax. Relative paths resolve from the file containing the import, not the working directory. Imports can nest up to 5 hops deep.
78
 
79
```markdown
80
See @README.md for project overview and @package.json for npm commands.
81
 
82
# Git workflow
83
@docs/git-instructions.md
84
 
85
# Personal preferences
86
@~/.claude/my-project-instructions.md
87
```
88
 
89
The first time Claude Code encounters external imports in a project, it shows an approval dialog. Declining disables imports for that project.
90
 
91
## Path-scoped rules with .claude/rules/
92
 
93
For larger projects, split CLAUDE.md into topic files under `.claude/rules/`. Each file covers one topic; all `.md` files are discovered recursively.
94
 
95
```
96
your-project/
97
├── CLAUDE.md
98
└── .claude/
99
    └── rules/
100
        ├── code-style.md
101
        ├── testing.md
102
        └── api-design.md
103
```
104
 
105
Rules without `paths` frontmatter load at launch unconditionally. Rules with `paths` only load when Claude reads a matching file:
106
 
107
```markdown
108
---
109
paths:
110
  - "src/api/**/*.ts"
111
  - "tests/api/**/*.test.ts"
112
---
113
 
114
# API Development Rules
115
 
116
- All API endpoints must include input validation
117
- Use the standard error response format
118
- Include OpenAPI documentation comments
119
```
120
 
121
This is how you keep context lean: the API team's 500-line conventions only load when the agent touches API files.
122
 
123
## AGENTS.md for multi-tool repos
124
 
125
If your repo already has AGENTS.md for Codex or Cursor, create a minimal CLAUDE.md that imports it:
126
 
127
```markdown
128
@AGENTS.md
129
 
130
## Claude Code
131
 
132
Use plan mode for changes under `src/billing/`.
133
```
134
 
135
Both tools read the same core instructions; Claude Code gets its own section below.
136
 
137
## Debugging "Claude ignored my rule"
138
 
139
- Run `/memory` to see which files are loaded this session. If yours is not listed, Claude never saw it.
140
- Check for conflicting instructions across CLAUDE.md files further up the tree.
141
- Make the instruction more concrete — "use 2-space indentation" beats "format nicely".
142
- Use the `InstructionsLoaded` hook to log exactly which files load and when.
143
- If the instruction must hit the system prompt level, use `--append-system-prompt` — but that must be passed on every invocation.
144
 
145
---
146
 
147
Next: [Hooks and Policy-as-Code](/s/agents-development/wiki/hooks-policy-as-code)
148
 
149
## Sources
150
 
151
- [Claude Code memory docs](https://code.claude.com/docs/en/memory)
152
- [Claude Code settings](https://code.claude.com/docs/en/settings)
153
- [Extension overview](https://code.claude.com/docs/en/features-overview)
© 2026 HeyUpСделано на Laravel, Vue и Tailwind.