Lv.1 0 XP

Agent SDK Hooks — Deep Dive Into Pre and Post Tool Use

Core 9 min +40 XP
💡
THE ANALOGY

Middleware in a web framework. Before a request hits your route handler, middleware can authenticate, rate-limit, log, and transform the request. After the handler responds, middleware can compress, cache, and add headers. Hooks are middleware for tool calls.

⚠️ EXAM TRAP — The Wrong Answer People Choose

Thinking PostToolUse hooks run after Claude processes the result. They run BEFORE Claude processes the result — they intercept the raw tool output and transform it before Claude ever sees it.

KEY POINTS
1 PreToolUse hooks fire synchronously before the tool executes — they can allow, block, or modify the tool's input parameters.
2 PostToolUse hooks fire after the tool executes but BEFORE Claude receives the result — they transform raw tool output into a format better suited for Claude's reasoning.
3 Hook chains execute in registration order — multiple hooks can be registered and all execute in sequence.
4 A blocked PreToolUse hook returns an error as the tool_result — Claude sees the error message and can decide how to proceed.
5 PostToolUse hooks are the correct mechanism for data normalization across heterogeneous tool sources.