Make Claude Code Auto‑Fix Its Own Bugs with a Ready‑to‑Copy Configuration

This article explains why Claude repeatedly makes the same coding errors, introduces a CLAUDE.md rule file and a series of hooks (PostToolUse, PreToolUse, Stop) plus an automatic retry loop and cross‑session memory, and shows a before‑after comparison that reduces manual debugging from 45 minutes to about 10 minutes per feature.

AI Architecture Hub
AI Architecture Hub
AI Architecture Hub
Make Claude Code Auto‑Fix Its Own Bugs with a Ready‑to‑Copy Configuration

Claude often fails to remember errors from previous turns, so each new conversation starts from scratch and the same bugs reappear. The author cites Anthropic’s advanced user guide, which recommends recording every error in a CLAUDE.md file to prevent repetition.

Step 1 – Self‑correcting CLAUDE.md

The file contains explicit, executable rules such as prohibiting refactoring, enforcing that all database queries go through services/, running npx tsc --noEmit after each change, and using conventional commit prefixes. It also includes a “Summarize from errors” section that logs each mistake (e.g., avoiding installing UI dependencies in packages/ui, not mixing development modes, ensuring loss‑function returns a number). The author notes that keeping the rule list around 12 items and under 200 lines maximizes Claude’s compliance.

Step 2 – PostToolUse Hook

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write(*.ts)",
        "hooks": [
          {"type": "command", "command": "npx prettier --write $file"},
          {"type": "command", "command": "npx tsc --noEmit 2>&1 | head -20"}
        ]
      },
      {
        "matcher": "Write(*.tsx)",
        "hooks": [
          {"type": "command", "command": "npx prettier --write $file"},
          {"type": "command", "command": "npx eslint --fix $file"}
        ]
      }
    ]
  }
}

After Claude writes a .ts file, Prettier formats it and TypeScript type‑checks it; after a .tsx file, ESLint fixes lint errors. This turns a manual cycle (5 × 45 min) into an automatic, real‑time fix.

Step 3 – Stop Hook (Quality Gate)

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {"type": "command", "command": "npm test 2>&1 | tail -5; echo \"Exit code: $\?\""}
        ]
      }
    ]
  }
}

When Claude signals completion, the Stop hook runs the test suite. If tests fail, Claude reads the failure log and continues fixing without human intervention.

Step 4 – PreToolUse Hook (Preventive Filtering)

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash(cat *log*)",
        "hooks": [{"type": "command", "command": "grep -n 'ERROR\\|WARN' $file | head -50"}]
      },
      {
        "matcher": "Write(**/.env*)",
        "hooks": [{"type": "command", "command": "echo 'Blocked: writing .env' && exit 1"}]
      }
    ]
  }
}

This hook extracts only the first 50 error lines from huge logs and blocks any attempt to write .env files, preventing costly mistakes.

Step 5 – Automatic Retry Mechanism

The author defines a prompt template that retries a failing test case up to three times. After each attempt, tests are run; success ends the loop, failure triggers error inspection and a new fix strategy. A token‑limit prevents infinite loops.

Step 6 – Cross‑Session Memory Learning

Claude can list learned facts via /memory and manually add rules (e.g., “this project uses pnpm, not npm”). Enabling “dream learning” lets Claude process past dialogues overnight and persist knowledge automatically.

Full settings.json (copy‑paste ready)

{
  "permissions": {
    "allow": [
      "Read", "Glob", "Grep", "LS", "Edit", "MultiEdit",
      "Write(src/**)", "Write(tests/**)",
      "Bash(npm test *)", "Bash(npx tsc *)", "Bash(npx prettier *)",
      "Bash(npx eslint *)", "Bash(git add *)", "Bash(git commit *)"
    ],
    "deny": [
      "Read(**/.env*)", "Write(**/.env*)",
      "Bash(rm -rf *)", "Bash(git push *)"
    ],
    "defaultMode": "acceptEdits"
  },
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write(*.ts)",
        "hooks": [
          {"type": "command", "command": "npx prettier --write $file"},
          {"type": "command", "command": "npx tsc --noEmit 2>&1 | head -20"}
        ]
      },
      {
        "matcher": "Write(*.tsx)",
        "hooks": [
          {"type": "command", "command": "npx prettier --write $file"},
          {"type": "command", "command": "npx eslint --fix $file"}
        ]
      }
    ],
    "PreToolUse": [
      {
        "matcher": "Bash(cat *log*)",
        "hooks": [{"type": "command", "command": "grep -n 'ERROR\\|WARN' $file | head -50"}]
      }
    ],
    "Stop": [
      {
        "hooks": [{"type": "command", "command": "npm test 2>&1 | tail -10; echo \"Exit: $\?\""}]
      }
    ]
  }
}

Configuration Before vs. After

Before configuration:

Claude writes code, tests fail (4 failures).

User manually explains each problem.

Claude fixes 3 issues but introduces a new bug.

Repeat manual explanation; each feature takes ~45 minutes.

The same bugs reappear the next day.

After configuration:

Claude writes code; hooks automatically format and type‑check all files.

When Claude declares completion, tests run automatically.

If tests fail, Claude reads the error log and continues fixing.

Memory feature prevents repeat of last week’s errors.

Each feature now requires only ~10 minutes, largely unattended.

The author concludes that the combined use of CLAUDE.md, hooks, retry loops, and memory learning dramatically cuts error rates and manual effort.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AIAutomationconfigurationHooksClaudeself-correction
AI Architecture Hub
Written by

AI Architecture Hub

Focused on sharing high-quality AI content and practical implementation, helping people learn with fewer missteps and become stronger through AI.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.