Learn · Agentic architecture & orchestration · CCAR-F task 1.5
Hooks for tool calls
Hooks run your code before and after every tool call: block what's forbidden, normalise what comes back, before Claude reasons.
Key ideas
- Hooks run your code deterministically at fixed points in the agent loop.
- PreToolUse runs before a tool and can allow, deny, ask or modify the input.
- PostToolUse runs after a tool and can replace the output (updatedToolOutput) or add context.
- Normalise data (dates, status codes, masking) before the model reasons over it.
- Use PreToolUse to enforce policy, such as denying refunds over a limit with a reason.
- Claude Code shell hooks use the same events, configured in settings JSON; exit code 2 blocks.
Worked example: Three formats, one hook
orders returns created as 1768435200 and status 2, shipping returns an ISO string and "SHIPPED", billing returns "15/01/2026" and "shipped". One PostToolUse hook on mcp__shop__.* maps all of them to 2026-01-15 and "shipped" before Claude sees any result. Check which input field carries the tool's result in your SDK version's reference.
Exam traps
- Answering inconsistent tool formats with a prompt line ("interpret dates carefully") instead of a PostToolUse hook.
- Putting a hard spending limit in the system prompt rather than a PreToolUse deny.
- Thinking PostToolUse can stop a tool from running. It fires after; blocking is PreToolUse.
- Denying without a reason, so the model doesn't know to escalate.
Official docs
Practise CCAR-F questions · Next lesson: Task decomposition