Hierarchical Rule Files with Collocation: Context at the Point of Need

James Phoenix
James Phoenix

Summary

Global rules can’t capture domain-specific constraints, forcing developers to mentally context-switch and LLMs to hallucinate patterns. The hierarchical rule files pattern collocates documentation and configuration files (CLAUDE.md, ESLint configs, learning files) as close as possible to the code they govern. This creates semantic locality where context lives at the point of need, reducing cognitive load and improving LLM code generation accuracy.

The Problem

Monolithic root-level rule files (CLAUDE.md, .eslintrc) don’t scale to large codebases with multiple domains. Global rules miss domain-specific constraints (API patterns vs database migrations vs UI components), forcing developers to remember context and LLMs to guess patterns. When context is far from code, it becomes stale, unused, and eventually wrong. Teams waste time searching for relevant documentation across scattered files.

The Solution

Apply the collocation principle: ‘Rules should live as close as possible to the code they govern.’ Create a hierarchy of rule files that mirror your codebase structure. Root files contain architecture and global principles. Domain directories contain domain-specific patterns. Module directories contain implementation details. This creates semantic locality where LLMs and developers always have relevant context without searching.

Leanpub Book

Read The Meta-Engineer

A practical book on building autonomous AI systems with Claude Code, context engineering, verification loops, and production harnesses.

Continuously updated
Claude Code + agentic systems
View Book

Related Concepts

References

Topics
Claude CodeContext ManagementDocumentationDomain Driven DesignLintingMemory SystemsProgressive DisclosureSoftware Architecture

Newsletter

Become a better AI engineer

Weekly deep dives on production AI systems, context engineering, and the patterns that compound. No fluff, no tutorials. Just what works.

Join 306K+ developers. No spam. Unsubscribe anytime.


More Insights

Cover Image for The Environment Leads The Agent

The Environment Leads The Agent

For a long time I tried to lead my coding agents with better and better prompts, and they kept drifting. What finally worked was the opposite move. As I optimised the boilerplate of the repository I was building, I kept pushing each lesson I learned down into the floor of the repo: hermetic environments, typed contracts, mechanical lint, integration-first tests, queryable telemetry. Somewhere along the way the repo itself became the thing steering the agent, and it asks me what to do far less than it used to. This is the journey that got me there, and what it taught me.

James Phoenix
James Phoenix
Cover Image for Your Own Life Is a Queryable, Validated Corpus

Your Own Life Is a Queryable, Validated Corpus

Your private data exhaust deserves the same treatment as production data: indexed, validated, version-controlled, and queried by an agent. Once you make that move, writing a song, paying a tax bill, and updating a CV all become the same engineering problem.

James Phoenix
James Phoenix