Stop Wasting Tokens in Your CLAUDE.md: The Layered Configuration System

Stop Wasting Tokens in Your CLAUDE.md: The Layered Configuration System

Separate global, project, and file-type rules into different CLAUDE.md files to cut token waste and make Claude Code more effective.

14h ago·3 min read·1 views·via hn_claude_code
Share:

The Technique — Layered Configuration

The most effective Claude Code users don't use a single, bloated CLAUDE.md. They use a layered system: a minimal global file, project-specific instructions, and file-type rules that only load when needed. This structure, detailed in the source from a daily user, prevents token waste and ensures the right context is active for the right task.

Why It Works — Token Efficiency & Precision

Every line in your global CLAUDE.md is loaded into every single interaction, consuming precious context tokens. The source's key insight: your global file should only contain universal preferences and nudges to correct agent behaviors. General principles like YAGNI or KISS are already in the system prompt—adding them is redundant. The real power comes from splitting context into layers that load on-demand, keeping the active context window lean and relevant.

How To Apply It — The Three Layers

1. The Global ~/.claude/CLAUDE.md

Keep this file minimal. Before adding anything, ask Claude: "Is this already covered in your system prompt?" The source recommends it should primarily contain high-level directives. Here's a critical snippet from their config:

yellow and red baseball on white table

<prefer_online_sources>
Use the find-docs skill or WebSearch to verify before relying on pre-trained knowledge. Look things up when:
- Writing code that uses libraries, APIs, or CLI tools
- Configuring tools, services, or environment variables
- Checking if a stdlib replacement exists for a third-party package
- Pinning dependency versions — always check the latest
</prefer_online_sources>

<auto_commit if="you have completed the user's requested change">
Use the commit skill to commit. Don't batch unrelated changes into one commit.
</auto_commit>

2. The Project ./CLAUDE.md

This is for project-specific instructions. The source emphasizes that the highest-signal content is the 'Gotchas' section. Build this from the failure points Claude actually encounters in your project. For example:

  • Database connection strings must use the local tunnel URL.
  • The build script requires NODE_ENV=production.
  • API calls need the staging header X-Env: test.

3. Per-File Type Rules in ~/.claude/rules/

This is the most powerful optimization. Place language-specific rules in ~/.claude/rules/ (e.g., python.md, javascript.md). Claude Code only loads these rules when editing files matching the path pattern. Here's the source's example for ~/.claude/rules/python.md:

---
paths:
- "**/*.py"
---
# Python
- Before adding a dependency, search PyPI or the web for the latest version
- Pin exact dependency versions in pyproject.toml — no >=, ~=, or ^ specifiers
- Target Python >=3.13 by default
- Use uv for project and environment management
- Use ruff for linting and formatting
- Use pytest for testing
- Use pathlib.Path over os.path

Putting It All Together

Your configuration should work like this:

  1. Global Layer (~/.claude/CLAUDE.md): 5-10 lines of universal behavior nudges.
  2. Project Layer (./CLAUDE.md): A concise list of project-specific gotchas and requirements.
  3. Rule Layer (~/.claude/rules/): A directory of markdown files with tech stack specifics that activate automatically.

Claude Code Statusline with English Grammar Check example

This structure ensures Claude has the precise context it needs without drowning in irrelevant instructions, making it faster and more accurate for every task.

AI Analysis

Claude Code users should immediately audit their global `CLAUDE.md`. Move any project-specific or language-specific rules out of it. Create a `~/.claude/rules/` directory and start by adding a `python.md` or `javascript.md` file with your stack's conventions. This instantly frees up context tokens for more relevant code and task instructions. For new projects, begin by creating a `./CLAUDE.md` focused solely on 'Gotchas'—the unique, easy-to-miss details of your codebase. This is more valuable than generic coding standards. Use the global file only for cross-cutting concerns like the `prefer_online_sources` directive, which is universally useful. The layered approach is a direct response to the context window economy. By loading rules on-demand, you keep the agent's 'working memory' clean. This leads to fewer hallucinations about your project's specifics and faster, more accurate code generation.
Original sourcevinta.ws

Trending Now

More in Products & Launches

Browse more AI articles