DEV Community

Cover image for Claude Code Hooks: Why Your AI Forgets Everything (5-Min Fix)
@kiwibreaksme
@kiwibreaksme

Posted on

Claude Code Hooks: Why Your AI Forgets Everything (5-Min Fix)

Every single session. The same conversation.

"Use TypeScript strict mode."
"Add JSDoc comments to functions."
"Don't touch the .env file."
"Ask me before making API calls."

I was spending the first 5 minutes of every Claude Code session just... repeating myself.

Sound familiar?


The Problem Nobody Talks About

AI coding assistants are incredibly powerful. But they have one fatal flaw:

They forget everything when the session ends.

  • Your carefully explained coding standards? Gone.
  • The architectural decisions you discussed for an hour? Vanished.
  • That one rule about never auto-committing? It doesn't exist anymore.

I tried CLAUDE.md files. They help, but Claude doesn't always read them first. I tried system prompts. Too clunky.

Then I discovered something that changed everything.


The Feature Hiding in Plain Sight

Claude Code Hooks.

Hooks are shell commands that run automatically at specific moments in Claude's lifecycle. Not suggestions. Not guidelines Claude might follow. Actual code that executes every single time.

Think of it like this
  • CLAUDE.md = "Hey Claude, please remember these rules" (polite request)
  • Hooks = "These rules WILL run, no exceptions" (enforced automation)

The difference? One relies on AI cooperation. The other just... works.



How Hooks Work (Simple Explanation)

Hooks trigger at specific events:

Event When It Fires Use Case
SessionStart Session begins Load your coding rules
PreToolUse Before any tool runs Block dangerous commands
PostToolUse After any tool runs Auto-format code
PreCompact Before context compression Preserve important context
Notification When Claude needs attention Desktop alerts

You configure them in .claude/settings.json:

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "echo 'Your rules here'"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

That's it. Every session, your rules load automatically.


5 Practical Hooks You Can Use Today

1️⃣ Never Forget Your Coding Standards

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "echo '[RULES] 1) TypeScript strict mode 2) Add JSDoc to all functions 3) No console.log in production code'"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Every session starts with your rules. No more repeating yourself.


2️⃣ Protect Sensitive Files

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "if echo \"$CLAUDE_TOOL_INPUT\" | grep -qE '(\\.env|credentials|secrets)'; then echo 'BLOCKED: Cannot edit sensitive files' >&2; exit 2; fi"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Claude literally cannot edit your .env files. Not "won't"—can't.


3️⃣ Auto-Format After Every Edit

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "file=$(echo \"$CLAUDE_TOOL_INPUT\" | jq -r '.file_path'); if [[ \"$file\" == *.ts ]]; then npx prettier --write \"$file\"; fi"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Every TypeScript file gets formatted automatically. No more "can you run prettier?"


4️⃣ Get Desktop Notifications

