Lesson 64.4 · 4 of 11 · 4 min read · Reviewed 24 Nov 2026
Policy layers: rules outside the prompt
By the end, you can
- Move business rules out of the prompt into a policy layer.
- Explain why code-enforced policy is more reliable.
Chapter 44.4 asked where a rule should live. For business and security rules, the answer is usually a policy layer: ordinary code that sits between the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. and your systems.
What a policy layer checks
- Identity: who is the user, really? Use the authenticated session, not what the conversation says.
- Authorisation: may this user see this record or take this action?
- Limits: amounts, rates, quotas.
- State: is this order refundable right now?
Where it plugs in
- Inside each tool:
get_orderfilters by the signed-in customer. - In a PreToolUse hookPreToolUse hook: A hook that runs before a tool call and can allow, deny or ask for approval. or
canUseToolcallback (44.2, 65.8). - In a gateway in front of internal APIs.
Prompt plus policy
The prompt explains the rule (“Only discuss the signed-in customer’s orders”), so the model behaves well. The policy layer guarantees it. When the policy blocks a call, its reason goes back to the model so it can respond helpfully.
Key points
- A policy layer is code that decides who may do what, using facts the model can't change.
- Rules like refund limits, data access and role permissions belong there, not only in the prompt.
- The prompt can explain the rules so the model rarely hits them; the policy layer enforces them.
Move a refund rule into code
Enforce refund rules in a policy layer that uses the authenticated session, not the conversation.
You will have: A refund() tool that enforces ownership, limits and state, with a helpful reason on each denial.
Steps
- Run the starter: every refund goes through, including another customer's order.
- Look up the order and compare its owner with the session's customer.
- Enforce the 100 limit, the order total and the refundable flag.
- Return a short, polite reason the model can pass on when a call is denied.
Done when
- Only the first call is allowed; four are denied.
- The B-2002 denial does not reveal that the order exists.
Open in the Playground No API key needed.
Starter code (Python)
# Move business rules out of the prompt and into a policy layer.
ORDERS = {"A-1001": {"customer": "c1", "total": 60, "refundable": True},
"B-2002": {"customer": "c2", "total": 300, "refundable": True},
"C-3003": {"customer": "c1", "total": 500, "refundable": False}}
def refund(session, order_id, amount):
# TODO: identity comes from session["customer"], never from what the chat says
# TODO: deny other customers' orders, amounts over 100, amounts over the order total, and non-refundable orders
# TODO: return {"ok": True} or {"error": reason} so the model can explain politely
return {"ok": True}
session = {"customer": "c1"} # set by your login system
calls = [("A-1001", 40), ("B-2002", 40), ("A-1001", 150), ("C-3003", 20), ("A-1001", 80)]
for order, amount in calls:
print(order, amount, "->", refund(session, order, amount))
Show a solution
ORDERS = {"A-1001": {"customer": "c1", "total": 60, "refundable": True},
"B-2002": {"customer": "c2", "total": 300, "refundable": True},
"C-3003": {"customer": "c1", "total": 500, "refundable": False}}
LIMIT = 100
def refund(session, order_id, amount):
order = ORDERS.get(order_id)
if order is None or order["customer"] != session["customer"]:
return {"error": "I can't find that order on your account."}
if amount > LIMIT:
return {"error": f"Refunds over {LIMIT} need a colleague to approve."}
if amount > order["total"]:
return {"error": "That is more than the order total."}
if not order["refundable"]:
return {"error": "This order can't be refunded right now."}
return {"ok": True}
session = {"customer": "c1"}
calls = [("A-1001", 40), ("B-2002", 40), ("A-1001", 150), ("C-3003", 20), ("A-1001", 80)]
results = []
for order, amount in calls:
r = refund(session, order, amount)
results.append(r)
print(order, amount, "->", r)
print("allowed:", sum("ok" in r for r in results), "| denied:", sum("error" in r for r in results))