Hooks & Agents
Automating Workflows and Spawning Specialists
Video Lesson Coming Soon
A video walkthrough for this module is in production. For now, dive into the written content below.
What You'll Learn
- ✓ What hooks are and when they fire
- ✓ Hook types and examples
- ✓ Configuring hooks in settings.json
- ✓ Subagents: parallel workers
- ✓ Custom agents in .claude/agents/
- ✓ Skills and reusable workflows
- ✓ Worktrees for parallel development
In this module 8 sections
What Are Hooks?
Hooks are scripts that run automatically at specific moments in Claude Code's lifecycle. They are not AI-powered — they are plain shell commands or HTTP requests that fire when certain events happen.
For example: - Before Claude Code runs a shell command → validate it is safe - After Claude Code edits a file → run the linter automatically - When a session starts → load project-specific context - When Claude Code finishes responding → send a notification
The key difference between hooks and CLAUDE.md instructions: CLAUDE.md is advisory — Claude Code tries to follow it but might not always. Hooks are deterministic — they execute every time, no exceptions.
Hook Events
| Event | Fires When | Common Use |
|---|---|---|
| PreToolUse | Before a tool runs | Validate or block dangerous commands |
| PostToolUse | After a tool completes | Run linters, formatters, or loggers |
| SessionStart | When a session begins | Load environment, check prerequisites |
| Stop | When Claude Code finishes | Send notifications, run final checks |
| UserPromptSubmit | When you send a message | Transform or validate your input |
Configuring Hooks
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "npx prettier --write $CLAUDE_FILE_PATH"
}
]
}
],
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "echo \"Running: $CLAUDE_TOOL_INPUT\" >> ~/.claude/audit.log"
}
]
}
]
}
} Subagents: Parallel Workers
When you give Claude Code a large task, it can spawn subagents — independent workers that handle subtasks in parallel.
Imagine you ask: "Refactor all API endpoints to use the new validation library." Instead of processing each endpoint one at a time, Claude Code might spawn three subagents: one for user endpoints, one for product endpoints, and one for payment endpoints. Each works independently and reports back.
Subagents have their own context, their own tool access, and their own permissions. The main Claude Code session acts as a coordinator — it delegates work, collects results, and assembles the final output.
Custom Agents
You can define custom agents with specific roles and instructions. These live in .claude/agents/ as markdown files with special frontmatter.
Custom Agents
---
name: Code Reviewer
description: Reviews code for security, performance, and best practices
tools:
- Read
- Grep
- Glob
---
You are a code reviewer. When given code to review:
1. Check for security vulnerabilities (SQL injection, XSS, etc.)
2. Look for performance issues (N+1 queries, unnecessary re-renders)
3. Verify error handling is comprehensive
4. Check that tests exist for new functionality
5. Ensure naming conventions match the project style
Be specific about issues. Reference line numbers.
Suggest fixes, not just problems. Skills and Reusable Workflows
Skills are reusable workflows saved as markdown files in .claude/skills/. Unlike agents (which are specialists you delegate to), skills are recipes — step-by-step procedures that Claude Code follows.
For example, a skill might define your team's PR workflow: run tests, check linting, generate a changelog, create the PR with a specific description template, and assign reviewers. You invoke it by name, and Claude Code follows the steps every time.
Worktrees for Parallel Development
Claude Code can create git worktrees — separate working directories on separate branches. This means you can run multiple Claude Code sessions simultaneously, each on a different branch, each working on a different task, without any conflicts.
Start a worktree session with claude -w feature-name. Claude Code creates a clean copy of your repo on a new branch and works there independently.
Key Takeaways
Hooks run scripts at lifecycle events — they are deterministic, unlike CLAUDE.md instructions which are advisory
Common hooks: auto-lint after edits (PostToolUse), validate commands (PreToolUse), send notifications (Stop)
Subagents enable parallel work by spawning independent workers for subtasks in large operations
Custom agents in .claude/agents/ define specialists with specific roles, tools, and instructions