Have you ever experienced a situation with Claude Code where the rules you defined in CLAUDE.md are followed initially, but start to be ignored toward the end of a long session?
I was particularly struggling with projects that had many strict rules in CLAUDE.md. With 10 to 20 clauses like "do not touch this directory" or "use only these specific APIs," having to roll back and review every time Claude violated a rule was truly exhausting.
The cause is context compression. As the conversation grows longer, CLAUDE.md gets pushed out of the context window, leaving the rules simply invisible to the model. I wanted to stop Claude the moment it attempts a violation, rather than noticing it later and rolling back. For my use case, it is better to have enforcement, even if it is a bit strict. With that in mind, I created claudemd-guard and published it to npm.
How it works
It operates as a PreToolUse hook for Claude Code and performs the following before executing tools like Edit, Write, or Bash:
- Collects CLAUDE.md from the project tree.
- Sends the collected rules and the intended operation to an AI validator.
- Blocks the operation if there is a clear violation, and lets it pass if not.
It runs via your local Claude CLI by default, so no additional API keys are required. It follows a fail-open design, meaning that if an error occurs during validation, the operation is allowed to proceed. I determined that having work stopped because the guard itself broke is a poor failure mode for a convenience tool.
Installation
npm install -g claudemd-guard
claudemd-guard install
Restart Claude Code to enable it. You can uninstall it with a single command: claudemd-guard uninstall.
I hope this reaches those who want to ensure CLAUDE.md is followed and are tired of rolling back violations.
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.