Understanding Claude Hooks
Your AI’s New Favorite Habit
I am currently in the process of getting certified by Anthropic. You can do it, too. It’s easy, online, and both the courses and the quizzes are free. The quizzes are actually easy, and you can retry till you pass. Here is the link:
https://anthropic.skilljar.com
In my first course of Claude Code 101, I learned about Claude hooks. This concept is new to me, and I have never used it before. Unfortunately, the course explains it so briefly as a submodule. Therefore, I decided to dig into it, learn more, and share my findings with you in a couple of articles.
If you’ve ever worked with Claude Code, you’ve probably noticed there are moments where you think: “I wish Claude would automatically format my code after every edit,” or “I want to prevent certain operations from running.”
That’s where hooks come in.
Hooks aren’t suggestions or probabilistic tendencies—they’re deterministic commands. Configure them in the settings.json file
They give you guaranteed control over Claude Code’s behavior at specific lifecycle events. Think of them as automated guardrails that always run, every single time, configured right in your settings.json file.
Let me explain what they are, why they matter, how they compare to skills and subagents, and how they’ll change the way you work with Claude Code.
What Are Claude Hooks, Really?
Here’s the mental model: Imagine Claude Code as a factory assembly line. You can set up checkpoints at specific stages where certain actions always happen, without fail. Those checkpoints are hooks.
For example:
After-edit hook: “After I finish editing code, automatically format it using Prettier.”
Before-bash hook: “Before running any Bash command, check that it’s not dangerous.”
After-write hook: “After writing a Python file, run the linter automatically.”
These aren’t suggestions. They always run. They’re deterministic.
Hooks vs Skills vs Subagents vs Regeneration: What’s Actually Different?
Now, if you’ve been using Claude Code, you might be wondering: “Isn’t this what Skills do? What about Subagents? And what’s the difference between all of these and writing specs that Claude regenerates from md to Python?”
Great question. They’re all related, but fundamentally different. Let me break this down clearly because understanding these distinctions will change how you architect your Claude Code workflows.
Hooks are deterministic event handlers configured in settings.json. They always run at specific moments (after edits, before saves, on permission requests). They execute shell commands or scripts you define, guaranteed, every time, no variation.
Skills are reusable packages of instructions, context, and code that give Claude new capabilities. When you create a Skill, you’re bundling your expertise into a directory structure (SKILL.md + supporting files) that Claude can discover and use when relevant. Skills are like reference materials in Claude’s toolkit.
Subagents are specialized AI agents you delegate tasks to. They’re defined in Markdown files with YAML front matter and have their own context windows, custom prompts, and tool access. When Claude encounters a task that matches a subagent’s description, it spawns that subagent to handle the work independently.
Regeneration is something completely different. It’s the process where you write a spec in .md format that acts as the source of truth for an agent’s behavior, and Claude Code automatically regenerates the implementation .py file from that spec. You’re not writing implementation code directly; you’re writing specifications, and Claude regenerates the code to match. This procedure saves a lot of tokens and is always deterministic.
Here’s the critical distinction:
Hooks and regeneration are both deterministic, but they work at different times. Hooks execute at runtime (when you’re working). Regeneration happens at dev-time (when you change a spec).
Let me show you:
Why This Distinction Matters: Two Types of Determinism
Here’s the insight that changes how you build systems with Claude Code:
Hooks give you deterministic actions at specific moments. When you need something to happen during your work session, hooks force it to happen. Always.
Regeneration gives you a deterministic implementation from a spec. When you want Claude to build an agent in a specific way, you write the spec once, and Claude regenerates the implementation every time the spec changes. The spec is the source of truth.
They solve different problems:
Hooks solve: “How do I guarantee formatting, linting, and security checks happen automatically?”
Regeneration solves: “How do I ensure my agent implementation always matches my intended behavior, no matter what?”
And here’s the power move: You can use them together.
Regeneration: Spec-Driven Implementation
Regeneration is the practice of writing a spec (source of truth) and having Claude regenerate the implementation to match.
You define an agent spec:
# Quiz Generator Agent Spec
## Purpose
Generate quiz questions from course content
## Inputs
- course_content: string
- difficulty: "beginner" | "intermediate" | "advanced"
## Behavior
1. Parse the course content
2. Extract key concepts
3. Generate 5-10 questions
4. Validate questions for clarity
5. Return as JSON
## Non-negotiables
- Always include answer explanations
- Never repeat questions within same quiz
- Always validate question grammar
Claude Code reads this spec and regenerates quiz_generator.py to implement exactly this behavior. Your spec is the source of truth.
When you need to change the agent’s behavior, you edit the spec, and Claude regenerates the implementation. You never edit the .py file directly—the spec controls everything.
This is powerful because:
The spec is readable (you can hand it to anyone)
The implementation always matches the spec (no drift)
Changes are traceable (you can see what spec changed)
New team members understand intent (read the spec, not the code)
Real Example: Hooks vs Regeneration vs Skills
Let’s say you’re building an educational platform with multiple agents. Here’s how you’d use each strategically:
Your Hooks (enforce standards during development):
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "black \"$CLAUDE_TOOL_INPUT_FILE_PATH\""
}
]
}
],
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "python .claude/hooks/check-dangerous.py"
}
]
}
]
}
}
Every Python file gets formatted with Black. Dangerous Bash commands are blocked. This happens automatically during your work, every time.
Your Regeneration Spec (define agent behavior once):
# Flashcard Generator Spec
## Purpose
Convert lecture notes into Flashcard pairs
## Input Processing
1. Accept markdown lecture notes
2. Parse headers and bullet points
3. Identify key concepts
## Flashcard Generation
- Create front (question) from concept
- Create back (answer) from explanation
- Ensure one concept per card
- Validate all cards are independent
## Output Format
Return JSON array with:
{
"front": "question text",
"back": "answer text",
"source": "lecture section"
}
## Constraints
- Never exceed 100 cards per lecture
- Always include source reference
- Validate JSON before returning
When you run Claude Code with this spec, it regenerates flashcard_generator.py to implement exactly this behavior. The spec is the source of truth—if the implementation doesn’t match, regenerate.
Your Skill (provide context and examples):
# Educational Content Skill
When working with educational agents:
## Best Practices for Flashcards:
- Keep front side concise (one question)
- Keep back side complete but brief
- Test with spaced repetition research
- Include source references
## Best Practices for Quizzes:
- Mix difficulty levels
- Include explanations for all answers
- Avoid trick questions
- Validate grammar and clarity
## Examples:
[example flashcard sets]
[example quiz questions]
Claude discovers this skill when working on educational content. Claude uses it as a context for better decisions.
Your Subagent (validate in parallel):
---
name: Content Validator
description: Validates educational content quality
tools: [Read, Grep, Glob]
---
When delegated content validation:
1. Check for factual accuracy
2. Verify grammar and clarity
3. Ensure learning outcomes are met
4. Check alignment with difficulty level
5. Return structured report
Claude delegates content validation to this subagent while continuing other work.
Together:
Hooks enforce code quality automatically (formatting, security)
Regeneration ensures agent behavior always matches intent (spec-driven)
Skills provide educational best practices (Claude learns from context)
Subagents validate content in parallel (keeps the main context clean)
When to Use Each
Use Hooks if:
You want automatic enforcement at specific moments
You need formatting, linting, or security validation to happen automatically
You want to prevent dangerous operations
You want to guarantee standards across all your work
Your philosophy: “Some things are non-negotiable”
Use Regeneration if:
You’re building agents with specific, defined behavior
You want the spec (not the code) to be your source of truth
You need behavior to remain consistent with intent
You want new team members to understand the agent's purpose quickly
Your philosophy: “Code should be generated from specification”
Use Skills if:
Claude needs domain-specific expertise or examples
You’re bundling instructions with supporting context
You want Claude to discover and use them when relevant
You need to share best practices and organizational knowledge
Your philosophy: “Give Claude good reference material”
Use Subagents if:
You have parallel work that would clutter your main context
You need specialized agents for specific task types
You want to isolate context between different work streams
Your philosophy: “Delegate specialized work to focused agents”
The Bottom Line
Hooks are deterministic runtime automation. Once configured, they always run at key moments, executing actual shell commands.
Regeneration is deterministic dev-time code generation. Your spec is the source of truth, and the implementation is regenerated to match.
Skills are reusable expertise packages that Claude discovers and uses.
Subagents are specialized agents you delegate work to.
Summary
Enforce code formatting automatically? Hooks.
Prevent dangerous commands? Hooks.
Ensure an agent always implements its spec? Regeneration.
Provide educational best practices? Skills.
Validate in parallel? Subagents.
This is what makes modern Claude Code development powerful. You have multiple layers of control: runtime enforcement (hooks), dev-time specification (regeneration), contextual guidance (skills), and parallel work (subagents).
In the next article, we’ll have a step-by-step tutorial with screenshots for a working hook that automatically formats every Python file you write or edit.




