Alexandr.Studio

Force Claude Code to Obey: Turn Rules into Hooks

Alexander Sedeke | 03/04/2026

In the world of AI-assisted coding, tools like Claude Code have revolutionized how developers interact with large language models (LLMs) for tasks ranging from debugging to project scaffolding. However, one common pain point is managing system instructions — often stored in files like CLAUDE.md. These files, meant to guide the AI's behavior, can quickly become bloated, outdated, or ineffective. As agents grow smarter, they increasingly discover environmental details on their own, rendering static rules obsolete. But what if you need to enforce specific behaviors, like forcing the use of pnpm over npm? Simple prompts like "Use pnpm instead of npm" often fail because both terms linger in the context, leading to inconsistent results.

Enter Claude Code hooks — a powerful feature documented at code.claude.com. Hooks allow you to intercept and modify the AI's actions at key points, turning vague guidelines into ironclad rules. Not only can they prevent unwanted commands, but they can also rewrite them on the fly, ensuring compliance without relying on the model's probabilistic interpretation.

The Problem with Traditional Instructions

CLAUDE.md files typically contain a mix of rules: some deterministic (e.g., "Never use npm; always use pnpm") and others more advisory (e.g., "Prefer clean code structures"). The issue? LLMs process these holistically, but in practice:

  • Contexts can dilute instructions—mentions of "npm" elsewhere might confuse the model.
  • Outdated info persists, as agents can't always "unlearn" stale data.
  • Enforcement is non-deterministic; the AI might comply 90% of the time but slip up when it matters.

This is where hooks shine. By hooking into events like PreToolUse (before a tool runs), PostToolUse (after), or Stop (to halt execution), you can programmatically check and alter inputs/outputs. For instance, a hook could scan for npm in a command and replace it with pnpm, transforming the request seamlessly.

Converting CLAUDE.md Rules to Hooks: A Systematic Approach

To migrate from a bloated CLAUDE.md, use a structured process to identify and implement enforceable rules. Here's a prompt-inspired workflow:

1Task: Convert deterministic rules from @CLAUDE.md into Claude Code hooks.
2
3Goal
4Identify instructions in @CLAUDE.md that can be enforced deterministically
5(e.g. disallowed CLI commands, required CLI tools, forbidden file edits,
6restricted directories).
7
8Process
9
101. Parse @CLAUDE.md and extract all instructions.
11
122. Classify each instruction into:
13 - Deterministic (can be enforced by a hook)
14 - Non-deterministic (guidance only)
15
163. For deterministic instructions:
17 Map them to Claude Code hooks such as:
18 - PreToolUse
19 - PostToolUse
20 - Stop
21
224. For each hook:
23 - Explain which rule it enforces
24 - Show the hook configuration
25 - Use a separate bash script to implement the logic.
26
27Example hook script:
28
29```bash
30#!/bin/bash
31INPUT=$(cat)
32COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')
33
34if echo "$COMMAND" | grep -q "drop table"; then
35 echo "Blocked: dropping tables is not allowed" >&2
36 exit 2
37fi
38
39exit 0
40```
41
425. Before implementing anything:
43 Present a table with
44 - Instruction from CLAUDE.md
45 - Deterministic? (yes/no)
46 - Proposed hook type
47 - Enforcement strategy
48
496. Wait for user confirmation.
50
517. After confirmation:
52 - Generate hook scripts
53 - Generate hook configuration
54 - Place scripts in /hooks/
55 - Ensure scripts are executable.
56
578. Finally:
58 Provide instructions to test the hooks
59 (restart Claude Code and trigger a blocked command).
60

Claude will output a clean table first (e.g., npm → pnpm rewrite on PreToolUse), wait for your OK, then spit out ready scripts.

Why Hooks Win Over Plain Instructions

  • Guaranteed execution — no more “Claude forgot” moments
  • Rewriting > blocking — fix the command transparently (great for pnpm, bun vs npm, etc.)
  • Declutter CLAUDE.md — keep only high-level guidance and architecture there
  • Agent-friendly evolution — let Claude discover the repo dynamically, enforce only the must-never-happen rules

Common ideas to hook-ify: block rm -rf, protect .env/secrets, force lint before commit, rewrite deprecated commands, restrict edits to legacy folders.

Start small: pick one rule that’s bitten you before, run the prompt above, apply the hooks, and test. Once you see Claude obey 100%, you’ll never go back to hoping.

Happy forcing — and enjoy a cleaner, safer workflow!