Project Instructions and Context
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
A single CLAUDE.md only goes so far. As a project grows you need instructions that apply to part of the code, a way to hand Claude a written spec, and some honesty about which instructions Claude merely reads and which ones the tool actually enforces. This lesson covers all three: rules scoped to file paths, giving Claude context on a task, and deciding where each kind of instruction belongs.
What you'll learn
- How to scope instructions to part of a codebase with
.claude/rules/.
- How to give Claude context for a task: files, specs and good prompts.
- The difference between instructions Claude reads and rules Claude Code enforces.
- How to debug "Claude isn't following my instructions".
Before you start
Rules that load only when they matter
Putting every convention in one file means the API conventions sit in context while Claude edits a CSS file. The .claude/rules/ folder fixes that. Each markdown file there covers one topic (testing.md, api-design.md), files are found recursively, and you can add a paths list to scope a rule to matching files. Rules without paths load at launch with the same priority as .claude/CLAUDE.md. Rules with it load when Claude reads, writes or edits a matching file.
The example project has two. First, API rules, scoped to the controllers and API routes:
---
paths:
- "app/Http/**/*.php"
- "routes/api.php"
---
# API rules
- Every endpoint validates input with a Form Request.
- Return API resources, never raw models.
- Authorise in a policy, not inside the controller body.
- A 404 is returned for another user's record, never a 403 (do not confirm that it exists).
And test rules, scoped to the tests folder:
---
paths:
- "tests/**/*.php"
---
# Test rules
- Use Pest, not PHPUnit classes.
- One behaviour per test, named as a sentence: `it('rejects an expired coupon')`.
- Never call the real Claude API or a carrier API. Fake the HTTP layer.
Some details from the documentation that save debugging time:
paths accepts a YAML list or a comma-separated string, and patterns are relative to the folder containing .claude. **/*.ts matches TypeScript anywhere; src/components/*.tsx only matches directly inside that folder; brace expansion like src/**/*.{ts,tsx} works.
paths is the only frontmatter field a rule reads. Any other field is ignored without an error. If the YAML doesn't parse, Claude Code ignores the frontmatter and loads the rule as if it had no paths, so it loads everywhere. claude --debug shows the parse error.
- A rule loads into the conversation when its trigger file is touched, so compaction can summarise it away. If a rule must persist, drop the
paths or move it to the root CLAUDE.md.
- Per-file size limits apply to files that load on demand (40,000 characters on a 200K context window; 150,000 to 200,000 on a 1M window). Past the limit, Claude gets the start of the file and a note to read the rest itself.
- You can share a set of rules across projects with symlinks.
The linter from lesson 5.11 checks exactly these: a rule with a misspelled field (path: instead of paths:), unreadable YAML, or an unclosed frontmatter block. We tested all three cases, and that the two example rules pass.
When to use which mechanism, in one table:
| Instruction is... | Put it in |
| A fact Claude needs in every session (commands, gotchas) | Root CLAUDE.md |
| Relevant only to some files | A path-scoped rule in .claude/rules/ |
| A multi-step procedure used sometimes | A skill (lesson 5.6) |
| Something that must happen or must never happen | A hook or a deny rule (lesson 5.7) |
| Personal, for this project only | CLAUDE.local.md |
Instructions you ask for versus rules you enforce
Anthropic's best-practices page draws a line that's worth memorising. Every instruction is one of two kinds:
- Non-deterministic: you write it and Claude reads it. That's your prompt,
CLAUDE.md and skills. The model decides what to do with it. It usually follows them, but there's no guarantee.
- Deterministic: you configure it and Claude Code, the harness, must execute it. That's hooks, deny rules and managed settings. They apply every time, whatever the model decided.
Most of the time you want Claude to decide, because current models read the code in front of them and judge whether a guideline fits. The deterministic kind is for the rules with no exceptions. Anthropic's rule of thumb: if you keep adding emphasis to a CLAUDE.md line and Claude still misses it, the line belongs in a hook. The reverse also holds: if you have hooks for things that are only preferences, delete them.
Giving Claude context for a task
Instructions describe your project in general. Context for a particular task is what you put in the prompt:
- Be specific. Name the file, describe the symptom, say what "fixed" looks like. If you care which of two reasonable approaches it uses, say so; it can't read that from the code.
- Hand it the thing.
@path/to/file includes the file, a pasted screenshot shows an error or a design, and cat error.log | claude -p "what's going on here" pipes output in.
- Say what scope you want. Claude tends to read a request conservatively and build the smallest thing that satisfies it. Anthropic notes that "be ambitious" still helps, as a statement about scope: "build the settings page from the mockup. be ambitious: empty and error states, the save flow wired up, keyboard navigation, not just the layout".
- Don't rely on magic words. Phrases like "think step by step" are ordinary prompt text. Thinking depth is controlled by the effort level.
For work you haven't fully thought through, let Claude interview you and write a spec, then start a fresh session to build from it:
I want to add rate limiting to the public API but haven't thought it through yet. Interview me until you could write the spec, then write it to SPEC.md.
The advantage is twofold: the build starts with a clean context and a written definition of done, and SPEC.md is the file you can point a reviewer at later.
Instructions at other levels
- The system prompt. Claude Code's own system prompt isn't published. To add standing instructions at the system-prompt level, use
--append-system-prompt at launch, which suits scripts and automation better than interactive use.
- Compaction guidance. A "Compact Instructions" section in
CLAUDE.md, or /compact focus on the API changes, controls what a summary keeps (lesson 5.9).
- Hook-injected context. A
UserPromptSubmit or SessionStart hook can add text such as the current branch or a freeze notice. We cover hooks in lesson 5.7.
- Git behaviour. If your
CLAUDE.md sets commit or pull-request rules that compete with Claude Code's built-in ones, turn those off with includeGitInstructions and set attribution text with attribution.
When Claude isn't following your instructions
Work down this list, which follows Anthropic's troubleshooting steps:
/context all and check "Memory files". If the file isn't listed, Claude can't see it.
- For a nested
CLAUDE.md, remember it loads only after Claude touches a file in that folder. To test, create it from your shell, then ask Claude to read a file there.
- Is the file in a location that loads for your session?
- Make the line more specific.
- Look for contradictions across
CLAUDE.md, nested files and rules.
- Check for a competing built-in behaviour (commit and PR text is the usual one).
- If the rule can't have exceptions, make it a hook.
The InstructionsLoaded hook can log which files load, when and why, which is handy for debugging path-scoped rules.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
A team's CLAUDE.md has grown to include API conventions, test style, frontend naming and database advice. Claude edits a test file and uses PHPUnit classes instead of Pest. The conventions are in there, but buried. The team splits the file: five lines of universal commands and gotchas stay in the root file, API and test conventions move into .claude/rules/api.md and tests.md with paths:, and the migration advice moves into a nested CLAUDE.md in the migrations folder. The root file shrinks to a few dozen lines, and the test rule appears in context at the moment Claude opens a test file.
Mistakes to avoid
- Writing
path: instead of paths: (ignored silently) or an unquoted glob that breaks YAML.
- Scoping a rule so tightly that it never loads.
- Duplicating the same instruction in
CLAUDE.md and a rule, then editing only one.
- Using rules for multi-step procedures. That's what skills are for.
- Relying on any of these files for security. They shape behaviour, they don't restrict it.
Security notes
Rule files are instructions committed to the repository, so changes to them deserve review like code changes. Never put credentials or customer data in them. For anything that must be prevented, such as reading .env or running destructive commands, use deny rules and hooks, which Claude Code enforces regardless of what the model decides.
Cost and speed
Rules without paths cost context on every session, and path-scoped ones only when triggered. A short root file with scoped rules is the cheapest way to keep detailed conventions available without paying for them constantly.
Key takeaways
.claude/rules/*.md splits instructions by topic; paths frontmatter loads them only for matching files.
paths is the only field a rule reads; typos are ignored silently, so lint them.
- Prompts,
CLAUDE.md and skills are requests; hooks, deny rules and managed settings are enforced.
- Give task context explicitly: specific prompts,
@ files, images, piped output, or an interview that produces a spec.
Lesson recap: asked versus enforced, and where each instruction lives.
Quick quiz
- What does a rule's
paths frontmatter do?
- Name the only frontmatter field a rule file reads.
- Which kind of instruction is guaranteed to apply: a
CLAUDE.md line or a hook?
- What should you do when you keep adding emphasis to a line Claude misses?
- Why start a fresh session after writing
SPEC.md?
Answers and explanations
- It scopes the rule to files matching the glob patterns, so it loads only when Claude works with a matching file.
paths. Any other field is ignored without an error.
- A hook.
CLAUDE.md is context that Claude reads; hooks are executed by Claude Code every time.
- Move it to a hook or deny rule, or shorten the file. Emphasis mostly makes Claude over-apply a rule.
- The build then starts with a clean context and a written definition of done, and the spec file can be used for review.
Official references and further reading
Related tutorials