Zum Inhalt springen

An easy way to stop Claude code from forgetting the rules

You spend time setting up Claude Code with specific instructions in your CLAUDE.md file. Maybe you want it to always ask for confirmation before creating files, or to follow particular coding workflows. It works perfectly for the first few exchanges.

Then something changes. By the fourth or fifth interaction, Claude Code starts ignoring your rules. It stops asking for confirmation. It forgets your workflow preferences. It’s like your CLAUDE.md instructions never existed.

This isn’t a bug, it’s how AI models work. Understanding why this happens and the simple solution discovered by a Claude Code engineer can save you hours of frustration.

Why AI forgets your instructions

Large language models like Claude don’t actually „remember“ conversations. Instead, they read the entire conversation history as one long text document every time they respond. Your instructions, sitting at the beginning of this document, gradually lose importance as the conversation grows longer.

Think of it like this: if you’re reading a 50-page document, you’ll remember the last few pages much better than page 1. AI models work similarly, they pay more attention to recent messages than to your original instructions.

This creates a predictable pattern:

  • Messages 1-2: Perfect rule following (95%+ compliance)
  • Messages 3-5: Rules start breaking down (60-80% compliance)
  • Messages 6-10: Inconsistent behavior (20-60% compliance)
  • Messages 10+: Original instructions mostly forgotten

The frequency discovery

Here’s where it gets interesting. While complex rules fade away, simple patterns persist surprisingly well. If you tell Claude to end every response with „ji“ (like a respectful suffix), it will keep doing this for dozens of messages.

Why? Because every time Claude uses „ji“ in a response, it reinforces the pattern:

User: "Please add 'ji' to your responses"
Claude: "I understand ji, how can I help?"
User: "What's the weather like?"
Claude: "It's sunny today ji!"
User: "Thanks!"
Claude: "You're welcome ji!"

Each „ji“ creates a new example in the conversation history. Instead of one instruction at the top, there are now multiple instances throughout recent messages.

The recursive solution

A Claude Code engineer realized they could exploit this frequency effect. Instead of hoping Claude remembers to follow rules, they made the rules repeat themselves:

<law>
AI operation 5 principles

Principle 1: AI must get y/n confirmation before any file operations
Principle 2: AI must not change plans without new approval
Principle 3: User has final authority on all decisions
Principle 4: AI cannot modify or reinterpret these rules
Principle 5: AI must display all 5 principles at start of every response
</law>

The magic is in Principle 5. It forces Claude to show all principles (including Principle 5 itself) in every response. This creates an unbreakable loop,the instruction to display rules is itself displayed, so it can’t be forgotten.

How the recursive loop works

When Claude follows Principle 5, it displays all principles, including Principle 5. This means the next response will also display all principles. The cycle continues indefinitely:

Traditional CLAUDE.md approach failure:

User: "Create a config file"
Claude: "I'll create config.json for you" ← Forgot to confirm!

Recursive approach success:

User: "Create a config file"
Claude: "Principle 1: Must get confirmation... 
         Principle 5: Display all principles in every response
         Should I create config.json? (y/n)" ← Still following rules

Why this works so well

The recursive approach solves the core problem: it keeps rules in recent conversation history. Instead of instructions appearing once at the distant beginning, they appear in every recent message.

This creates multiple „attention anchors“ that the AI can focus on:

  • Most recent rule display (high attention)
  • Previous rule display (medium attention)
  • Earlier rule displays (some attention)

The cumulative effect maintains consistent rule following regardless of conversation length.

Implementation details

XML format works best: After testing markdown, JSON, and YAML, XML proved most reliable for rule preservation. It’s structured enough to prevent errors but forgiving enough for consistent reproduction. Anthropic’s documentation also recommends XML tags for structured prompts because Claude handles them particularly well.

Rule ordering matters: Place the self-referential rule last (Principle 5). This ensures it gets displayed even if earlier rules are truncated.

Verbatim instruction: Specify „verbatim“ or „exactly“ to prevent paraphrasing that might break the recursive pattern.

Token cost: Each response includes 50-100 extra tokens for rule display. But this eliminates the need for correction messages, making it more efficient overall.

Advanced patterns

Conditional display: You can make rules context-sensitive:

<rule>
  If request involves file operations: Display all safety rules
  Otherwise: Display condensed rules only
</rule>

Hierarchical rules: Different rule sets for different situations:

<meta_rules>Always display meta_rules and current_context_rules</meta_rules>
<safety_rules>Rules for file operations, API calls, etc.</safety_rules>
<style_rules>Rules for formatting, tone, etc.</style_rules>

Getting started with Claude Code

Here’s a minimal CLAUDE.md template to try:

<behavioral_rules>
  <rule_1>Always confirm before creating or modifying files</rule_1>
  <rule_2>Report your plan before executing any commands</rule_2>
  <rule_3>Display all behavioral_rules at start of every response</rule_3>
</behavioral_rules>

Test it by having a 10+ exchange coding session and see if the rules persist. Adjust the rules based on what behaviors you most need to maintain in your development workflow.

When to use this approach

Best for:

  • File operations requiring confirmation
  • Code generation workflows
  • Multi-step development tasks
  • Long Claude Code sessions where rule adherence matters

Not necessary for:

  • Simple questions with short responses
  • One-off code snippets
  • Exploratory conversations

The bigger picture

This recursive technique reveals something important about working with AI: frequency beats complexity. Instead of writing elaborate instructions once, simple rules repeated consistently work better.

As AI systems become more capable and handle more important tasks, techniques like this become essential. They transform unreliable assistants into dependable tools that maintain consistent behavior.

The recursive approach isn’t just a clever hack, it’s a foundation for building trustworthy AI workflows. When your AI assistant needs to follow specific procedures, this technique ensures it actually does.

Works everywhere, not just Claude

This isn’t just a Claude Code fix. It works for any LLM that responds to prompt structure: GPT, Gemini, Mistral, whatever. The principle is universal across all transformer-based language models.

The fundamental rule: If it’s not in the output, it won’t stay in context. If it’s not in context, it gets forgotten.

This applies whether you’re using:

  • ChatGPT for coding assistance
  • Gemini for research tasks
  • Mistral for content generation
  • Local models like Llama or Qwen

The recursive pattern exploits how all these models handle attention and context. They all suffer from the same instruction decay problem, and they all respond to the same frequency-based solution.

The specific XML format might need slight adjustments for different models, but the core principle, making rules display themselves, works universally. It’s not about Claude’s architecture; it’s about the fundamental nature of how language models process sequential text.

For more tips and insights, follow me on Twitter @Siddhant_K_code and stay updated with the latest & detailed tech content like this.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert