Spawn autonomous sub-agents that run in parallel, and wire lifecycle hooks to trigger shell commands at every step of the AI's workflow.
Agents & Hooks are two distinct but complementary powers in Claude Code. Agents let you spawn isolated sub-Claude instances — each with its own context, tools, and task — running in parallel or sequentially. Hooks are shell commands that fire automatically on lifecycle events like tool calls, file writes, or task completion.
Together they let you build complex multi-step AI pipelines: one agent researches, another writes, a third reviews — while hooks validate outputs, format code, and trigger deployments along the way.
| Event | When it fires | Common use |
|---|---|---|
| PreToolCall | Before Claude calls any tool | Validate permissions, log intent |
| PostToolCall | After any tool returns | Run linter, format code, check output |
| PreFileWrite | Before writing a file | Backup, safety check path |
| PostFileWrite | After a file is written | Run tests, hot-reload dev server |
| TaskComplete | When Claude finishes a task | Deploy, notify Slack, commit to git |
| UserPromptSubmit | On every user message | Inject context, validate input |
Spawn three agents simultaneously: one checks security vulnerabilities, one reviews performance, one validates tests. Orchestrator merges all findings into a single report.
Use a PostFileWrite hook to automatically run Prettier on any JS/TS file Claude writes, ensuring consistent formatting without manual intervention.
Agent writes a feature, PostFileWrite hook runs tests automatically, hook output is fed back so Claude sees red/green and iterates until all tests pass — no manual test runs needed.
Delegate large codebase exploration to a sub-agent with read-only tools. It scans thousands of files and returns only the relevant 10 — keeping the main context clean.
Wire a TaskComplete hook to run your deploy script automatically when Claude finishes a task — staging deploy triggered without lifting a finger.