By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Northwind's support agent refuses to discuss competitors, and one system prompt line enforces it. Six months later, two more callers share that model: a nightly ticket summary and a chat bot.
Neither sends that line. The chat bot tells a customer that a competitor is cheaper. Nothing broke in the code anyone reviewed.
A guardrail is a check outside the model that decides whether a request, a response or an action may proceed. A content policy is the set of rules about what the application will produce. A guardrail in one prompt belongs to one endpoint, not to the platform.
The shared tool path. Prompts multiply with every caller, and a copied check gets forgotten. One choke point survives a new endpoint or a new team.
| Layer | What it does | What it costs | Its failure mode |
|---|---|---|---|
| Input filter | Labels the request before generation | Latency and false refusals | Misses text in a tool result |
| System instruction | States the policy to follow | A few hundred tokens | Loses to pressure and is copied |
| Output check | Validates schema, secrets and content | Rejects some good answers | Catches wording, not meaning |
| Action gate | Checks the tool, arguments and approver | Blocks valid work | Useless if the credential is broader |
Two rules follow. Put the strongest check where the effect happens, the last point before the change is real. And measure what each layer refuses, because a guardrail that blocks good work gets switched off.
The path has two properties worth naming. A refusal carries a reason, because 'no' with no rule id becomes a support ticket. And the gate runs after the model, so the model cannot skip it.
Audit closes the loop. Write one line per decision: request id, caller, tool name, the arguments that matter, the rule that fired and the outcome.
Least privilege means a component gets the smallest permission it needs, for the shortest time. It caps the damage of every other failure.
The common mistake is a broad credential plus a prompt that asks the model to stay in range. A credential does not read prompts.
refund_order(order_id, amount) with a cap beats a payment key with full access.The gate below is the idea in twenty lines. It trusts neither the model, nor the document, nor the caller's tenant claim.
from dataclasses import dataclass
ALLOWED_TOOLS = {'lookup_order', 'refund_order'}
MAX_REFUND = 200.00
@dataclass(frozen=True)
class Caller:
user_id: str
tenant_id: str
roles: frozenset[str]
def authorize(caller: Caller, tool: str, args: dict) -> tuple[bool, str]:
if tool not in ALLOWED_TOOLS:
return False, 'tool_not_allowed'
if args.get('tenant_id') != caller.tenant_id:
return False, 'tenant_mismatch'
if tool == 'refund_order':
amount = float(args.get('amount', 0))
if amount > MAX_REFUND and 'approver' not in caller.roles:
return False, 'approval_required'
return True, 'allowed'
agent = Caller(user_id='u_88', tenant_id='northwind', roles=frozenset({'support'}))
print(authorize(agent, 'refund_order', {'tenant_id': 'northwind', 'amount': 480.0}))
print(authorize(agent, 'refund_order', {'tenant_id': 'acme', 'amount': 40.0}))
print(authorize(agent, 'drop_table', {'tenant_id': 'northwind'}))Note what the gate does not do. It never reads the conversation, so no wording changes its answer. It returns a code a test can assert on.
Authentication answers who is calling. Authorisation answers what that caller may do. An agent needs both on every call.
The exam does not ask you to implement OAuth. It asks you to pick the design that limits blast radius, and to know what the tool must check anyway.
| Design | Who holds the credential | Blast radius | Use it when |
|---|---|---|---|
| Shared service account | The application, for every tenant | Every tenant at once | Never for customer data |
| Scoped service account | The application, one resource | One resource | A background job with no user |
| Per-user token | The end user, delegated | One user's data | A feature reading their records |
| Short-lived credential | Issued per task, minutes | One task | A high-value write |
Prefer the narrowest row that works. A nightly summary with no user needs a scoped account. A customer reading their own orders needs the caller's identity.
Claude Code hooks are commands your harness runs at fixed points in the loop, outside the model. PreToolUse runs before a tool executes, PostToolUse after, Stop at the end.
A hook is the most deterministic guardrail in the developer workflow, because the model cannot decline it. PreToolUse reads one JSON event on standard input. Exit code 0 allows the call; exit code 2 blocks it and returns the error to the model.
import json
import sys
DESTRUCTIVE = ('rm -rf', 'git push --force', 'drop table')
def main() -> int:
event = json.load(sys.stdin)
if event.get('tool_name') != 'Bash':
return 0
command = event.get('tool_input', {}).get('command', '')
for pattern in DESTRUCTIVE:
if pattern in command:
print(
f'blocked: {pattern!r} is not allowed in this repository',
file=sys.stderr,
)
return 2
return 0
if __name__ == '__main__':
raise SystemExit(main())Write the blocked message for the model, because the model is the reader. 'blocked: no force pushes in this repository' gives it a next step; 'error' does not.
A hook is one layer, not a sandbox; it cannot cover a tool that another configuration exposes. Pair it with the allow-list and the credential scope.
Irreversible actions get a person. An action is irreversible when a second call cannot undo it: a sent email, a refund, a deploy. Route those through an approval that shows the tool, the target and the effect.
Key takeaways
A guardrail belongs to the platform; prompts are copied and forgotten.
Layer input, output and action checks; the strongest sits where the effect happens.
Least privilege caps every other failure, and a credential ignores your prompt.
Take the tenant from the session, and choose the narrowest identity that works.
A PreToolUse hook exits 2 to block a call; its stderr is the model's feedback.
Send irreversible actions to a person, and log the decision, not the transcript.
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.