{
  "hooks": {
    "Notification": [
      {
        "matcher": ".",
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e 'display notification \"Claude needs your attention\" with title \"Claude Code\"'"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Walk away from your desk. Get pinged when Claude needs you.

💡 Use notify-send on Linux


5️⃣ Remember Rules After Context Compression

This one's important. When your conversation gets too long, Claude compresses the context—and often forgets your rules.

{
  "hooks": {
    "PreCompact": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "echo '[REMINDER] After compaction: TypeScript strict, JSDoc required, ask before API calls'"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Your rules survive the compression. Every time.


The Setup (Literally 2 Minutes)

Step 1: Create the settings file

mkdir -p .claude
touch .claude/settings.json
Enter fullscreen mode Exit fullscreen mode

Step 2: Add your hooks

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "echo '[My Coding Rules] 1) Your rule here 2) Another rule'"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Commit it to your repo

git add .claude/settings.json
git commit -m "Add Claude Code hooks"
Enter fullscreen mode Exit fullscreen mode

✅ Done. Every developer on your team now has the same rules.


But Wait—There's Still a Problem

Hooks solve the "rules" problem beautifully. But there's something they can't do:

They can't remember WHY you built things a certain way.

Imagine this scenario
  • Monday: You and Claude decide to use JWT instead of sessions. Good reasons discussed.
  • Wednesday: New session. Claude suggests using sessions.
  • You: "We already decided on JWT!"
  • Claude: "Oh, I didn't know that."

Hooks can remind Claude of rules. But they can't preserve the reasoning behind your architectural decisions.


The Missing Piece: Context in Code

This is why I built CodeSyncer.

GitHub logo bitjaru / codesyncer

Claude forgets everything when the session ends. CodeSyncer makes it remember.

CodeSyncer

Claude forgets everything when the session ends. CodeSyncer makes it remember.

npm version License GitHub stars

한국어 | English


The Problem → The Solution

Problem Without CodeSyncer With CodeSyncer
Context loss Every session = start from scratch @codesyncer-* tags = permanent memory
Forgot to tag Changes go untracked codesyncer watch catches everything
Rules forgotten AI forgets guidelines mid-session Hooks auto-remind at optimal moments
Dangerous inference AI guesses prices, security, APIs Auto-pause on critical keywords

Demo

CodeSyncer Demo


Quick Start

# 1. Install
npm install -g codesyncer

# 2. Initialize
cd /path/to/your/project
codesyncer init

# 3. Let AI set up (say this to Claude)
"Read .claude/SETUP_GUIDE.md and follow the instructions"

# 4. Start coding (say this each session)
"Read CLAUDE.md"
Enter fullscreen mode Exit fullscreen mode

Upgrade

npm install -g codesyncer@latest
Enter fullscreen mode Exit fullscreen mode

Core Features

🏷️ Tag System

Put context IN your code. AI reads code, so it recovers context automatically.

// @codesyncer-decision: [2024-01-15] Using JWT (session management is simpler)
Enter fullscreen mode Exit fullscreen mode

⭐ Star CodeSyncer on GitHub

The idea is simple: store context directly in your code comments.

/**
 * Authentication service
 *
 * @codesyncer-decision [2026-01-15] JWT over sessions (stateless, easier scaling)
 * @codesyncer-inference Token expiry 24h (standard for web apps)
 */
export class AuthService {
  // @codesyncer-rule Never log tokens, even in debug mode
  async login(credentials: Credentials) {
    // ...
  }
}
Enter fullscreen mode Exit fullscreen mode

Next session, Claude reads the code and instantly knows:

  • ✅ What was decided
  • ✅ Why it was decided
  • ✅ What rules apply

No repetition. No forgetting. The context lives in the code itself.


Hooks + CodeSyncer = Complete Solution

Problem Solution
AI forgets coding rules Hooks → Auto-load every session
AI forgets decisions @codesyncer-decision → Lives in code
AI makes assumptions @codesyncer-inference → Documents reasoning
AI touches sensitive logic @codesyncer-rule → Explicit warnings

CodeSyncer even sets up the hooks for you:

npx codesyncer init
Enter fullscreen mode Exit fullscreen mode

This creates:

  • .claude/settings.json with SessionStart and PreCompact hooks
  • .claude/CLAUDE.md with your coding rules
  • ✅ Tag system ready to use

Quick Start

🚀 Try CodeSyncer Now

Option 1: Just Hooks (Manual)

mkdir -p .claude
echo '{"hooks":{"SessionStart":[{"hooks":[{"type":"command","command":"echo [RULES] Your rules here"}]}]}}' > .claude/settings.json
Enter fullscreen mode Exit fullscreen mode

Option 2: Full Setup (Recommended)

npx codesyncer init
Enter fullscreen mode Exit fullscreen mode

TL;DR

Hook Purpose
SessionStart Load rules every session
PreCompact Preserve rules after compression
PreToolUse Block dangerous operations
PostToolUse Auto-format, auto-lint
Notification Desktop alerts

Hooks handle the "rules."
CodeSyncer handles the "reasoning."

Together, your AI finally stops forgetting.


Links

🔗 Claude Code Hooks Documentation
🔗 CodeSyncer GitHub


Have you tried hooks yet? What rules are you automating?

👇 Drop a comment below!

Top comments (0)