Prompt
How do I make an AI answer engine that respects permissions?
Latest observation
To build an AI answer engine that respects permissions, treat permissions as a first-class security layer around the model—not something the model “just knows.”
Core principle
Never let the model decide access by itself.
Your application should:
- authenticate the user,
- determine what they’re allowed to access,
- retrieve only permitted content,
- give the model only that content,
- enforce output filtering before returning the answer.
Recommended architecture
1) Authenticate the user
Use your normal auth system:
- SSO/OAuth/SAML
- session cookies
- API keys
- service accounts
Identify:
- user ID
- roles/groups
- org/team
- tenant
- clearance level
2) Authorize at retrieval time
When the user asks a question, translate that into a search/query over your knowledge base or documents, but apply ACL checks before retrieval.
Examples of permission rules:
- user can only see docs in their team
- user can see docs with tags matching their clearance
- user can see documents they own
- some fields in a document are masked unless privileged
Best practice:
- store ACL metadata on every document/chunk
- filter results using the user’s permission context
- do not retrieve unauthorized text at all
3) Use permission-aware indexing
For RAG systems, build your index with metadata such as:
tenant_iddocument_idallowed_rolesallowed_usersdepartmentsensitivityclassification
Then during search:
- filter by metadata before or during vector search
- or do a two-stage retrieval:
- broad candidate search
- authorization filter
- rerank only permitted candidates
4) Pass only permitted context to the model
The LLM should only see:
- the user question
- approved context snippets
- minimal metadata needed for citation
Do not pass:
- hidden docs
- raw ACL tables
- unauthorized chunks “just in case”
5) Enforce output filtering
Even with correct retrieval, the model may infer sensitive details from context. Add a final policy check before sending the response.
Examples:
- redact secrets, PII, internal IDs
- block answers if they reveal unauthorized info
- replace with “You don’t have access to that information”
6) Prefer “deny by default”
If permission is unclear:
- do not answer from hidden sources
- ask the user to request access
- or provide a safe, generic answer
Practical patterns
Pattern A: Document-level ACL
Simplest:
- each document has an access list
- user can retrieve only if they’re on the list
Good for:
- enterprise knowledge bases
- policies
- wikis
Pattern B: Row/field-level security
Needed when one document contains mixed sensitivity:
- retrieve doc
- mask restricted fields before prompting
- use structured extraction to separate public vs sensitive parts
Pattern C: Tenant isolation
For multi-tenant systems:
- every query must include
tenant_id - index partitioning or strict filters
- never allow cross-tenant retrieval
Pattern D: Policy engine
Use a centralized policy engine like:
- OPA/Open Policy Agent
- Cedar
- Zanzibar-style authorization
This gives consistent decisions for:
- search
- retrieval
- tool use
- response delivery
Important security mistakes to avoid
- letting the LLM read all documents and “be careful”
- relying on prompt instructions for access control
- retrieving first and filtering later only in the UI
- using embeddings without ACL metadata
- caching responses without permission scoping
- allowing tool calls that bypass authorization
- leaking through citations, summaries, or examples
Minimal implementation flow
- User asks a question
- Backend authenticates user
- Backend computes permissions
- Retrieval layer searches only authorized sources
- LLM generates answer from permitted context
- Output filter checks for sensitive leakage
- Return answer or deny
Simple pseudocode
def answer_question(user, question):
perms = get_permissions(user)
candidates = search_index(question, filters={
"tenant_id": user.tenant_id,
"allowed_roles": perms.roles,
})
authorized = [doc for doc in candidates if can_access(user, doc)]
context = build_context(authorized)
response = llm.generate(
system_prompt=POLICY_PROMPT,
user_prompt=question,
context=context,
)
if violates_policy(response, user):
return "Sorry, I can’t provide that information."
return response
If you want strong guarantees
Use all of these:
- authenticated users
- permission-aware retrieval
- structured ACLs
- policy engine
- output redaction
- auditing/logging
- tests for unauthorized leakage
Good test cases
Verify the system fails safely for:
- user without access to a doc
- user with partial access to a doc
- cross-tenant query
- prompt injection trying to reveal hidden docs
- model hallucinating unauthorized info
- cached response shown to wrong user
If you want, I can also give you:
- a reference architecture diagram,
- a Python/FastAPI example, or
- a vector database ACL design for permission-aware RAG.