12 Rules for an Effective claude.md File
The claude.md file (called agents.md in some other systems) is prepended to every prompt you send to your coding agent. It's a persistent constitution for how the agent should behave, what conventions it should follow, and what mistakes it should never repeat. A well-written one transforms an agent that constantly needs correction into one that operates reliably over long sessions with minimal oversight. A poorly written one wastes context on noise and still leaves the agent guessing.
These 12 rules cover the patterns that matter most: auto-improvement, testing discipline, safe dependencies, naming consistency, architecture orientation, and performance. They're written with Claude Code in mind but apply to any agent that reads a context file at session start.
Keep it under 500 lines
Before the specific rules, one overriding constraint. The claude.md is loaded into every session. LLM performance degrades as context grows, so a longer file doesn't mean better guidance — it means less reliable adherence to all of it.
Keep the file under 500 lines. If you have more than that, modularize: nested claude.md files in subdirectories (api/claude.md, web/claude.md) let you scope rules to specific parts of the project. Complex reusable workflows go in .claude/skills/ and get linked from the main file rather than inlined.
Rule 1: Auto self-improvement
Treat the claude.md as a failure log, not a wishlist. Every rule should exist because the agent made a specific mistake at least once. When that mistake gets corrected, it gets codified.
This framing, popularized by Mitchell Hashimoto (creator of Terraform), creates a feedback loop where the agent gets more reliable with every correction rather than repeating the same mistakes across sessions.
Rule 2: A testing and linting loop
Define a strict loop the agent must complete before considering any task done. Without this, agents declare success based on whether the code looks plausible rather than whether it actually passes.
Rule 4: Strict type-checking
AI agents produce significantly fewer bugs when working in a strictly typed codebase. Type errors become a first-class signal rather than noise.
Rule 5: Well-maintained libraries only
Agents will install packages to solve problems that already exist in the standard library, or install packages from single maintainers with no recent activity. Both are risks: unnecessary maintenance burden in the first case, supply chain exposure in the second.
Rule 6: Naming conventions
Without a canonical vocabulary, agents will mix "login" and "sign in," or use deleteUser in one place and removeProduct in another. This makes the codebase harder to reason about for both humans and the agent itself.
Rule 7: Project structure
Without a map, agents infer structure by scanning the file tree, which wastes tokens and can still produce incorrect results. A simple structure guide is cheap to write and saves work every session.
Rule 8: End-to-end testing
Unit and integration tests catch isolated failures. End-to-end testing is what catches the interactions between pieces. Instruct the agent to drive the application like a human after any feature that spans both sides of the stack.
Rule 9: UI testing
A green test suite says nothing about whether the screen looks right. A button could be covered by another element or text could be truncated by a long name. Use the agent's multimodal capabilities to inspect screenshots.
Rule 10: Performance
Agents commonly fetch large datasets and filter them in application code rather than in SQL. Catch this before it reaches production by defining explicit performance budgets and database rules.
Rule 11: Error handling
Silent failures are the hardest bugs to track down. Define a strict policy that prevents errors from being swallowed and establishes a consistent response envelope.
Rule 12: Architecture orientation
An agent that needs to re-discover your project's architecture at the start of every session spends tokens on exploration that a short reference section would eliminate. Give it a map.
Putting it together
These rules work best as a starting skeleton rather than a final document. Start with the failure log section and the testing loop; add naming conventions and architecture orientation as soon as the codebase has enough shape to define them. Let the failure log grow from real mistakes rather than trying to anticipate every edge case upfront.
The goal is a file that's short enough to be reliable as context, specific enough to eliminate guessing, and alive enough to improve with every session that finds a new failure worth codifying.