Lesson 65.5 · 5 of 10 · 4 min read · Reviewed 24 Nov 2026
Least-privilege tool design
By the end, you can
- Design tools so a fooled agent can do little harm.
- Apply least privilege to scopes, data and actions.
Chapter 34.5 made the case for narrow tools. Here’s the security checklist.
Credentials
- One credential per tool or agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done., scoped to exactly what it needs.
- Short-lived tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. where possible.
- Never put credentials in prompts, tool descriptions or model-visible output.
Data
- Filter by the authenticated user in code.
- Return only needed fields; leave out internal notes and other customers’ data.
Actions
- Cap amounts, counts and rates inside the tool.
- Separate read tools from write tools.
- Make writes idempotentIdempotency: The property that doing an operation twice has the same effect as doing it once. (63.2) and reversible where possible.
- Gate irreversible actions (64.5).
The test
For each tool, ask: if an attacker fully controlled the model, what’s the worst this tool could do? If the answer is unacceptable, narrow the tool.
Key points
- Assume the agent will sometimes be fooled; limit what that can cost.
- Scope credentials narrowly, filter data by the real user, and cap action sizes.
- Split read and write tools, and keep high-impact actions behind gates.
Narrow a broad tool to least privilege
Replace an any-query database tool with narrow tools whose worst case is acceptable.
You will have: Two scoped tools and a printed worst-case figure for a fully fooled agent.
Steps
- Read run_sql and say what an attacker could do with it.
- Write get_my_orders() to filter by the session's customer and drop internal fields.
- Write refund_my_order() for own orders only, capped at 100 and the order total.
- Compute the worst case if an attacker controlled every call.
Done when
- Only order A-1 is returned, without its notes.
- Both bad refunds are refused, and the worst case is 60 credits.
Open in the Playground No API key needed.
Starter code (Python)
# Narrow a broad tool to least privilege, then ask: if an attacker controlled the model, what is the worst it could do?
DB = {"orders": [{"id": "A-1", "customer": "c1", "total": 60, "notes": "VIP, internal only"},
{"id": "B-2", "customer": "c2", "total": 300, "notes": "fraud check pending"}]}
def run_sql(query):
# The broad tool the agent has today: any query, any table, any customer.
return f"ran: {query}"
# TODO: replace run_sql with two narrow tools:
# get_my_orders(session) -> only the signed-in customer's orders, without the notes field
# refund_my_order(session, order_id, amount) -> own orders only, capped at 100
def get_my_orders(session):
return DB["orders"]
def refund_my_order(session, order_id, amount):
return "refunded"
attacker = {"customer": "c1"} # a fooled agent acting for c1
print("orders seen:", get_my_orders(attacker))
print("refund B-2:", refund_my_order(attacker, "B-2", 300))
print("refund A-1 x5000:", refund_my_order(attacker, "A-1", 5000))
Show a solution
DB = {"orders": [{"id": "A-1", "customer": "c1", "total": 60, "notes": "VIP, internal only"},
{"id": "B-2", "customer": "c2", "total": 300, "notes": "fraud check pending"}]}
CAP = 100
PUBLIC_FIELDS = ("id", "total")
def get_my_orders(session):
return [{k: o[k] for k in PUBLIC_FIELDS} for o in DB["orders"] if o["customer"] == session["customer"]]
def refund_my_order(session, order_id, amount):
order = next((o for o in DB["orders"] if o["id"] == order_id and o["customer"] == session["customer"]), None)
if order is None:
return "error: no such order on your account"
if amount > min(CAP, order["total"]):
return f"error: refunds are capped at {min(CAP, order['total'])} for this order"
return f"refunded {amount}"
attacker = {"customer": "c1"}
seen = get_my_orders(attacker)
print("orders seen:", seen)
print("refund B-2:", refund_my_order(attacker, "B-2", 300))
print("refund A-1 x5000:", refund_my_order(attacker, "A-1", 5000))
worst = sum(min(CAP, o["total"]) for o in DB["orders"] if o["customer"] == attacker["customer"])
print("worst case if the model is fully controlled:", worst, "credits, own orders only")