By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
An engineer asked a support agent to summarise a call. The agent pasted the transcript into a shared ticket, including a customer's API key. The system prompt said never to share secrets, and the model followed it most of the time.
A rule that must hold every time needs a place in the loop where code runs. A prompt is a strong hint. It is not a control.
A hook is a function your host code registers at a fixed point in the agent loop. The model does not see it and cannot skip it. The Agent SDK exposes two points that matter for tools.
| Hook | Runs | Can | Use it for |
|---|---|---|---|
| PreToolUse | After the model requests a call, before the tool runs | Allow, deny, or rewrite the input | Policy, redaction, argument checks |
| PostToolUse | After the tool returns | Validate, log, or trigger a follow-up | Audit, schema checks, metrics, repair |
Both run in your process, with your credentials, under your tests. That is the whole advantage: a hook is deterministic in a way a prompt is not.
A deny should carry a reason the model can act on. Blocked invites a retry with the same input. Refunds above 100 need human approval; call escalate_to_human gives the model somewhere to go. The reason is part of the interface.
Write the reason as an instruction, not a verdict. Name the limit that was hit, the tool it should use next, and whether a retry with a smaller request is acceptable. A denial with a next step costs one turn; a bare denial costs a loop.
The snippet below is the whole policy from the diagram, with the model removed. It is short on purpose: a hook that is hard to read is a hook nobody reviews.
REFUND_LIMIT = 100.0
def pre_tool_use(tool_name, tool_input):
"""Decide before the tool runs: allow, deny, or rewrite the input."""
if tool_name != "process_refund":
return {"decision": "allow"}
amount = float(tool_input.get("amount", 0))
if amount > REFUND_LIMIT:
return {
"decision": "deny",
"reason": f"Refunds above {REFUND_LIMIT:.0f} need human approval.",
}
return {"decision": "allow"}
def post_tool_use(tool_name, tool_input, result):
"""Validate and record after the tool returns."""
return {"logged": True, "tool": tool_name, "ok": not result.get("is_error")}
print(pre_tool_use("process_refund", {"amount": 42}))
print(pre_tool_use("process_refund", {"amount": 420}))
print(post_tool_use("process_refund", {"amount": 42}, {"is_error": False}))Notice what the test proves. For 42 the decision is allow. For 420 it is deny, with a reason. No model was involved, so the result does not vary between runs.
You pass hooks with the agent options and match them to tool names. They sit alongside allowedTools and the permission mode, and the three answer different questions. allowedTools says what may be called at all. The permission mode says how much the agent may do without asking. A hook says what is acceptable for this specific call.
Keep hooks small and boring. A hook that calls a model is no longer deterministic. A hook that fails open is worse than no hook, because it looks like protection.
Pick a tool in a system you own. Write the policy before you write the code.
PostToolUse check that catches a success-shaped but invalid result, such as an empty list returned as ok.Key takeaways
A hook is code at a fixed point in the loop, so the model cannot skip it.
PreToolUse allows, denies or rewrites a call before execution; PostToolUse validates, logs or follows up after it.
Give every deny a reason and a next step so the model recovers instead of retrying.
Hooks are pure enough to unit-test, which is what makes them a control rather than a hope.
A hook cannot undo a side effect, and a hook that fails open is not protection.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.