Advanced

Subagents and Hooks Explained

Subagents give you specialists with their own context. Hooks give you automation on lifecycle events. Together they turn a single agent into a small team that runs itself.

Setuproll Team9 min read2026-06-18

Once your basic build works, two features take it from useful to genuinely powerful: subagents and hooks. They solve different problems. Subagents are about delegation. Hooks are about automation. You will want both.

Subagents: specialists with their own context

A subagent is a Markdown file with YAML frontmatter that defines a focused specialist: its own system prompt, its own tool allowlist, and crucially its own context window. The official subagents docs cover the full schema. The main agent delegates a job, the subagent does it in isolation, and only the result comes back. That keeps the main context clean.

.claude/agents/reviewer.md
---
name: reviewer
description: Reviews diffs for bugs and security issues before commit.
tools: Read, Grep, Bash
---

You are a senior code reviewer. Review only the staged diff.
Flag correctness bugs, missing error handling and unsafe input.
Be specific: cite the file and line. Do not rewrite the code.
Why isolation matters
A subagent that fills its own context with a giant search does not pollute your main session. You get the answer without paying for the noise on every later turn.

Hooks: automation on lifecycle events

Hooks are shell commands that fire on specific events: before a tool runs, after a file is edited, when a session starts. They are deterministic, so you use them for the things you never want the model to forget, like running the formatter or blocking an edit to a protected file.

.claude/settings.json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "npx prettier --write $CLAUDE_FILE" }
        ]
      }
    ]
  }
}
zsh - hooks firing
$claude "add a rate limiter to the login route"
● Edit: src/routes/auth.ts
↳ hook PostToolUse: prettier --write src/routes/auth.ts
✓ formatted, 1 file changed
$
Hooks run real shell commands
A hook executes on your machine with your permissions. Only wire up commands you trust, and be careful copying hook configs from the internet without reading them.

Putting them together

  1. Delegate research and review to subagents so the main context stays focused.
  2. Use hooks to enforce formatting, tests and safety automatically.
  3. Bundle the whole thing into a plugin marketplace if your team shares one stack.
Create custom subagents - Claude Code DocsOfficial reference for subagent frontmatter, tool allowlists and isolated context.code.claude.comdisler/claude-code-hooks-masteryReference repo covering every hook lifecycle event, with TTS and security examples.github.com4khesreallyhim/awesome-claude-codeCurated skills, hooks, slash commands, agents and plugins for Claude Code.github.com23k
How to Build Claude Subagents Better Than 99% of People22:40
How to Build Claude Subagents Better Than 99% of People· IndyDevDan

Start with one subagent for code review and one hook for formatting. That alone will make every session calmer. Add more only when you feel the specific pain they solve.

0 Comments

Sign in to post

Loading discussion...