28.5 Hooks as deterministic enforcement
- Approved by
- Panaversity ·
In everyday life. An office building has a front desk. The receptionist writes every visitor's name in the visitor book, and turns away anyone who has no appointment. The receptionist does this for every visitor, and no visitor can talk their way past the book. But the building has other doors: the loading dock, the parking garage and the fire stairs. So the cash office on the third floor has its own lock, and it opens only for a signed slip from the manager, whichever door the visitor came in by. In this section, the front desk is a hook, the visitor book is the evidence line, the other doors are other routes to the money, and the cash office's lock is the payment tool's own check.
What you will learn. You practice this in Lab 28, Task 3, and prove it in Task 4.
- What makes a hook deterministic, when an instruction is not?
- What does the payment hook do, and in what order?
- Which route does the hook guard, and which routes does it miss?
- What does the hook cost?
- How do you correct a hook configuration that looks right?
Why it matters. The exam guide asks for "hooks as deterministic enforcement so that destructive actions cannot execute even under adversarial instruction".1 A hook does that on its own route.
At work. This section builds the hook that would deny the 9:00 call in the overview, and shows the routes on which it never runs.
What makes a hook deterministic, when an instruction is not?
A hook runs every time its event fires, and the model has no say in it. Deterministic here means two things: it always runs, and the same call always meets the same check. Anthropic's guide says hooks give "deterministic control: certain actions always happen rather than relying on the LLM to choose to run them."2
An instruction is text the model reads, in a system prompt, a skill or a CLAUDE.md file. Claude Code's documentation says these files "aren't enforced configuration. To block an action regardless of what Claude decides, use a PreToolUse hook instead."3 So a written rule guides, and only a setting, a hook or a permission enforces it. A fooled model can ignore "never pay without an approval". It cannot skip a hook, because the hook is not in its context at all. That puts the hook in the second tier.
The exam also asks for "hook configurations so that required actions execute deterministically rather than at the model’s discretion".1 So a hook can make an action always happen, and it can stop one. The payment hook does both.
What does the payment hook do, and in what order?
It writes an evidence line first, then it asks for the approval and denies the call if none matches. It is one callback, a function of yours that the SDK calls, registered in the orchestrator's own code on the event before every tool call, PreToolUse:
// src/agents/month-end-orchestrator.ts, a sketch. The kit names the server.
const paymentGuard: HookCallback = async (input) => {
const call = input as PreToolUseHookInput
await writeEvidenceLine(call) // 1. always, for every proposed payment
const row = await getApproval(call) // 2. the connector's get_approval, on the run's token
if (!row) {
return {
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: `No approval on record for ${invoiceOf(call)}`,
},
}
}
return {} // 3. no decision: the other checks still decide
}
const options = {
hooks: {
PreToolUse: [
{ matcher: "mcp__ap__schedule_payment", hooks: [paymentGuard], onFailure: "block" },
],
},
}Read it in order.
- The evidence line comes first. It is one line in
evidence/payments.jsonl(proposed). The line records which tool, which invoice, which amount, which run, and when. It must exist for every call, allowed or denied. That is the exam's "required action". The portal's approval page lists the line as a proposal only when the run's token carriesap:pay(28.4), the scope that means "may propose a payment". - Then the hook asks. It calls
get_approvalwith the run's own token. The model cannot call that tool, because the orchestrator denies it by name (28.4). No matching row, and the hook denies, with a reason the model reads. - A match is no decision. When a row matches, the hook returns
{}, which decides nothing. The call goes on to the rest of the checks in 22.5's order, and then to the connector, which checks it again. This is what "the hook lets the call through" means everywhere in this chapter. The hook never returns "allow", because it is not the last guard.
Why one callback and not two hooks? Hooks that match one event "run in parallel", and "completion order is non-deterministic".4 Two hooks could deny before the evidence line was written. Inside one callback, the order is the order of the lines.
The matcher is the pattern that says which tools a hook runs for. A matcher made only of letters, digits, _, -, spaces, commas and | is read as an exact name, or a list of exact names. So schedule_payment alone is compared as an exact string and matches no tool, because the tool's full name carries the connector's prefix. A matcher with any other character is a regular expression, a pattern in which some characters stand for many, so mcp__ap__.* matches every tool on that server.5 The Agent SDK's "matchers follow the same rules as matchers in settings files".4 The last setting, onFailure: "block", is 28.6's.
Which route does the hook guard, and which routes does it miss?
One route: a tool call that this orchestrator's model makes. The hook does nothing if its matcher misses, if it is not loaded, or if the payment is reached another way. Figure 28.3 shows four routes:
- The orchestrator's tool call. The hook is here, and only here.
- The workflow's own code,
src/workflows/month-end.ts. It calls the connector with no model in between, so no hook runs. - A script on someone's laptop, holding a token, as at 9:40. Nothing in the orchestrator runs.
- A hosted session. A hosted worker runs no process of yours, so it has no hooks (23.6).
"Not loaded" is real in this build. The orchestrator runs with settingSources empty, and the Agent SDK loads hooks from settings files only when the matching settingSources entry is on.4 So a hook in .claude/settings.json never reaches it.
So the action itself checks too, with the scope, the row and the run key of 28.4. The hook stops the model early and explains why. The connector's check is the guarantee. A fooled model changes nothing for the hook, which reads the call, not the model's reasons. But it checks only what it was written to check. At 10:15, only the connector caught Priya's missing scope, and her run proposed nothing.
Figure 28.3. Four routes to one payment. The hook stands on one. The connector's check stands where all four meet.
What does the hook cost?
A call to get_approval before every proposed payment, a line in a file, and code you must test. The real cost is a configuration mistake. A matcher that is too wide puts the approval check on read tools, and denies them, which breaks the agent's function. So a hook needs tests on both sides: one that proves it denies, and one that proves the read tools still work.
How do you correct a hook configuration that looks right?
Read where it runs, what it matches, and what it does on every path, before you change anything. Here is a configuration a builder wrote. Name each fault in your own words before you read on. The broken configuration in Lab 28, Task 3 has three other faults, so these do not tell you its answers.
const paymentGuard: HookCallback = async (input) => {
const call = input as PostToolUseHookInput
const row = await getApproval(call)
if (!row) {
await writeEvidenceLine(call)
return { hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: "No approval on record" } }
}
return {}
}
const options = {
hooks: {
PostToolUse: [{ matcher: "mcp__ap__.*", hooks: [paymentGuard] }],
},
}Three faults, and one missing setting that 28.6 names:
- It runs after the tool.
PostToolUsefires when the tool has already returned. By then the payment is made, and a message to the model cannot take it back. Move the hook toPreToolUseand return adeny. - It matches too much.
mcp__ap__.*is a regular expression that matches every tool on the server, sofind_vendorandlist_open_invoiceswould meet the approval check too. Name the one tool in full,mcp__ap__schedule_payment. - The required action runs only sometimes. The evidence line is written only when no approval is found, so a payment that goes ahead leaves no line. Write the line first, on every path.
The missing setting is onFailure.
The operating rule: put a required action and a deny in one PreToolUse callback, in that order. Name the tool in full. And never treat the hook as the last guard: it decides on its own route, and the connector decides on every route.
Do this now. Ask your coding agent to list the routes to one action in your own project. Copy this line:
Pick the one tool in this project that changes something. List every way a call can reach it: a model's tool call, my own code, a script, a hosted session. For each one, name every check the call meets and the file and line where each check lives. Mark which checks run on every route. Change no file.
If no check runs on every route, that tool has no guarantee, only guards.
Check yourself
Question 1 / 7
0 answered
A line in CLAUDE.md says "never pay without an approval". Why does a hook enforce that rule when the line does not?
Sources
- Owner
- Panaversity