<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Hooks on KnightLi Blog</title>
        <link>https://knightli.com/en/tags/hooks/</link>
        <description>Recent content in Hooks on KnightLi Blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en</language>
        <lastBuildDate>Fri, 01 May 2026 03:11:27 +0800</lastBuildDate><atom:link href="https://knightli.com/en/tags/hooks/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>Claude Code Hooks Mastery: An Introduction to 13 Hook Lifecycle Events and Automation Control</title>
        <link>https://knightli.com/en/2026/05/01/claude-code-hooks-mastery-guide/</link>
        <pubDate>Fri, 01 May 2026 03:11:27 +0800</pubDate>
        
        <guid>https://knightli.com/en/2026/05/01/claude-code-hooks-mastery-guide/</guid>
        <description>&lt;p&gt;&lt;code&gt;claude-code-hooks-mastery&lt;/code&gt; is a learning project focused on &lt;code&gt;Claude Code Hooks&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is not just a collection of scattered scripts. It explains the Claude Code hook lifecycle, configuration methods, script patterns, and common automation scenarios in one place. For people who want Claude Code to be more controllable and more like an engineering assistant, this kind of material is worth reading.&lt;/p&gt;
&lt;p&gt;Claude Code can already read code, edit files, and run commands by default. But if you want it to automatically check permissions, block risky operations, inject project rules, run tests, or remind it of team conventions at specific moments, chat instructions alone are not stable enough. The value of hooks is that they turn “rules I need to remind the AI about every time” into executable workflow.&lt;/p&gt;
&lt;h2 id=&#34;what-problems-hooks-solve&#34;&gt;What Problems Hooks Solve
&lt;/h2&gt;&lt;p&gt;After using Claude Code for a while, common pain points include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every new session needs the same project rules repeated&lt;/li&gt;
&lt;li&gt;You worry that it may run commands it should not run&lt;/li&gt;
&lt;li&gt;You want checks before and after file edits&lt;/li&gt;
&lt;li&gt;You want formatting, tests, or security scans before committing&lt;/li&gt;
&lt;li&gt;You want team conventions as fixed workflow instead of verbal reminders&lt;/li&gt;
&lt;li&gt;You want context before and after tool calls for logging or blocking&lt;/li&gt;
&lt;li&gt;You want complex tasks to trigger subagents or dedicated scripts&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hooks are designed for these “automatic actions at fixed moments.”&lt;/p&gt;
&lt;p&gt;You can think of them as event hooks in the Claude Code workflow. When a session starts, a user submits a prompt, the model is about to call a tool, a tool call finishes, or an agent is about to stop, Claude Code can run the scripts you configured.&lt;/p&gt;
&lt;h2 id=&#34;the-13-hook-lifecycle-events&#34;&gt;The 13 Hook Lifecycle Events
&lt;/h2&gt;&lt;p&gt;One of the main points in the project README is that it systematically covers the 13 Claude Code hook events.&lt;/p&gt;
&lt;p&gt;These events span multiple stages, from session startup to tool calls, and from user input to agent termination. By purpose, they can be roughly grouped as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Session startup: initialize environment and inject project context&lt;/li&gt;
&lt;li&gt;User input: inspect prompts, add rules, and perform auditing&lt;/li&gt;
&lt;li&gt;Before tool calls: permission checks, command blocking, and security validation&lt;/li&gt;
&lt;li&gt;After tool calls: log results, trigger formatting, and run verification&lt;/li&gt;
&lt;li&gt;Task ending: summarize, clean up, notify, or save state&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This lifecycle design means you do not need to put every rule into one very long prompt.&lt;/p&gt;
&lt;p&gt;For example, permission control should happen before tool calls. Formatting checks are better after file edits. Project rule injection is better at session startup or after user input. Putting rules at the right hook point is usually more reliable than stuffing everything into a system prompt.&lt;/p&gt;
&lt;h2 id=&#34;where-configuration-lives&#34;&gt;Where Configuration Lives
&lt;/h2&gt;&lt;p&gt;Claude Code hooks are usually configured through settings files.&lt;/p&gt;
&lt;p&gt;Common locations include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;User-level configuration: &lt;code&gt;~/.claude/settings.json&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Project-level configuration: &lt;code&gt;.claude/settings.json&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;User-level configuration is good for personal preferences, such as general security rules, command blocking, and log paths.&lt;/p&gt;
&lt;p&gt;Project-level configuration is better for repository-specific rules, such as which tests must run, which directories cannot be edited, how generated files are handled, and which checks are required before commit.&lt;/p&gt;
&lt;p&gt;If you use Claude Code in a team, it is better to put project-level configuration into the repository. That way everyone opens the project with the same AI collaboration constraints instead of relying on personal memory.&lt;/p&gt;
&lt;h2 id=&#34;why-single-file-scripts-matter&#34;&gt;Why Single-File Scripts Matter
&lt;/h2&gt;&lt;p&gt;The project emphasizes &lt;code&gt;UV&lt;/code&gt; single-file scripts.&lt;/p&gt;
&lt;p&gt;The benefit is simple deployment. A single Python file can declare dependencies and run without maintaining a complex environment for one hook. This fits hooks well because many hooks only do one small thing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Check whether a command is allowed&lt;/li&gt;
&lt;li&gt;Determine whether a file path is safe&lt;/li&gt;
&lt;li&gt;Read project rules and return them to Claude&lt;/li&gt;
&lt;li&gt;Scan output for sensitive information&lt;/li&gt;
&lt;li&gt;Run formatting or tests after edits&lt;/li&gt;
&lt;li&gt;Write events to logs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The smaller a hook script is, the easier it is to maintain, and the less likely it is to become a new complicated system.&lt;/p&gt;
&lt;h2 id=&#34;what-automation-can-hooks-do&#34;&gt;What Automation Can Hooks Do
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;claude-code-hooks-mastery&lt;/code&gt; shows many directions. In real work, the most common ones are below.&lt;/p&gt;
&lt;h3 id=&#34;1-permission-and-security-control&#34;&gt;1. Permission and Security Control
&lt;/h3&gt;&lt;p&gt;This is the most direct use of hooks.&lt;/p&gt;
&lt;p&gt;Before Claude Code executes a command, a hook can inspect the command content. If it contains high-risk actions such as deletion, reset, cleanup, or overwrite, it can block execution or require manual confirmation.&lt;/p&gt;
&lt;p&gt;Similar rules can apply to file paths:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Do not modify production configuration&lt;/li&gt;
&lt;li&gt;Do not write to secret files&lt;/li&gt;
&lt;li&gt;Do not delete migration scripts&lt;/li&gt;
&lt;li&gt;Do not touch specific directories&lt;/li&gt;
&lt;li&gt;Do not run unapproved network commands&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Putting this protection before tool calls is more reliable than writing “do not perform dangerous operations” in a prompt.&lt;/p&gt;
&lt;h3 id=&#34;2-context-injection&#34;&gt;2. Context Injection
&lt;/h3&gt;&lt;p&gt;Many projects have fixed background information:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Tech stack&lt;/li&gt;
&lt;li&gt;Coding conventions&lt;/li&gt;
&lt;li&gt;Test commands&lt;/li&gt;
&lt;li&gt;Branching strategy&lt;/li&gt;
&lt;li&gt;Directory structure&lt;/li&gt;
&lt;li&gt;Prohibited actions&lt;/li&gt;
&lt;li&gt;Rules for generated files&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Telling Claude Code this manually every time is annoying and easy to forget. Hooks can automatically inject necessary context at session startup or after the user submits a prompt.&lt;/p&gt;
&lt;p&gt;This is like giving Claude Code a project-level work manual. It does not replace the README or development documentation, but it helps AI enter the correct state before executing a task.&lt;/p&gt;
&lt;h3 id=&#34;3-verification-after-edits&#34;&gt;3. Verification After Edits
&lt;/h3&gt;&lt;p&gt;After Claude Code modifies files, hooks can automatically trigger checks.&lt;/p&gt;
&lt;p&gt;Common actions include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Run formatting&lt;/li&gt;
&lt;li&gt;Run lint&lt;/li&gt;
&lt;li&gt;Run unit tests&lt;/li&gt;
&lt;li&gt;Check type errors&lt;/li&gt;
&lt;li&gt;Scan generated files&lt;/li&gt;
&lt;li&gt;Validate Markdown or JSON format&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This helps reduce low-level mistakes. When AI edits multiple files, a lightweight verification pass after modification can reveal problems earlier.&lt;/p&gt;
&lt;p&gt;However, hooks should not run heavy tasks by default. Running the full test suite after every file change can make the experience slow. A better approach is to choose checks based on file type, directory, and task risk.&lt;/p&gt;
&lt;h3 id=&#34;4-team-rule-validation&#34;&gt;4. Team Rule Validation
&lt;/h3&gt;&lt;p&gt;If a team already has clear conventions, some of them can be placed in hooks.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Commit message format&lt;/li&gt;
&lt;li&gt;Code style rules&lt;/li&gt;
&lt;li&gt;Do not directly edit certain generated files&lt;/li&gt;
&lt;li&gt;Documentation must be updated together&lt;/li&gt;
&lt;li&gt;API changes must update tests&lt;/li&gt;
&lt;li&gt;Certain directories can only be generated by specific tools&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This makes Claude Code more like part of the team workflow rather than an unconstrained external assistant.&lt;/p&gt;
&lt;p&gt;Of course, hooks should not replace CI. They are better for local reminders and early blocking. Final validation should still belong to CI, review, and test systems.&lt;/p&gt;
&lt;h3 id=&#34;5-subagents-and-dedicated-tasks&#34;&gt;5. Subagents and Dedicated Tasks
&lt;/h3&gt;&lt;p&gt;The README also mentions subagent-related content.&lt;/p&gt;
&lt;p&gt;This type of usage is suitable for sending complex tasks into more specialized workflows. For example, the main conversation can understand the requirement, while a hook or configuration triggers dedicated checking, auditing, summarizing, or documentation tasks.&lt;/p&gt;
&lt;p&gt;For individual users, the first useful step is not complex agent orchestration. It is better to hand repetitive, clear, low-risk actions to hooks first. More complex automation can come after the rules become stable.&lt;/p&gt;
&lt;h2 id=&#34;statusline-and-output-styles&#34;&gt;Statusline and Output Styles
&lt;/h2&gt;&lt;p&gt;The project also covers statusline and output styles.&lt;/p&gt;
&lt;p&gt;This may look like a small experience detail, but it matters for long-term Claude Code usage. A statusline can show current context, task state, environment information, or hints. Output styles can make Claude Code answers fit your working habits better.&lt;/p&gt;
&lt;p&gt;If you collaborate with AI in the same terminal every day, these details affect efficiency. Good status hints reduce mistakes and help you quickly determine whether the current session is in the right project, branch, and environment.&lt;/p&gt;
&lt;h2 id=&#34;do-not-make-hooks-too-heavy&#34;&gt;Do Not Make Hooks Too Heavy
&lt;/h2&gt;&lt;p&gt;Hooks are powerful, but they are not the place to put everything.&lt;/p&gt;
&lt;p&gt;Good rules are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;High-frequency actions should be fast&lt;/li&gt;
&lt;li&gt;Security blocking should be clear&lt;/li&gt;
&lt;li&gt;Output should be short&lt;/li&gt;
&lt;li&gt;Failure reasons should be readable&lt;/li&gt;
&lt;li&gt;Scripts should have a single responsibility&lt;/li&gt;
&lt;li&gt;Heavy checks should be explicit commands or CI tasks&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If a hook takes more than ten seconds every time, users will soon want to disable it. If a hook has vague blocking rules, both Claude Code and the user will struggle to understand what to do next.&lt;/p&gt;
&lt;p&gt;Hooks are best for tasks with clear boundaries: allow or reject, add context, log events, run lightweight checks, and suggest the next step.&lt;/p&gt;
&lt;h2 id=&#34;who-should-use-it&#34;&gt;Who Should Use It
&lt;/h2&gt;&lt;p&gt;If you only occasionally ask Claude Code to edit a small piece of code, you may not need to study hooks deeply yet.&lt;/p&gt;
&lt;p&gt;But this project is useful if you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use Claude Code frequently&lt;/li&gt;
&lt;li&gt;Often let AI modify real project code&lt;/li&gt;
&lt;li&gt;Worry about AI running dangerous commands&lt;/li&gt;
&lt;li&gt;Want to automatically inject team rules into AI workflows&lt;/li&gt;
&lt;li&gt;Want checks to run automatically after edits&lt;/li&gt;
&lt;li&gt;Want to turn repeated reminders into configuration&lt;/li&gt;
&lt;li&gt;Are building a more stable AI coding workflow&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hooks are especially meaningful in collaborative projects. They can turn part of team experience into scripts instead of relying on every person to remind AI manually.&lt;/p&gt;
&lt;h2 id=&#34;notes-for-use&#34;&gt;Notes for Use
&lt;/h2&gt;&lt;p&gt;First, start with security hooks.&lt;/p&gt;
&lt;p&gt;Compared with complex automation, command blocking, path protection, and sensitive file checks are easier to implement and immediately reduce risk.&lt;/p&gt;
&lt;p&gt;Second, commit project-level rules carefully.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;.claude/settings.json&lt;/code&gt; affects everyone who uses the repository. Before committing rules, make sure they do not over-restrict normal development or depend on paths that only exist on your machine.&lt;/p&gt;
&lt;p&gt;Third, keep hook output concise.&lt;/p&gt;
&lt;p&gt;Claude Code consumes this output. If it is too long, it pollutes the context. If it is too vague, it does not guide the next step. It is best to return only the necessary judgment and next recommendation.&lt;/p&gt;
&lt;p&gt;Fourth, keep hooks debuggable.&lt;/p&gt;
&lt;p&gt;When hooks increase in number, problems can come from configuration, scripts, permissions, paths, dependencies, or Claude Code itself. Clear logs make later debugging much easier.&lt;/p&gt;
&lt;h2 id=&#34;reference&#34;&gt;Reference
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://github.com/disler/claude-code-hooks-mastery&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;disler/claude-code-hooks-mastery&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;final-thought&#34;&gt;Final Thought
&lt;/h2&gt;&lt;p&gt;The value of &lt;code&gt;Claude Code Hooks&lt;/code&gt; is turning “rules I hope AI remembers every time” into workflows that actually execute.&lt;/p&gt;
&lt;p&gt;If you already use Claude Code in real projects, hooks are a key step from “a coding assistant that can chat” toward “a constrained engineering collaborator.”&lt;/p&gt;
</description>
        </item>
        <item>
        <title>Claude Code&#39;s Four-Part Environment Setup: CLAUDE.md, Rules, Memory, and Hooks Explained</title>
        <link>https://knightli.com/en/2026/04/23/claude-code-claude-md-rules-memory-hooks-guide/</link>
        <pubDate>Thu, 23 Apr 2026 10:43:40 +0800</pubDate>
        
        <guid>https://knightli.com/en/2026/04/23/claude-code-claude-md-rules-memory-hooks-guide/</guid>
        <description>&lt;p&gt;If you use &lt;code&gt;Claude Code&lt;/code&gt; for a while, you quickly realize something: the model itself is important, but the environment you give it, the boundaries you define, and the rules you set matter just as much.&lt;/p&gt;
