Skip to content

How to Use CLAUDE.md Effectively for Claude Code: Trigger-Action Rules That Work

Problem

My CLAUDE.md file wasn’t working. I’d write instructions like “be helpful” and “care about code quality,” but Claude would ignore them half the time. When I asked it to remember important details, it would forget them by the next session. When I told it to check for errors, it would skip that step and jump straight to implementation.

Here’s what my CLAUDE.md looked like:

"CLAUDE.md
# Claude Instructions
You are a helpful coding assistant. You care about code quality and testing. Always think through problems carefully. Remember what the user tells you. Be thorough in your responses and don't skip important details. Write clean, maintainable code.

When I worked with this file, I noticed:

  • Claude would sometimes run tests, sometimes skip them
  • It would forget context between sessions
  • It would ignore “be thorough” and give short responses
  • Instructions felt like optional suggestions rather than rules

What happened?

I searched for why my CLAUDE.md wasn’t being followed and found a Reddit discussion that explained the problem: I was writing personality descriptions instead of executable rules.

The key insight from that thread was: CLAUDE.md is not a prompt, it’s an operating system.

Looking at my file, I could see the issue:

  • “Be helpful” is subjective - Claude has to guess what that means
  • “Care about code quality” doesn’t specify what to check
  • “Think through problems” has no trigger - when should this happen?
  • “Remember what user tells you” doesn’t say when or how to store it

These descriptions leave too much room for interpretation. Claude follows them when it “feels like it,” not consistently.

How to solve it?

I rewrote my CLAUDE.md using trigger-action rules. Each rule specifies a condition and a mandatory action.

Here’s my new file:

"CLAUDE.md
# Claude Operating Rules
## Rule 1: Context Capture
**Trigger:** User shares information that might be needed later (file paths, preferences, project details)
**Action:** Before responding, check if this information should be written to a file. If yes, create/update a note file before answering.
## Rule 2: Test Coverage Check
**Trigger:** User requests new feature or bug fix
**Action:** Before implementing, check if test file exists. If no, create test file. If yes, verify coverage is >=80%. If below 80%, flag this before starting implementation.
## Rule 3: Linter Enforcement
**Trigger:** About to suggest code changes
**Action:** Run linter on affected files. If linter errors exist, show them first and ask if user wants to fix them before proceeding with suggestions.
## Rule 4: Git Safety
**Trigger:** User asks for destructive operation (refactor, delete, rewrite)
**Action:** First ask: "Should I create a git branch before starting?" Wait for confirmation. Then proceed with changes on new branch.
## Rule 5: Console.log Prevention
**Trigger:** Edit operation adds console.log to a file
**Action:** Immediately after edit, display warning: "console.log added to {filename}. Remove before committing?"

I tested each rule intentionally:

Test 1: Context capture I told Claude about a project preference:

I prefer to use functional components with hooks, not class components.

Under the old system, it would forget this. With the new rule, it responded:

[Rule 1 triggered: Context capture]
I'll save this preference to .claude-context.md before continuing.

Test 2: Test coverage I asked for a new feature:

Add a user authentication function

Under the old system, it would jump straight to coding. With Rule 2, it responded:

[Rule 2 triggered: Test coverage check]
Checking for test file...
No test file found. Creating auth.test.js before implementing.

Test 3: Git safety I requested a refactor:

Refactor the authentication module to use a cleaner pattern

Under the old system, it would start refactoring immediately on my main branch. With Rule 4:

[Rule 4 triggered: Destructive operation]
Should I create a git branch before starting? [y/n]

Now test again - I asked Claude to add a debug log:

Add console.log to track the user state

It added the log, then immediately showed:

[Rule 5 triggered: Console.log added]
console.log added to auth.js line 45. Remove before committing? [y/n]

The rules fire every time. No interpretation needed.

The reason

Trigger-action rules work because they eliminate ambiguity.

Binary conditions: The rule either fires or it doesn’t. There’s no “maybe” or “sometimes.” The trigger happened (user asked for destructive operation) or it didn’t.

Mandatory actions: Each rule specifies exactly what must happen. Not “try to remember” or “consider checking,” but “create branch” or “show warning.”

Consistent execution: The same input always produces the same action. When I ask for a refactor, Claude always asks about branching. When I add console.log, Claude always warns me.

Testable verification: I can verify whether a rule fired by looking for the expected output. Did Claude show the branch prompt? Yes. Did it create the test file? Yes.

I also learned some common mistakes to avoid:

Mistake 1: Writing goals instead of rules

# BAD: Goal
Write clean code.
# GOOD: Rule
**Trigger:** About to commit changes
**Action:** Run linter. If errors exist, stop and show them.

Mistake 2: Using soft language

# BAD: Soft
Try to remember important details.
# GOOD: Mandatory
**Trigger:** User shares project-specific information
**Action:** Write to .claude-context.md immediately.

Mistake 3: Describing what Claude “is” instead of what it “does”

# BAD: Description
You are thorough and careful.
# GOOD: Action
**Trigger:** User requests a change
**Action:** List all affected files and confirm before proceeding.

Mistake 4: Rules that are too broad

# BAD: Broad
Check everything before committing.
# GOOD: Specific
**Trigger:** Git commit command
**Action:** Check in order:
1. Linter errors?
2. Test failures?
3. console.log present?
4. Files >500 lines?

Advanced patterns

I also found some useful patterns in the Reddit thread:

Pattern 1: Decision tree rules

Decision Tree Example
## Error Handling Decision Tree
**Trigger:** Error encountered during operation
**Action:** Follow this tree:
1. Is error a network timeout? → Retry once with 2x timeout
2. Is error a permission issue? → Show exact file/path and suggest chmod/chown command
3. Is error a missing dependency? → Show install command with exact package name
4. Otherwise → Show full error stack and ask user how to proceed

Pattern 2: Pre-flight checklists

Pre-flight Checklist Example
## Pre-Commit Checklist
**Trigger:** User runs git commit command
**Action:** Before executing, check in order:
1. Run linter → if errors, stop and show them
2. Run tests → if failures, stop and show them
3. Check for console.log → if found, stop and list them
4. Check file size (>500 lines) → if exceeded, warn about splitting
5. If all pass → proceed with commit

Pattern 3: Context-aware behavior

Context Switching Example
## Context Switching
**Trigger:** User mentions a different file or project
**Action:**
1. Save current context to {project}/.claude-context.md
2. Read {new-project}/.claude-context.md if exists
3. Show confirmation: "Switched from {old} to {new}. Context loaded."

I tried the decision tree pattern when I hit an error during file operations:

Attempted to read /config/settings.json but got permission denied

Claude followed the tree:

[Error Handling Decision Tree triggered]
Error type: Permission issue
Action: Showing exact file/path and suggesting fix:
File: /config/settings.json
Owner: root
Current user: www-data
Suggested command:
sudo chown www-data:www-data /config/settings.json
Run this command? [y/n]

The pattern worked exactly as specified.

Summary

In this post, I showed how to make CLAUDE.md rules actually work by using trigger-action format instead of vague personality descriptions. The key point is treating CLAUDE.md like code: each rule specifies a clear trigger condition and a mandatory action. This eliminates interpretation and ensures consistent behavior across sessions.

When I switched from “be helpful” to “when X happens, do Y,” my rules went from being ignored 50% of the time to being followed 100% of the time. CLAUDE.md became an operating system, not a suggestion box.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments