AGENT0S
HomeLibraryAgentic
FeedbackLearn AI
LIVE
Agent0s · AI Intelligence Library
Share FeedbackUpdated daily · 7am PST
Library/technique
techniqueadvancedClaude Code

Claude Code Hooks Mastery

This is a comprehensive resource for developers to gain precise control over the Claude AI coding assistant's behavior using hooks. It enables the creation of custom automations, enforcement of quality checks, and integration with other services directly into the AI's workflow.

AI SETUP PROMPT

Paste into Claude Code — it will scan your project and set everything up

# Apply Technique: Claude Code Hooks Mastery

## What This Is
This is a comprehensive resource for developers to gain precise control over the Claude AI coding assistant's behavior using hooks. It enables the creation of custom automations, enforcement of quality checks, and integration with other services directly into the AI's workflow.

Source: https://github.com/disler/claude-code-hooks-mastery

## Before You Start

Scan my workspace and analyze:
- The project language, framework, and directory structure
- Existing AI provider config (check .env, .env.local, config files for API keys — OpenRouter, OpenAI, Anthropic, Google AI, etc.)
- Whether this repository or a similar tool is already cloned or installed

Then ask me before proceeding:
1. Which AI provider/API should this use? (Use whatever I already have configured, or ask me to set one up — options include direct provider APIs or a unified service like OpenRouter)
2. Where in my project should this be integrated?
3. Are there any customizations I need (model preferences, naming conventions, constraints)?

## Fetch the Source

Clone or inspect the repository to understand what needs to be installed:
```bash
gh repo clone disler/claude-code-hooks-mastery
```
Review the README, directory structure, and any install instructions before proceeding.

## What to Implement

This is an **AI Technique** — a pattern or methodology for working with AI models.

- Explain how this technique applies to my current project and what benefit it provides
- Implement it in a way that fits my existing codebase — suggest concrete files to modify or create
- If it requires specific model capabilities (structured output, function calling, etc.), verify my current provider supports them
- Show me a working example I can test immediately

## Additional Context

- Clone the repository `https://github.com/disler/claude-code-hooks-mastery.git` into a local directory for the user to explore and use as a reference.
- Verify if `uv` (from Astral) is installed in the user's environment, as it is a required dependency. If not present, instruct the user on how to install it or offer to run the installation command.
- Scan the cloned repository's hook examples. Prompt the user to select one (e.g., `UserPromptSubmit` hook for prompt validation) and assist them in configuring it within their `claude-code.json` settings file, linking it to the corresponding Python script from the repository.

## Reference Implementation

```
```mermaid
flowchart TB
    subgraph SESSION["🟢 Session Lifecycle"]
        direction TB
        SETUP[["🔧 Setup<br/>(init/maintenance)"]]
        START[["▶️ SessionStart<br/>(startup/resume/clear)"]]
        END[["⏹️ SessionEnd<br/>(exit/sigint/error)"]]
    end

    subgraph MAIN["🔄 Main Conversation Loop"]
        direction TB
        PROMPT[["📝 UserPromptSubmit"]]
        CLAUDE["Claude Processes"]

        subgraph TOOLS["🛠️ Tool Execution"]
            direction TB
            PRE[["🔒 PreToolUse"]]
            PERM[["❓ PermissionRequest"]]
            EXEC["Tool Executes"]
            POST[["✅ PostToolUse"]]
            FAIL[["❌ PostToolUseFailure"]]
        end

        subgraph SUBAGENT["🤖 Subagent Lifecycle"]
            direction TB
            SSTART[["🚀 SubagentStart"]]
            SWORK["Subagent Works"]
            SSTOP[["🏁 SubagentStop"]]
        end

        NOTIFY[["🔔 Notification<br/>(Async)"]]
        STOP[["🛑 Stop"]]
    end

    subgraph COMPACT["🗜️ Maintenance"]
        PRECOMPACT[["📦 PreCompact"]]
    end

    SETUP --> START
    START --> PROMPT
    PROMPT --> CLAUDE
    CLAUDE --> PRE
    PRE --> PERM
    PERM --> EXEC
    EXEC --> POST
    EXEC -.-> FAIL
    CLAUDE -.-> SSTART
    SSTART --> SWORK
    SWORK --> SSTOP
    POST --> CLAUDE
    CLAUDE --> STOP
    CLAUDE -.-> NOTIFY
    STOP --> PROMPT
    STOP -.-> END
    PROMPT -.-> PRECOMPACT
    PRECOMPACT -.-> PROMPT
```
```

## Guidelines

- Adapt everything to my existing project — do not assume a specific stack or directory layout
- Use whichever AI provider I already have configured; if I need a new one, tell me what to sign up for and I'll give you the key
- Check my .env files for existing API keys (OpenRouter, OpenAI, Anthropic, Google AI) before asking me to add one
- Review any fetched code for safety before installing or executing it
- After setup, run a quick verification and show me a summary of exactly what was installed, where, and how to use it
4,471 charactersCompatible with Claude Code & Codex CLI
MANUAL SETUP STEPS
  1. 01Clone the repository `https://github.com/disler/claude-code-hooks-mastery.git` into a local directory for the user to explore and use as a reference.
  2. 02Verify if `uv` (from Astral) is installed in the user's environment, as it is a required dependency. If not present, instruct the user on how to install it or offer to run the installation command.
  3. 03Scan the cloned repository's hook examples. Prompt the user to select one (e.g., `UserPromptSubmit` hook for prompt validation) and assist them in configuring it within their `claude-code.json` settings file, linking it to the corresponding Python script from the repository.

CODE INTELLIGENCE

bash
```mermaid
flowchart TB
    subgraph SESSION["🟢 Session Lifecycle"]
        direction TB
        SETUP[["🔧 Setup<br/>(init/maintenance)"]]
        START[["▶️ SessionStart<br/>(startup/resume/clear)"]]
        END[["⏹️ SessionEnd<br/>(exit/sigint/error)"]]
    end

    subgraph MAIN["🔄 Main Conversation Loop"]
        direction TB
        PROMPT[["📝 UserPromptSubmit"]]
        CLAUDE["Claude Processes"]

        subgraph TOOLS["🛠️ Tool Execution"]
            direction TB
            PRE[["🔒 PreToolUse"]]
            PERM[["❓ PermissionRequest"]]
            EXEC["Tool Executes"]
            POST[["✅ PostToolUse"]]
            FAIL[["❌ PostToolUseFailure"]]
        end

        subgraph SUBAGENT["🤖 Subagent Lifecycle"]
            direction TB
            SSTART[["🚀 SubagentStart"]]
            SWORK["Subagent Works"]
            SSTOP[["🏁 SubagentStop"]]
        end

        NOTIFY[["🔔 Notification<br/>(Async)"]]
        STOP[["🛑 Stop"]]
    end

    subgraph COMPACT["🗜️ Maintenance"]
        PRECOMPACT[["📦 PreCompact"]]
    end

    SETUP --> START
    START --> PROMPT
    PROMPT --> CLAUDE
    CLAUDE --> PRE
    PRE --> PERM
    PERM --> EXEC
    EXEC --> POST
    EXEC -.-> FAIL
    CLAUDE -.-> SSTART
    SSTART --> SWORK
    SWORK --> SSTOP
    POST --> CLAUDE
    CLAUDE --> STOP
    CLAUDE -.-> NOTIFY
    STOP --> PROMPT
    STOP -.-> END
    PROMPT -.-> PRECOMPACT
    PRECOMPACT -.-> PROMPT
```

FIELD OPERATIONS

Automated Code Reviewer Hook

Use the `UserPromptSubmit` hook to intercept code review requests. The hook script can then use a separate, specialized AI sub-agent to perform a detailed static analysis, check for common bugs, and enforce project style guides before the main agent begins its review.

Interactive Debugging Sub-Agent

Create a hook that triggers on a `SubagentStart` event when the user's prompt contains keywords like 'debug' or 'fix this error'. This hook launches a specialized debugging sub-agent that can interactively ask the user for more information, run tests, and inspect variables, creating a dedicated debugging session within the main chat.

STRATEGIC APPLICATIONS

  • →Implement a `PreToolUse` hook to function as a compliance and security gatekeeper. The hook intercepts any command that accesses sensitive files or external APIs, cross-references the action against a company policy database, and automatically blocks non-compliant actions while logging the attempt for security audits.
  • →In a customer support setting, use the `UserPromptSubmit` hook to analyze an incoming query and the user's customer ID. The hook can then route the query to different sub-agents or apply different system prompts based on the user's subscription tier (e.g., premium users are handled by a more powerful model like Opus, while free users are handled by Haiku).

TAGS

#hooks#claude-code#automation#python#uv#agent-orchestration#sub-agents#mcp
Source: GITHUB · Quality score: 9/10
VIEW SOURCE