Lesson 44.4 · 4 of 4 · 5 min read · Reviewed 24 Nov 2026
Hooks vs prompt instructions
By the end, you can
- Decide whether a rule belongs in a prompt, a permission setting or a hook.
- Explain why critical rules need code, not words.
This is one of the most tested ideas in agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. design: where does a rule live?
Three layers
| Layer | What it controls | Strength |
|---|---|---|
| Prompt instructions | Tone, judgment, priorities, how to approach a task | Followed most of the time |
| Permissions / allowedTools | Which tools exist and which need approval | Enforced |
| Hooks | Conditional rules on specific inputs and outputs | Enforced, and can be precise |
A quick test
Ask: what happens if the model breaks this rule once?
- A slightly too formal tone? Prompt.
- The agent edits a file it shouldn’t? PermissionsPermission rule: An allow, ask or deny rule that controls which tools, commands or paths a coding agent may use.: deny writes to that path.
- A refund over the limit, a query against production, a secret in a log? HookHook: Code that runs automatically at a lifecycle event, such as before or after a tool call, to check, block or log it..
Use both
The best designs layer them. The prompt explains the rule and why it exists, so the model rarely hits the wall. The hook is the wall. When the hook does fire, its reason tells the model what to do instead, so the agent recovers instead of stalling.
Key points
- Prompts guide behaviour well most of the time; hooks and permissions enforce it every time.
- Use prompts for judgment and style, permissions for which tools exist, and hooks for conditional rules on inputs and outputs.
- If breaking a rule once would be costly or irreversible, enforce it in code.
Measure a prompt rule against a hook
Show with numbers why a costly rule belongs in a hook and not only in the prompt.
You will have: A short simulation that counts over-limit refunds with the rule in the prompt only, then with a hook.
Steps
- Run the starter and note how many over-limit refunds slip through with the prompt alone.
- Write the PreToolUse check that denies refunds over the limit.
- Run both modes again and compare the counts.
- Change the model's follow rate from 0.98 to 0.999 and see that the prompt-only count is still not reliably zero.
Done when
- Prompt only lets a few over-limit refunds through out of 500.
- Prompt plus hook lets 0 through.
- You can say in one sentence which of the four rules in 36.4's table belongs where.
Open in the Playground No API key needed.
Starter code (Python)
# Compare a rule in the prompt with the same rule in a hook, over 500 simulated refund requests.
import random
LIMIT = 500 # credits; above this a person must approve
def make_requests():
r = random.Random(7)
return [r.choice([40, 120, 300, 650, 900, 1200]) for _ in range(500)]
def model_decides(amount, r):
"""Simulated model that read the rule in its prompt. It follows it 98% of the time."""
if amount > LIMIT and r.random() < 0.98:
return "escalate"
return "refund"
def pre_tool_use(amount):
# TODO: return "deny" when the refund is over LIMIT, otherwise "allow"
return "allow"
def run(use_hook):
r = random.Random(7)
over = 0
for amount in make_requests():
action = model_decides(amount, r)
if action == "refund" and use_hook and pre_tool_use(amount) == "deny":
action = "escalate" # the hook's reason sends the model to a person instead
if action == "refund" and amount > LIMIT:
over += 1
return over
print("prompt only, over-limit refunds:", run(False))
print("prompt + hook, over-limit refunds:", run(True))
Show a solution
# Compare a rule in the prompt with the same rule in a hook, over 500 simulated refund requests.
import random
LIMIT = 500 # credits; above this a person must approve
def make_requests():
r = random.Random(7)
return [r.choice([40, 120, 300, 650, 900, 1200]) for _ in range(500)]
def model_decides(amount, r):
"""Simulated model that read the rule in its prompt. It follows it 98% of the time."""
if amount > LIMIT and r.random() < 0.98:
return "escalate"
return "refund"
def pre_tool_use(amount):
return "deny" if amount > LIMIT else "allow"
def run(use_hook):
r = random.Random(7)
over = 0
for amount in make_requests():
action = model_decides(amount, r)
if action == "refund" and use_hook and pre_tool_use(amount) == "deny":
action = "escalate" # the hook's reason sends the model to a person instead
if action == "refund" and amount > LIMIT:
over += 1
return over
print("prompt only, over-limit refunds:", run(False))
print("prompt + hook, over-limit refunds:", run(True))
Finished the chapter? Take the chapter check.