Skip to content

Agentic Workflows — Mastery4 / 8

Hooks — Make the Agent Obey Your Rules

A prompt asks the model to remember. A hook makes it happen — deterministically, every time, outside the model's control.

Hooks — Make the Agent Obey Your Rules

You can ask the agent to run the formatter after every edit. Or you can guarantee it. Hooks are shell commands Claude Code runs automatically at defined points in its loop — turning "please remember" into "always."

The events you'll actually use

  • PostToolUse — after a tool succeeds. Format or lint the file just edited.
  • PreToolUse — before a tool runs. Inspect and block dangerous calls.
  • UserPromptSubmit — inject context before the model sees your prompt.
  • SessionStart / Stop — load context at the start, notify at the end.

Wiring one up

Hooks live under a hooks key in settings.json. Each event maps to matchers (a regex over the tool name) and commands:

{
  "hooks": {
    "PostToolUse": [
      { "matcher": "Edit|Write",
        "hooks": [{ "type": "command", "command": "npx prettier --write \"$CLAUDE_FILE_PATHS\"" }] }
    ]
  }
}

Exit codes are the control channel

  • 0 — success; the agent continues.
  • 2blocking error; the action is prevented and stderr is fed back so the agent can adjust.
  • other non-zero — non-blocking error, logged.

Recipes that earn their keep

  • Auto-format on editPostToolUse + Edit|Write.
  • Block rm -rf / force-pushPreToolUse + Bash, exit 2 on match.
  • Secret scan before writePreToolUse + Write, block on detected keys.
  • Desktop ping when input is neededNotification.

Hooks enforce behavior. Next we package intent — reusable team workflows — as custom slash commands.

Share this article

#Hooks #DevTools #AgenticAI

LinkedInX / TwitterBlueskyThreadsRedditHacker NewsWhatsAppEmail

Series — Agentic Workflows — Mastery

  1. Part 01The Agentic Workflow MindsetMost developers treat AI like a smarter autocomplete. The ones who pull ahead treat it like a teammate that acts. Here is the mental model.
  2. Part 02MCP Servers 101 — Give Your Agent Real ToolsThe Model Context Protocol is how your agent stops guessing and starts querying your database, your issues, your browser. Here is the mental model and the first connection.
  3. Part 03Build Your First Custom MCP ServerOff-the-shelf servers cover GitHub and Postgres. The high-leverage one is the server only you can write — the bridge to your own system.
  4. Part 04Hooks — Make the Agent Obey Your Rulesyou are hereA prompt asks the model to remember. A hook makes it happen — deterministically, every time, outside the model's control.
  5. Part 05Custom Slash Commands as Team WorkflowsA custom slash command is a reusable prompt you commit to the repo — so the whole team runs the same high-quality instruction instead of re-typing it.
  6. Part 06Subagents — Delegating Work That ScalesOne giant context gets slow and vague. Subagents let the main agent hand focused work to specialists with their own context and tools — and run them in parallel.
  7. Part 07The Daily-Driver Setup — Settings, Permissions, Status LineThe difference between fighting the agent and flowing with it is twenty minutes of configuration you do once. Here is the setup.
  8. Part 08Multi-Tool Pipelines — Ticket to Reviewed BranchThe payoff: chain MCP, hooks, commands and subagents into one flow that takes a ticket to a reviewed branch — with you in the loop only where it counts.

Keep learning

PDF — lifetime

Hooks

Run your own shell commands at every step of the agent loop.

See the PDF →

Course

The Claude Mastery course

12 modules · 5 languages · certificate · 3-day free trial.

See plans →
LinkedInX / TwitterBlueskyThreads