By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A payments team added one line to its system prompt: never issue a refund above 100 USD without human approval. The rule held in every test. Three weeks later a support agent pasted a customer message that claimed the limit had been raised, and the agent issued a refund of 640 USD.
The rule was a suggestion written in the same channel as the untrusted text. A hook is the same rule written as code that runs at a fixed point in the loop, whether or not the model agrees with it.
A hook is a callback that your harness runs at a named point in the loop. The harness is the program around the loop: it defines the tools, enforces permissions, holds credentials, runs the sandbox and emits telemetry.
At a pre-tool point, the hook sees the tool name and its input. At a post-tool point, it sees the result. At the end of a turn or a session, it sees the state that is about to be saved. Its return value is a decision the loop must obey.
That is the difference from a prompt rule. A prompt rule is a probability. A hook is a branch. The same input produces the same verdict every time, so you can unit test the policy without a model in the test.
| Hook point | What it sees | What it is for |
|---|---|---|
| Before a tool runs | Tool name and input | Deny, allow, require approval, rewrite input |
| After a tool returns | The result, including errors | Redact, validate, log, block the value |
| At the end of a turn | State about to be saved | Block an incomplete turn, or write an audit line |
| At session start | Project context | Load memory and policy before the first call |
A hook has a small vocabulary: allow, deny, ask a human, rewrite the input, redact the result, or block the turn from being saved. Choose one and make the reason explicit.
A silent deny teaches the model nothing and hides the event from your logs. Always return a reason, and always write one line about the decision. That line is what lets you answer why a refund did not go out.
REFUND_LIMIT = 100
def pre_tool_use(tool_name, tool_input, context):
"""A deny stops the call before it reaches the real system."""
if tool_name != "issue_refund":
return {"decision": "allow"}
amount = tool_input.get("amount", 0)
if amount > REFUND_LIMIT and not context.get("human_approved"):
return {
"decision": "deny",
"reason": f"{amount} exceeds the {REFUND_LIMIT} USD limit",
}
return {"decision": "allow"}
def post_tool_use(result):
"""Redact before the value reaches the transcript."""
note = str(result.get("note", ""))
return {"note": note.replace("4111 1111 1111 1111", "[card redacted]")}
print(pre_tool_use("issue_refund", {"amount": 42}, {}))
print(pre_tool_use("issue_refund", {"amount": 420}, {}))
print(pre_tool_use("issue_refund", {"amount": 420}, {"human_approved": True}))
print(post_tool_use({"note": "card 4111 1111 1111 1111 charged"}))Notice what the hook does not do. It does not ask the model to be careful, and it does not depend on a prompt. A refactor of the system prompt cannot weaken it, and a user cannot argue past it.
You can write the harness yourself on the Agent SDK, or run on an Anthropic-hosted managed agent, which supplies the harness, the sandbox and the runtime for you. The question is who holds which responsibility, not which option is newer.
A self-hosted harness keeps credentials, the network boundary and the logs inside your own infrastructure. That is the right default when data residency, egress control or a private tool estate dominates. You also own upgrades, scaling and the on-call page.
A managed agent removes that operations work and shortens the path to a running system. You trade some control over the runtime: where the sandbox runs, which versions you pin, and how a tool reaches a private service.
| Concern | Self-hosted harness | Anthropic-hosted managed agent |
|---|---|---|
| Credentials | Stay in your secret manager | Held by the hosted runtime |
| Network | Your egress rules apply | Reaches only what the platform allows |
| Upgrades | You pin and roll out | Platform versions move for you |
| Operations | You run, scale and page | Mostly theirs |
| Custom tools | Any private service you can reach | Must fit the platform's integration path |
Pick one rule that currently lives only in a prompt.
Key takeaways
A hook is a callback at a fixed loop point, and its return value is a decision the harness obeys.
Hooks are deterministic policy you can test; a prompt rule is only a probability.
Deny with a reason, and log the tool, decision, reason and actor.
The harness owns tools, permissions, credentials, sandbox and telemetry.
Self-hosting keeps control and adds operations; a managed agent removes operations and takes some control.
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.