Skip to content
bits
Work with coding agents · Lesson 6 of 7 · 8 min

Subagents and hooks

Verified claude-code 2.1.317

Tutor:

Two more tools for bigger jobs. A subagent is a helper Claude can hand a task to. A hook is a rule that runs every time, whether Claude remembers it or not.

Tutor:

Subagents. A subagent is a separate Claude with its own context window, its own instructions and, if you like, fewer tools. Claude hands it a job ("search the codebase for every place we format dates") and gets back a short answer instead of pages of search results. Use one to:

  • run independent jobs side by side;

  • keep noisy output, like a big search or a long log, out of your way;

  • review code it didn't write, with fresh eyes.

Each one costs extra tokens, and handing a job to a subagent doesn't make the answer smarter by itself. The fresh eyes are what help a review.

A subagent is a markdown file in .claude/agents/ (or ~/.claude/agents/ for every project). Claude uses it when a task matches its description, or when you ask for it by name.

Tutor:
Plain text
---
name: code-reviewer
description: Reviews a change for bugs and edge cases. Use after code is written, before committing.
tools: Read, Grep, Glob
---

You review code you didn't write. Look for wrong conditions, missed edge
cases and missing tests. List each problem with the line and a fix.
Don't rewrite the code.
Tutor:

Hooks. A rule in CLAUDE.md is advice: Claude usually follows it. Anthropic's guide is blunt that a prompt can't guarantee anything. When something must always (or never) happen, use a hook: a shell command Claude Code runs at a set moment, like after every file edit or before every command.

Tutor:

Hooks live in your settings file, .claude/settings.json. This one, from Claude Code's docs, formats every file Claude edits with Prettier:

Tutor:
JSON
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
          }
        ]
      }
    ]
  }
}

For a plain "never run this command", a deny rule in the same settings file does the job with no script: "permissions": { "deny": ["Bash(npm *)"] }. Use a hook when you need logic or a custom message.

A hook runs real commands on your machine with your permissions, so treat one you didn't write like any other script: read it first.

quick check+20 neurons
Tutor:

Your team must never run npm in a pnpm repo. Claude did it twice last week. What's the most reliable fix?

    Pick one option

    Pick one, then Check.

    Tutor:

    Your turn. Write a reviewer subagent in your own words: a description that says when to use it, read-only tools, and instructions. Then, after the file, hand it this function to review: function isOpen(hour) { return hour > 7 && hour < 18 }. Moon Café opens at 7:00 and closes at 18:00. Claude plays your subagent here.

    Your turn

    Goal Write a subagent with a clear trigger and read-only tools, and see it catch the opening-hour bug

    What good looks likeThe coach checks the reply
    • Has a description linenot checked yet
    • Says when to use itnot checked yet
    • Limits its toolsnot checked yet
    • Hands it the functionnot checked yet
    • Gives the opening hoursnot checked yet
    • Ran at 1.0not checked yet
    • Catches the 7 o'clock bugnot checked yet
    • The description says when the subagent should be usednot checked yet
    • Keeps the reviewer read-only, so it reports instead of editingnice to have
    • Points out that the café shows closed at 7 o'clock, when it has just openednot checked yet

    Run your prompt and the replies land here.

      ~0 tokens

      Turn on JavaScript to run prompts.