&lt;p&gt;At first, many people focus on &amp;ldquo;how should I write this prompt?&amp;rdquo; But once you really start using &lt;code&gt;Claude Code&lt;/code&gt; seriously, you care more about something else:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Does it know who you are?&lt;/li&gt;
&lt;li&gt;Does it know how you work?&lt;/li&gt;
&lt;li&gt;Does it know which rules cannot be broken?&lt;/li&gt;
&lt;li&gt;Does it know which actions require confirmation first?&lt;/li&gt;
&lt;li&gt;Can it remember these boundaries over time?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What makes &lt;code&gt;Claude Code&lt;/code&gt; a mature tool is not just model capability. It is that there is a whole system for turning your working style into persistent structure. At a high level, that system can be divided into four layers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CLAUDE.md&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Rules&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Memory&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Hooks&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article explains all four in one pass.&lt;/p&gt;
&lt;h2 id=&#34;why-environment-setup-matters-more-than-one-off-prompts&#34;&gt;Why environment setup matters more than one-off prompts
&lt;/h2&gt;&lt;p&gt;You can think of &lt;code&gt;Claude Code&lt;/code&gt; as an assistant you hired.&lt;/p&gt;
&lt;p&gt;On day one, you would not just tell them, &amp;ldquo;help me do things.&amp;rdquo; You would give them a handbook and explain:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;who you are&lt;/li&gt;
&lt;li&gt;what communication style you prefer&lt;/li&gt;
&lt;li&gt;which actions always require confirmation&lt;/li&gt;
&lt;li&gt;which mistakes have happened before and must not happen again&lt;/li&gt;
&lt;li&gt;where the most important project documents live&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That is why, in the long run, environment setup usually matters more than a single prompt.&lt;/p&gt;
&lt;p&gt;A prompt solves &amp;ldquo;what should we do this time?&amp;rdquo; Environment setup solves &amp;ldquo;how should we work every time from now on?&amp;rdquo;&lt;/p&gt;
&lt;h2 id=&#34;layer-1-claudemd&#34;&gt;Layer 1: &lt;code&gt;CLAUDE.md&lt;/code&gt;
&lt;/h2&gt;&lt;p&gt;Start with the most basic piece. &lt;code&gt;CLAUDE.md&lt;/code&gt; is essentially just a text file.&lt;/p&gt;
&lt;p&gt;You can write instructions for Claude in it, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;who you are&lt;/li&gt;
&lt;li&gt;what you are working on&lt;/li&gt;
&lt;li&gt;your communication preferences&lt;/li&gt;
&lt;li&gt;rules that must be followed&lt;/li&gt;
&lt;li&gt;special background for the current project&lt;/li&gt;
&lt;li&gt;where important documents or directories are&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Every time &lt;code&gt;Claude Code&lt;/code&gt; starts, this document is automatically injected into context, so the model will definitely read it.&lt;/p&gt;
&lt;p&gt;I usually think of it as a &amp;ldquo;shared understanding file&amp;rdquo;, because that is what it really is: the standing agreement between you and the model.&lt;/p&gt;
&lt;h3 id=&#34;what-belongs-in-claudemd&#34;&gt;What belongs in &lt;code&gt;CLAUDE.md&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;The best things to put in &lt;code&gt;CLAUDE.md&lt;/code&gt; are usually these kinds of information:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;identity and work background&lt;/li&gt;
&lt;li&gt;tone and output preferences&lt;/li&gt;
&lt;li&gt;global behavior rules&lt;/li&gt;
&lt;li&gt;important project background that comes up often&lt;/li&gt;
&lt;li&gt;common mistakes and how to avoid them&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;your time zone&lt;/li&gt;
&lt;li&gt;whether you allow the model to send emails or messages directly&lt;/li&gt;
&lt;li&gt;which actions count as irreversible&lt;/li&gt;
&lt;li&gt;your habits for handling documents and files&lt;/li&gt;
&lt;li&gt;security practices and boundaries around sensitive information&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;one-very-important-principle-keep-it-concise&#34;&gt;One very important principle: keep it concise
&lt;/h3&gt;&lt;p&gt;There is one especially important principle for &lt;code&gt;CLAUDE.md&lt;/code&gt;: keep it as concise as possible.&lt;/p&gt;
&lt;p&gt;The reason is simple: it gets injected into context every time.&lt;/p&gt;
&lt;p&gt;If it becomes too long, it takes up too much context space and dilutes the information that actually matters. The model is not ignoring it, but its attention becomes more spread out, so it is more likely to miss the rules you care about most.&lt;/p&gt;
&lt;p&gt;The official recommendation is usually to stay under &lt;code&gt;400&lt;/code&gt; lines.&lt;/p&gt;
&lt;p&gt;My own habit is even more conservative: I try to keep it under &lt;code&gt;200&lt;/code&gt; lines.&lt;/p&gt;
&lt;h3 id=&#34;common-scopes-for-claudemd&#34;&gt;Common scopes for &lt;code&gt;CLAUDE.md&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;In practice, &lt;code&gt;CLAUDE.md&lt;/code&gt; can exist at different levels, and those levels determine its scope. The two most common ones are:&lt;/p&gt;
&lt;h4 id=&#34;1-user-level&#34;&gt;1. User Level
&lt;/h4&gt;&lt;p&gt;This is the global level.&lt;/p&gt;
&lt;p&gt;It lives in your machine-level environment and applies to all projects you work on locally.&lt;/p&gt;
&lt;p&gt;This is a good place for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;your identity information&lt;/li&gt;
&lt;li&gt;general communication preferences&lt;/li&gt;
&lt;li&gt;habits that apply across projects&lt;/li&gt;
&lt;li&gt;global security rules&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, if your time zone is not the default one people often assume, but Bangkok time, that fits very well at the &lt;code&gt;user level&lt;/code&gt;, because it helps the model avoid mistakes whenever it works with dates and times later.&lt;/p&gt;
&lt;h4 id=&#34;2-project-level&#34;&gt;2. Project Level
&lt;/h4&gt;&lt;p&gt;This is the project level.&lt;/p&gt;
&lt;p&gt;It sits inside a specific project directory and only applies to that project.&lt;/p&gt;
&lt;p&gt;This is a good place for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;project-specific background&lt;/li&gt;
&lt;li&gt;rules that only make sense in this project&lt;/li&gt;
&lt;li&gt;explanations of the project&amp;rsquo;s directory structure&lt;/li&gt;
&lt;li&gt;entry points to the project&amp;rsquo;s key documents&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, if one project handles finance and another handles HR, the background and constraints are obviously different, so they should not live in the same global instructions.&lt;/p&gt;
&lt;h3 id=&#34;how-to-decide-which-level-to-use&#34;&gt;How to decide which level to use
&lt;/h3&gt;&lt;p&gt;The rule is actually simple:&lt;/p&gt;
&lt;p&gt;If the thing you are writing would still be true in another project, put it at the &lt;code&gt;user level&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If it stops being true as soon as you switch projects, put it at the &lt;code&gt;project level&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;how-to-write-the-first-version&#34;&gt;How to write the first version
&lt;/h3&gt;&lt;p&gt;There are two common ways to get started:&lt;/p&gt;
&lt;h4 id=&#34;1-use-init&#34;&gt;1. Use &lt;code&gt;/init&lt;/code&gt;
&lt;/h4&gt;&lt;p&gt;You can run the slash command &lt;code&gt;/init&lt;/code&gt; directly in the terminal and let Claude scan the current project to generate a basic &lt;code&gt;CLAUDE.md&lt;/code&gt; for you.&lt;/p&gt;
&lt;h4 id=&#34;2-let-claude-help-you-organize-it&#34;&gt;2. Let Claude help you organize it
&lt;/h4&gt;&lt;p&gt;You can also ask Claude to look up how other people structure &lt;code&gt;CLAUDE.md&lt;/code&gt;, ask you some questions based on your situation, and then help you organize a version that fits you.&lt;/p&gt;
&lt;p&gt;In many cases, that is much easier than writing it from scratch yourself.&lt;/p&gt;
&lt;h3 id=&#34;one-very-practical-habit&#34;&gt;One very practical habit
&lt;/h3&gt;&lt;p&gt;As you collaborate with Claude over time, whenever you notice something that definitely needs to be remembered in the future, or something that must not go wrong again, you can ask it to write that into &lt;code&gt;CLAUDE.md&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Before doing that, though, you still want to decide:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;is this a global rule?&lt;/li&gt;
&lt;li&gt;or a rule for the current project only?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Do not dump everything into one file.&lt;/p&gt;
&lt;h2 id=&#34;layer-2-rules&#34;&gt;Layer 2: &lt;code&gt;Rules&lt;/code&gt;
&lt;/h2&gt;&lt;p&gt;Next is &lt;code&gt;Rules&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The biggest difference between it and &lt;code&gt;CLAUDE.md&lt;/code&gt; is not the file format. It is how it gets loaded.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CLAUDE.md&lt;/code&gt; is read no matter what you are doing.&lt;/p&gt;
&lt;p&gt;The advantage of &lt;code&gt;Rules&lt;/code&gt; is that they can be &lt;strong&gt;loaded conditionally&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In other words, a rule can be loaded only for certain paths, files, tools, or scenarios.&lt;/p&gt;
&lt;h3 id=&#34;why-conditional-loading-matters&#34;&gt;Why conditional loading matters
&lt;/h3&gt;&lt;p&gt;Because context space is always scarce.&lt;/p&gt;
&lt;p&gt;If every rule gets shoved into context all the time, two things happen:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the model carries more overhead&lt;/li&gt;
&lt;li&gt;truly important rules get buried&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That is the value of loading rules on demand: the model sees the right information at the right time.&lt;/p&gt;
&lt;h3 id=&#34;when-to-move-rules-from-claudemd-into-rules&#34;&gt;When to move rules from &lt;code&gt;CLAUDE.md&lt;/code&gt; into &lt;code&gt;Rules&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;Usually there are two situations:&lt;/p&gt;
&lt;h4 id=&#34;1-claudemd-has-become-too-long&#34;&gt;1. &lt;code&gt;CLAUDE.md&lt;/code&gt; has become too long
&lt;/h4&gt;&lt;p&gt;If your &lt;code&gt;CLAUDE.md&lt;/code&gt; starts going beyond &lt;code&gt;200&lt;/code&gt; lines, keeps growing, and the important content gets diluted, it is time to split some rules out.&lt;/p&gt;
&lt;h4 id=&#34;2-some-rules-only-apply-to-specific-paths&#34;&gt;2. Some rules only apply to specific paths
&lt;/h4&gt;&lt;p&gt;If you clearly know that some rules only make sense for certain kinds of files, for example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;rules that only apply to Python scripts&lt;/li&gt;
&lt;li&gt;rules that only apply to a hooks directory&lt;/li&gt;
&lt;li&gt;rules that only apply to one subproject&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;then those rules belong in &lt;code&gt;Rules&lt;/code&gt; much more naturally.&lt;/p&gt;
&lt;h3 id=&#34;where-rules-fit-best&#34;&gt;Where &lt;code&gt;Rules&lt;/code&gt; fit best
&lt;/h3&gt;&lt;p&gt;The most typical use case is &amp;ldquo;specific situation, specific path, specific file type&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;conventions that only apply when handling hook files&lt;/li&gt;
&lt;li&gt;coding rules that only matter in a certain class of scripts&lt;/li&gt;
&lt;li&gt;ways of working that only apply under one directory&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keeping that kind of content inside &lt;code&gt;CLAUDE.md&lt;/code&gt; is usually not cost-effective.&lt;/p&gt;
&lt;h2 id=&#34;layer-3-memory&#34;&gt;Layer 3: &lt;code&gt;Memory&lt;/code&gt;
&lt;/h2&gt;&lt;p&gt;The third layer is &lt;code&gt;Memory&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Like &lt;code&gt;CLAUDE.md&lt;/code&gt; and &lt;code&gt;Rules&lt;/code&gt;, it also enters model context, but its core difference is this:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CLAUDE.md&lt;/code&gt; is something you define deliberately.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Memory&lt;/code&gt; is more like notes Claude writes for itself during collaboration.&lt;/p&gt;
&lt;h3 id=&#34;what-goes-into-memory&#34;&gt;What goes into &lt;code&gt;Memory&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;When Claude judges that something is worth remembering, or should be kept for a while, it writes that information into &lt;code&gt;Memory&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Common examples include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a way of working you corrected&lt;/li&gt;
&lt;li&gt;a newly added preference&lt;/li&gt;
&lt;li&gt;temporary state in the current project&lt;/li&gt;
&lt;li&gt;something you did not finish today and need to continue tomorrow&lt;/li&gt;
&lt;li&gt;who you have been collaborating with recently&lt;/li&gt;
&lt;li&gt;personal information or context that only came up recently&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In other words, &lt;code&gt;Memory&lt;/code&gt; is closer to dynamic knowledge than long-term policy.&lt;/p&gt;
&lt;h3 id=&#34;how-it-differs-from-the-first-two-layers&#34;&gt;How it differs from the first two layers
&lt;/h3&gt;&lt;p&gt;A simple distinction is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CLAUDE.md&lt;/code&gt; / &lt;code&gt;Rules&lt;/code&gt;: long-term, policy-like, explicit rules&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Memory&lt;/code&gt;: temporary, dynamic, newly learned context during work&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If something only matters for the next few days, or keeps changing as the project evolves, it usually belongs in &lt;code&gt;Memory&lt;/code&gt;, not in a permanent rule file.&lt;/p&gt;
&lt;h3 id=&#34;memory-can-also-be-written-manually&#34;&gt;&lt;code&gt;Memory&lt;/code&gt; can also be written manually
&lt;/h3&gt;&lt;p&gt;Even though &lt;code&gt;Memory&lt;/code&gt; can be maintained automatically, you can also explicitly tell Claude things like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;please remember what I need to do tomorrow&lt;/li&gt;
&lt;li&gt;please remember whose status I need to follow up on&lt;/li&gt;
&lt;li&gt;please remember the key milestone for this project this month&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It can write that into &lt;code&gt;Memory&lt;/code&gt; for you.&lt;/p&gt;
&lt;p&gt;You can also use the slash command &lt;code&gt;/memory&lt;/code&gt; to see what memories currently exist and edit or delete them manually.&lt;/p&gt;
&lt;p&gt;That said, I personally do not maintain it manually too often, because Claude itself can periodically reorganize these memories and clear out what has gone stale.&lt;/p&gt;
&lt;h2 id=&#34;layer-4-hooks&#34;&gt;Layer 4: &lt;code&gt;Hooks&lt;/code&gt;
&lt;/h2&gt;&lt;p&gt;The last and most advanced layer is &lt;code&gt;Hooks&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Everything before this, including &lt;code&gt;CLAUDE.md&lt;/code&gt;, &lt;code&gt;Rules&lt;/code&gt;, and &lt;code&gt;Memory&lt;/code&gt;, is still ultimately natural-language guidance.&lt;/p&gt;
&lt;p&gt;You write rules, and the model usually follows them, but it is still operating by interpreting them first and then acting.&lt;/p&gt;
&lt;p&gt;As long as the rule lives in natural language, a few problems remain:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the model may occasionally miss it&lt;/li&gt;
&lt;li&gt;too many rules can dilute attention&lt;/li&gt;
&lt;li&gt;in some situations, the model may decide the rule is not important enough&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That is not because you wrote the rule badly. It is because natural-language rules are hard to enforce with &lt;code&gt;100%&lt;/code&gt; reliability.&lt;/p&gt;
&lt;h3 id=&#34;what-hooks-really-are&#34;&gt;What &lt;code&gt;Hooks&lt;/code&gt; really are
&lt;/h3&gt;&lt;p&gt;&lt;code&gt;Hooks&lt;/code&gt; are no longer natural-language instructions. They are scripts.&lt;/p&gt;
&lt;p&gt;They are event-triggered, program-level enforcement logic.&lt;/p&gt;
&lt;p&gt;Once a certain event happens, that logic will run. It will not be skipped because the model decided to ignore it.&lt;/p&gt;
&lt;p&gt;That is the key value of &lt;code&gt;Hooks&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;they turn &amp;ldquo;should follow this&amp;rdquo; into &amp;ldquo;must execute this&amp;rdquo;.&lt;/p&gt;
&lt;h3 id=&#34;when-you-should-upgrade-to-hooks&#34;&gt;When you should upgrade to &lt;code&gt;Hooks&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;If you notice that a rule is already written in &lt;code&gt;CLAUDE.md&lt;/code&gt; or &lt;code&gt;Rules&lt;/code&gt;, but Claude still occasionally fails to follow it, and missing it carries real risk, then that rule should probably become a &lt;code&gt;Hook&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In simple terms:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;low-risk behavior: use rules&lt;/li&gt;
&lt;li&gt;high-risk behavior: use &lt;code&gt;Hooks&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;the-most-typical-hooks-scenarios&#34;&gt;The most typical &lt;code&gt;Hooks&lt;/code&gt; scenarios
&lt;/h3&gt;&lt;p&gt;The most obvious examples are actions you absolutely do not want to get wrong, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;requiring confirmation before sending an email&lt;/li&gt;
&lt;li&gt;requiring confirmation before sending Slack, Outlook, or Gmail messages&lt;/li&gt;
&lt;li&gt;intercepting dangerous file deletions&lt;/li&gt;
&lt;li&gt;blocking the outbound leak of passwords or API keys&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If those are only written as natural-language rules, the model can still make a mistake someday.&lt;/p&gt;
&lt;p&gt;If they are implemented as &lt;code&gt;Hooks&lt;/code&gt;, the event gets intercepted every time.&lt;/p&gt;
&lt;p&gt;That is a real program-level safety barrier.&lt;/p&gt;
&lt;h3 id=&#34;common-trigger-points-for-hooks&#34;&gt;Common trigger points for &lt;code&gt;Hooks&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;&lt;code&gt;Hooks&lt;/code&gt; can be attached at different stages, for example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;injecting reminders at the start of a conversation&lt;/li&gt;
&lt;li&gt;checking conditions before a tool runs&lt;/li&gt;
&lt;li&gt;validating results after a tool runs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You do not necessarily need to know the formal terminology yourself.&lt;/p&gt;
&lt;p&gt;Often, as long as you can describe the requirement clearly and ask Claude whether it should become a hook, it can help you design it.&lt;/p&gt;
&lt;p&gt;You can also use the slash command &lt;code&gt;/hook&lt;/code&gt; to inspect which hooks are currently configured.&lt;/p&gt;
&lt;h2 id=&#34;a-more-practical-way-to-get-started&#34;&gt;A more practical way to get started
&lt;/h2&gt;&lt;p&gt;If you want to connect all four layers together, I usually recommend this order:&lt;/p&gt;
&lt;h3 id=&#34;step-1-use-init-to-generate-a-basic-claudemd&#34;&gt;Step 1: use &lt;code&gt;/init&lt;/code&gt; to generate a basic &lt;code&gt;CLAUDE.md&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;Do not try to hand-write a huge complete rule document at the start.&lt;/p&gt;
&lt;p&gt;Let Claude scan the project and generate a starting point, then iterate from there.&lt;/p&gt;
&lt;h3 id=&#34;step-2-add-things-as-you-work&#34;&gt;Step 2: add things as you work
&lt;/h3&gt;&lt;p&gt;As you collaborate, whenever you notice:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;this must be remembered in the future&lt;/li&gt;
&lt;li&gt;this mistake must not happen again&lt;/li&gt;
&lt;li&gt;this preference will keep applying every time&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;then ask Claude to add it to &lt;code&gt;CLAUDE.md&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;step-3-move-things-into-rules-once-claudemd-grows&#34;&gt;Step 3: move things into &lt;code&gt;Rules&lt;/code&gt; once &lt;code&gt;CLAUDE.md&lt;/code&gt; grows
&lt;/h3&gt;&lt;p&gt;Once &lt;code&gt;CLAUDE.md&lt;/code&gt; gets longer and longer, and the model no longer reliably follows every rule, split things out:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;which rules are global?&lt;/li&gt;
&lt;li&gt;which ones only apply to certain paths?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Move the second kind into &lt;code&gt;Rules&lt;/code&gt; so they load conditionally.&lt;/p&gt;
&lt;h3 id=&#34;step-4-upgrade-high-risk-rules-into-hooks&#34;&gt;Step 4: upgrade high-risk rules into &lt;code&gt;Hooks&lt;/code&gt;
&lt;/h3&gt;&lt;p&gt;If some rules still get missed even after you wrote them down, and the cost of missing them is high, do not stay in the natural-language layer. Upgrade them into &lt;code&gt;Hooks&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That is the point where &amp;ldquo;reminder&amp;rdquo; becomes &amp;ldquo;enforcement&amp;rdquo;.&lt;/p&gt;
&lt;h3 id=&#34;step-5-let-memory-handle-temporary-state&#34;&gt;Step 5: let &lt;code&gt;Memory&lt;/code&gt; handle temporary state
&lt;/h3&gt;&lt;p&gt;For things that expire, change frequently, or are not long-term policy, do not shove everything into &lt;code&gt;CLAUDE.md&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is usually cleaner to let &lt;code&gt;Memory&lt;/code&gt; hold things like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;current project progress&lt;/li&gt;
&lt;li&gt;recent collaborators&lt;/li&gt;
&lt;li&gt;newly added preferences&lt;/li&gt;
&lt;li&gt;short-term plans and to-dos&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That keeps context cleaner and makes the model behave more consistently.&lt;/p&gt;
&lt;h2 id=&#34;what-each-layer-should-store&#34;&gt;What each layer should store
&lt;/h2&gt;&lt;p&gt;If you want a quick mental model, use this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CLAUDE.md&lt;/code&gt;: long-term shared understanding, global instructions, foundational project background&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Rules&lt;/code&gt;: specialized rules loaded by path or scenario&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Memory&lt;/code&gt;: dynamic knowledge, temporary state, things learned recently&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Hooks&lt;/code&gt;: program-level enforcement for high-risk actions&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;closing&#34;&gt;Closing
&lt;/h2&gt;&lt;p&gt;Many people treat &lt;code&gt;Claude Code&lt;/code&gt; as &amp;ldquo;a chat interface that can write code&amp;rdquo;. But once you use it deeply, it feels more like a long-term intelligent workstation.&lt;/p&gt;
&lt;p&gt;The key is not only how you phrase each instruction. It is whether you have given it a stable, clear, and accumulative environment.&lt;/p&gt;
&lt;p&gt;Once you build these four layers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CLAUDE.md&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Rules&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Memory&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Hooks&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;the quality of collaboration between you and the model usually improves very noticeably.&lt;/p&gt;
&lt;p&gt;Because you are no longer re-explaining from scratch who you are, how you work, and what must not happen every single time. You have actually turned those things into part of the environment.&lt;/p&gt;
&lt;p&gt;That is the key step in turning a strong model into a mature tool.&lt;/p&gt;
</description>
        </item>
        
    </channel>
</rss>
