The idea
An agent acting for a user must never be able to do more than that user could. If a junior employee asks the agent to read the CEO’s mailbox, the request should fail exactly as it would in the normal app.
Pass the user’s identity from the authenticated session into every tool call (never from model-generated arguments), and check permissions in the tool or the API it calls. For third-party systems, use OAuth with the user’s own delegated token and minimal scopes rather than a powerful service account.
Log the user, the agent and the action together, so you can answer "who did this?"
Agents with a super-user service account are privilege escalation waiting to happen.
Words to know
- Delegated accessActing on behalf of a user with their permissions.
- Confused deputyA privileged component tricked into misusing its authority.
Try it without code
Find your service accounts
- List the credentials your agent uses for each tool.
- Mark any that can access more than the current user can.
Use it in code
Inject identity from the session and check permissions in the tool.
from dataclasses import dataclass
@dataclass
class Session:
user_id: str
roles: set[str]
def read_document(doc_id: str, *, session: Session, docs, acl):
if not acl.can_read(session.user_id, doc_id):
return {"error": "You do not have access to this document."}
return {"text": docs.get(doc_id)}
def dispatch(call, session):
# the model controls call.input; the session comes from the authenticated request
handlers = {"read_document": read_document}
return handlers[call.name](**call.input, session=session, docs=DOCS, acl=ACL)Add per-user document permissions to your agent and test that a user cannot read a document by guessing its ID.
Common mistakes
- user_id as a tool argument the model fills in.
- Checking permissions only in the UI.