Task 1.3
Hooks & Programmatic Enforcement
Deterministic business rules vs probabilistic prompt instructions
📘What You Need to Know
The Critical Distinction: Hooks vs Prompts
Hooks are deterministic — they run 100% of the time, every time. Use them for critical business rules, compliance requirements, and security checks. Prompts are probabilistic — the model may follow them, ignore them, or interpret them inconsistently. Use prompts for style preferences, tone guidelines, and soft behavioral nudges.
The exam tests this distinction constantly. When a scenario involves financial limits, regulatory compliance, or security enforcement, the correct answer is always programmatic hooks — never "add it to the system prompt."
Programmatic hooks for business rules. Prompts for preferences. This single principle eliminates wrong answers on many exam questions.
Hook Types and When to Use Each
PreToolUse hooks intercept tool calls before execution. They can block the call entirely, modify parameters before the tool runs, or inject validation logic. Use PreToolUse when you need to prevent an action from happening — authorization checks, parameter sanitization, rate limiting.
PostToolUse hooks intercept after execution. They can modify the tool output, normalize data formats, enforce business thresholds on results, or trigger side effects like escalation. Use PostToolUse when you need to validate or transform what the tool returned.
- •PreToolUse: block unauthorized actions, validate parameters, enforce rate limits before execution
- •PostToolUse: enforce refund thresholds, redact PII from outputs, trigger escalation on policy violations
# PostToolUse hook example — $500 refund limit
def post_tool_use_hook(tool_name, tool_input, tool_output):
if tool_name == "process_refund":
amount = tool_input.get("amount", 0)
if amount > 500:
return {
"blocked": True,
"action": "escalate_to_human",
"reason": f"Refund ${amount} exceeds $500 limit"
}
return tool_output # allow normal flowReal-World Example: Refund Policy Enforcement
A customer support agent has a process_refund tool. The business rule: refunds under $500 are auto-approved; refunds $500 and above require human approval. The wrong approach is adding "never process refunds over $500" to the system prompt — Claude might ignore this under edge cases or creative interpretations.
The correct approach: a PostToolUse hook on process_refund that checks the amount after the tool executes. If amount > 500, the hook returns blocked=True with action=escalate_to_human. This runs deterministically regardless of what Claude decided.
Valid vs Invalid Escalation Triggers
Valid escalation triggers are objective and programmatic: the customer explicitly requests a human agent, a policy gap is detected (no rule covers the situation), the task exceeds agent capabilities, or a business threshold is exceeded (enforced via hooks, not model judgment).
Invalid escalation triggers rely on model judgment: negative customer sentiment (an angry customer asking to change their address does not need a human), self-reported low confidence (model confidence scores are not well-calibrated for escalation decisions), or vague "when in doubt, escalate" prompt instructions.
Sentiment ≠ task complexity. An angry tone does not mean the task is beyond the agent's capabilities.
⚠️Exam Traps
Trap: Using prompt instructions for a $500 refund limit
Why it's wrong: Prompts are probabilistic — the model may ignore them.
Trap: Escalating to human based on negative customer sentiment
Why it's wrong: Sentiment does not equal task complexity.
Trap: Trusting self-reported model confidence for escalation decisions
Why it's wrong: Model confidence is not well-calibrated.
✓Concept Check
You need to enforce a $500 refund limit. What is the correct approach?
🔨Build Exercise
Intermediate40 minutesImplement Refund Guardrails with Hooks
What you'll learn
- •Distinguish hooks from prompt-based enforcement
- •Implement PreToolUse and PostToolUse hooks
- •Verify deterministic behavior regardless of model decisions