Agent SDK hooks for interception and normalization
What this covers
- Use
PostToolUseto normalize tool output before the model sees it - Intercept outgoing tool calls to block policy violations
- Recognize when a guarantee is required and prompting cannot provide one
- Route a blocked action somewhere rather than leaving the agent with a dead end
- Reserve hooks for transforms with one right answer, not for judgments the data cannot settle
Key terms
- Hook
- Code that runs automatically before or after a defined event.
- Model Context Protocol (MCP)
- A standard way for AI applications to connect to tools and data sources.
- Tool call
- A structured request from a model to run a tool with specific input.
Use hooks when a rule must be enforced by code instead of left to model judgment.
An epoch timestamp represents a date and time as a number counted from January 1, 1970, in Coordinated Universal Time (UTC). In this example, the value counts seconds. The hook converts it to the ISO 8601 date format before the model receives it:
// Runs in a PostToolUse hook, before the result reaches the model.
function normalizeDeliveryDate(result) {
if (typeof result.deliveredAt === 'number') {
result.deliveredAt = new Date(result.deliveredAt * 1000).toISOString();
}
return result;
}# Runs in a PostToolUse hook, before the result reaches the model.
def normalize_delivery_date(result):
if isinstance(result.get("deliveredAt"), (int, float)):
result["deliveredAt"] = datetime.fromtimestamp(
result["deliveredAt"], tz=timezone.utc
).isoformat()
return resultThe model receives the converted date, so it doesn't need to interpret the epoch value itself.
A hook is ordinary code that runs at the tool-call boundary. It is separate from the prompt and runs every time the tool is called. That repeated code, not a prompt instruction, provides the guarantee.
PostToolUse: normalize before the model reads it
Real backends disagree with each other. One tool returns Unix timestamps, another ISO 8601, a third numeric status codes with a lookup table nobody has read since 2019.
You can ask the model to reconcile those formats, but it may occasionally interpret one incorrectly. Those errors can be difficult to notice.
A PostToolUse hook transforms results into one consistent format before they reach the model. The model then receives one predictable format instead of having to convert each result itself.
A hook can only apply a rule you can write down. Epoch seconds map to one calendar date, so the transform is safe. A note reading "should arrive early next week" doesn't, and a hook that guesses a date replaces visible uncertainty with a confident wrong value. The limit is whether the rule has one right answer, not whether the data arrived as a structured field.
Before cooking, you can convert every measurement in the recipe to grams, or convert each one in your head as you go. Converting first removes a whole class of mistake. It also only works for measurements that convert; a pinch doesn't.
Interception: block before it happens
Hooks also sit on the outgoing side. A refund above a policy threshold can be blocked at the tool-call boundary — and, importantly, redirected. Blocking alone leaves the agent stuck; blocking and routing to human escalation leaves the customer served.
The decision rule
Ask what happens when the rule is broken. If the answer involves money, legal exposure, or safety, you need a deterministic guarantee, and a hook is how you get one. Prompt instructions are for guidance, tone, and preference — the things where an occasional miss is survivable.
PostToolUse hookstool call interceptionMCP tools
Field note — common misconceptions
- MythThat the model can be trusted to reconcile inconsistent formats reliably
- ActuallyIt mostly succeeds, and the misses are quiet; a
PostToolUsehook removes the error class. - MythThat a blocked action should simply fail rather than route somewhere
- ActuallyBlocking and routing to human escalation is what leaves the customer served.
- MythThat hooks are an optimization rather than a correctness mechanism
- ActuallyWhere a violation involves money, legal exposure, or safety, a hook is how you get a guarantee.
Guided review
Review this lesson as a study deck
Review the lesson's main ideas in five guided slides, then test yourself with three flashcards.
Open Task 1.5 study deckCross-domain reasoning
Connect this idea
Tool access sets an authority boundary
Hooks, tool descriptions, access limits, MCP configuration, and escalation rules decide what an agent may do and when another person or system must decide.
Applied practice
Practice this lesson in a lab
Use a related lab to create a decision, implementation or diagram, evidence record, and review.
- Lab 3 · Deterministic prerequisite and human handoff
Enforce safety policy outside the prompt and produce a handoff another person can act on.