This prompt is for teams building Retrieval-Augmented Generation (RAG) systems over compliance-sensitive, regulated, or classified data stores. Its job is to rewrite a user's natural language query while cross-referencing a provided user entitlement manifest. The rewritten query must embed explicit access constraints so that downstream retrieval components cannot surface documents outside the user's clearance scope. Use this prompt when a single retrieval index serves multiple user classes with different data access levels, and you need a pre-retrieval enforcement layer that fails closed.
Prompt
Entitlement-Check Query Rewrite Prompt for Secure RAG

When to Use This Prompt
Defines the job-to-be-done, ideal user, required context, and boundaries for the Entitlement-Check Query Rewrite Prompt.
The ideal user is an engineering lead or AI architect responsible for a multi-tenant RAG system where data sensitivity varies by user role. Required context includes a structured user entitlement manifest—typically a JSON object listing allowed document scopes, classification levels, or project memberships—and the original user query. This prompt is not a replacement for database-level access controls or row-level security. It is a defense-in-depth component that reduces the blast radius of a misconfigured retriever, not a primary authorization system. Do not use this prompt when the retrieval index is already partitioned per user or when entitlements are enforced at the storage layer with guaranteed isolation.
Before deploying this prompt, ensure you have a reliable entitlement source of truth and a mechanism to inject the rewritten query into your retrieval pipeline. The prompt works best when paired with a validation step that confirms the rewritten query contains only authorized scope references. If your entitlement model changes frequently, plan to version the manifest schema and test the prompt against stale or malformed manifests. In high-risk regulated environments, add a human review step for queries that touch the boundary of a user's clearance—this catches edge cases where the model misinterprets complex entitlement rules.
Use Case Fit
Where the Entitlement-Check Query Rewrite Prompt delivers value and where it introduces risk. Use these cards to decide if this pattern fits your retrieval architecture before you integrate it.
Good Fit: Regulated or Multi-Tenant RAG
Use when: your retrieval corpus contains compliance-sensitive, legal, financial, healthcare, or tenant-isolated documents where unauthorized access carries regulatory or contractual consequences. Why: the prompt explicitly cross-references user entitlements during query construction, preventing retrieval paths from touching out-of-scope documents before the vector or keyword index is even called.
Bad Fit: Public-Facing or Open-Corpus Search
Avoid when: all users share the same document access scope or the corpus is publicly available with no access restrictions. Why: the entitlement-check rewrite adds latency and token overhead without providing security value. A simpler query expansion or decomposition prompt will be faster and cheaper.
Required Inputs: Entitlement Profile and Query
What you need: a structured user entitlement profile (roles, clearance levels, tenant IDs, allowed document scopes) and the raw user query. Guardrail: validate the entitlement profile freshness before every rewrite call. Stale permissions produce either over-restricted or under-restricted queries that are both correctness and compliance failures.
Operational Risk: Entitlement Drift Between Rewrite and Retrieval
What to watch: the model rewrites the query with embedded access checks, but the user's entitlements change between the rewrite step and the actual retrieval execution. Guardrail: treat the rewritten query's access constraints as a point-in-time artifact. Re-validate entitlements at retrieval time and reject results that cross updated boundaries.
Operational Risk: Over-Restriction from Conservative Rewrites
What to watch: the model interprets entitlement boundaries too conservatively and strips legitimate query terms, causing zero-result or low-recall retrievals. Guardrail: log and monitor the rate of empty result sets after entitlement rewrites. Implement a fallback path that surfaces the restriction decision for human review rather than silently returning nothing.
Not a Substitute for Authorization Enforcement
What to watch: teams treat the prompt-level entitlement check as their primary access control. Guardrail: the rewrite prompt is a defense-in-depth layer, not the enforcement mechanism. Always apply authorization filters at the retrieval index or database level. The prompt reduces the attack surface but does not close it.
Copy-Ready Prompt Template
A reusable prompt template that rewrites a user query while embedding entitlement checks to prevent retrieval from unauthorized document scopes.
This template is the core instruction set you will send to the model. It forces the model to treat the user's access profile as a first-class constraint during query generation, not as an afterthought. The prompt instructs the model to produce a rewritten query and an explicit list of allowed and denied document scopes, making the entitlement decision auditable before any retrieval call is made. Use this template when you need to guarantee that a RAG pipeline never surfaces content from outside a user's clearance boundary, such as in legal document review, financial audit, or healthcare record retrieval.
textYou are a secure query rewriter for a Retrieval-Augmented Generation system. Your job is to rewrite a user's natural language query so that it retrieves only documents the user is authorized to see. You must never generate a query that could access documents outside the user's entitlement scope. ## INPUTS - User Query: [USER_QUERY] - User Entitlements (JSON): [USER_ENTITLEMENTS] - Available Document Scopes (JSON): [DOCUMENT_SCOPES] - Output Schema: [OUTPUT_SCHEMA] ## INSTRUCTIONS 1. Parse the user's query to understand the information need. 2. Cross-reference the information need with the user's entitlements. Identify which document scopes the user is permitted to access and which are explicitly denied. 3. Rewrite the query to include explicit scope constraints. The rewritten query must only reference permitted scopes. 4. If the user's query implicitly or explicitly requests information from a denied scope, rewrite the query to exclude that scope and add a note to the `entitlement_violations` field. 5. If the user has no entitlements that can satisfy the query, set the rewritten query to `null` and explain why in the `block_reason` field. 6. Output the result as a valid JSON object matching the Output Schema. ## CONSTRAINTS - Do not invent document scopes. Use only scopes from the provided `DOCUMENT_SCOPES` list. - Do not assume a user has access to a scope unless it is explicitly listed in their entitlements. - If a scope is not in the user's entitlements, treat it as denied. - The rewritten query must be a standalone string suitable for a vector or keyword search engine. - If the query is blocked, `rewritten_query` must be `null` and `block_reason` must be populated. ## OUTPUT SCHEMA { "rewritten_query": "string | null", "allowed_scopes": ["string"], "denied_scopes": ["string"], "entitlement_violations": ["string"], "block_reason": "string | null" }
Before sending this prompt to the model, replace every square-bracket placeholder with concrete data from your application. [USER_ENTITLEMENTS] should be a JSON object mapping scope identifiers to permission levels. [DOCUMENT_SCOPES] should be the complete list of valid scopes in your system, which prevents the model from hallucinating scope names. [OUTPUT_SCHEMA] can be replaced with your own schema if you need additional fields like confidence_score or audit_trail. After receiving the model's output, validate the JSON structure, confirm that every scope in allowed_scopes and denied_scopes exists in your system's scope registry, and log any entitlement_violations for security review. Never pass the rewritten_query directly to your retrieval engine without this validation step.
Prompt Variables
Inputs required to reliably execute the Entitlement-Check Query Rewrite Prompt. Each placeholder must be populated before the prompt is sent to the model. Validation notes describe how to verify the input is well-formed and safe.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[USER_QUERY] | The original natural language question from the user that needs to be rewritten for secure retrieval. | Show me the Q3 audit findings for the Phoenix project. | Must be a non-empty string. Reject if null, empty, or exceeds the model's context window after assembly. Sanitize for prompt injection patterns before insertion. |
[USER_ENTITLEMENTS] | A structured list or JSON array of document scopes, classifications, and access levels the user is authorized to view. | ["project:Phoenix", "classification:Internal", "region:US"] | Must be a valid JSON array or list. Reject if empty or missing. Each entry must match a known entitlement schema. Log if entitlements contain wildcards or overly broad scopes. |
[ENTITLEMENT_SCHEMA] | The canonical taxonomy of allowed entitlement keys and value formats used in the system. | ["project:", "classification:", "region:", "clearance:"] | Must be a valid JSON array of string patterns. Validate against a known schema version. Reject if schema is missing or contains unrecognized keys that could allow privilege escalation. |
[RETRIEVAL_INDEX_DESCRIPTION] | A brief description of the document corpus, including available metadata fields and their semantics. | Index contains audit reports with metadata fields: project, classification, region, clearance, date. | Must be a non-empty string. Should accurately reflect the live index schema. Stale descriptions cause the model to generate filters against non-existent fields. Version this alongside the index schema. |
[OUTPUT_SCHEMA] | The expected JSON structure for the rewritten query, including the rewritten text and the embedded access filter clause. | {"rewritten_query": "string", "access_filter": {"must": [...], "must_not": [...]}} | Must be a valid JSON Schema or example object. Validate that the model's output conforms to this schema. Reject outputs missing required fields or containing extra fields that could bypass downstream validation. |
[MAX_QUERY_LENGTH] | The maximum allowed character length for the rewritten query text to prevent overly verbose rewrites that degrade retrieval performance. | 500 | Must be a positive integer. Enforce post-generation truncation or rejection if exceeded. Overly long queries often indicate the model is embedding document content rather than search terms. |
[FORBIDDEN_TERMS] | A list of terms, patterns, or scope identifiers that must never appear in the rewritten query or access filter. | ["classification:TopSecret", "clearance:admin"] | Must be a JSON array of strings. Validate the model's output against this deny-list. Any match should trigger a rejection or forced human review. Update this list when new sensitive scopes are added to the system. |
Implementation Harness Notes
How to wire the Entitlement-Check Query Rewrite Prompt into a secure RAG application with validation, logging, and human review gates.
This prompt is not a standalone component; it is a security-critical gate that must be integrated into your retrieval pipeline before any vector or keyword search executes. The prompt accepts a user query and a structured entitlement payload, then produces a rewritten query with embedded access constraints. The application layer is responsible for supplying accurate, up-to-date entitlements and for enforcing the output constraints downstream. Never rely on the model alone to enforce access boundaries—the retrieval system must apply the generated filters as a hard constraint, and the application must verify that returned document IDs fall within the user's authorized scope.
Wire the prompt into a pre-retrieval middleware function that constructs the entitlement payload from your identity provider (e.g., OAuth scopes, LDAP groups, or a policy engine like OpenFGA or Cedar) and injects it into the [USER_ENTITLEMENTS] placeholder. The function should validate the model's output against a strict schema: a rewritten query string, an allow-list of document scopes or classification labels, an explicit deny-list, and an audit trail of any terms removed from the original query. Implement a retry loop with a maximum of two attempts if the output fails schema validation or if the generated allow-list contains scopes outside the user's entitlements. Log every rewrite attempt—including the original query, the entitlement payload hash, the rewritten query, and the filter clauses—to an immutable audit store. For regulated environments, route any query where the model removes more than 30% of the original terms or where the deny-list is non-empty to a human reviewer before retrieval proceeds.
Model choice matters here. Use a model with strong instruction-following and low hallucination rates on structured output tasks, such as Claude 3.5 Sonnet or GPT-4o, and set the temperature to 0 or near-zero to maximize deterministic enforcement of the entitlement schema. If you are using a local or air-gapped model, validate the output schema even more aggressively and consider a secondary validation prompt that checks whether the rewritten query would retrieve documents outside the user's clearance. The next step after implementing this harness is to build a regression test suite using the entitlement boundary enforcement test cases described in the companion evaluation section—run those tests on every prompt or model change before deployment.
Expected Output Contract
Defines the exact structure, types, and validation rules for the rewritten query object returned by the Entitlement-Check Query Rewrite Prompt. Use this contract to parse, validate, and integrate the model's output into your retrieval pipeline.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
rewritten_query | String | Must be non-empty. Must contain at least one explicit access constraint derived from [USER_ENTITLEMENTS]. | |
access_constraints_applied | Array of strings | Each string must match a valid document scope or clearance label from [ACCESS_CONTROL_TAXONOMY]. Array must not be empty. | |
original_query_preserved | Boolean | Must be true if the core semantic intent of [USER_QUERY] is unchanged; false if the query was rejected or fundamentally altered for safety. | |
denied_scope_terms_removed | Array of strings | If present, each string must be a term or phrase from [USER_QUERY] that was removed because it maps to a scope outside [USER_ENTITLEMENTS]. | |
entitlement_conflict_flag | Boolean | Must be true if [USER_QUERY] explicitly requests access to a scope not in [USER_ENTITLEMENTS]; false otherwise. Triggers human review if true. | |
rewrite_rationale | String | Must be a single sentence explaining which entitlement rules were applied. Must not exceed 280 characters. | |
requires_human_approval | Boolean | Must be true if entitlement_conflict_flag is true OR if access_constraints_applied contains a scope marked as 'sensitive' in [ACCESS_CONTROL_TAXONOMY]. |
Common Failure Modes
Entitlement-check query rewrites fail in predictable ways. The most dangerous failures are silent ones where the rewrite appears valid but bypasses access controls. These cards cover the failure patterns that surface first in production and the guardrails that catch them before retrieval executes.
Entitlement Boundary Leakage
What to watch: The rewritten query drops or weakens an access constraint present in the user's entitlement profile, allowing retrieval to touch documents outside the user's clearance. This often happens when the model paraphrases a permission clause into a softer form or omits a required scope filter. Guardrail: Post-rewrite validation that diffs the generated query against the input entitlement list and rejects any rewrite where a required scope is absent. Log the diff for audit.
Over-Permission Through Synonym Expansion
What to watch: The model expands a query term with a synonym that maps to a broader or different entitlement scope than the user possesses. For example, expanding 'Q3 financials' to 'financial reports' may cross from departmental access into restricted corporate-wide data. Guardrail: Maintain an allowlist of permitted expansion terms per entitlement scope. Validate every expansion term against the user's scope before injection. Reject terms that cross scope boundaries.
Implicit Assumption of Full Access
What to watch: The model rewrites the query as if the user has unrestricted access, ignoring the entitlement context entirely. This is common when the prompt places the entitlement block after the rewrite instruction or when the model treats entitlements as optional hints rather than hard constraints. Guardrail: Structure the prompt so entitlement constraints appear before the rewrite task and use explicit constraint language such as 'You must not retrieve from scopes outside this list.' Run a regression test with a zero-entitlement user to verify the model refuses or returns empty queries.
Entitlement List Truncation in Long Contexts
What to watch: When a user has many entitlement scopes, the model may silently drop scopes that appear later in the list, especially if the prompt is near the context window limit. The rewrite looks plausible but misses access to documents the user is authorized to see, causing false negatives in retrieval. Guardrail: Post-rewrite verification that the output references every entitlement scope from the input. If the count doesn't match, flag for human review or re-prompt with explicit scope enumeration.
Query Drift Away from Security Intent
What to watch: The model optimizes for retrieval relevance at the expense of security, producing a well-formed query that would return great results but crosses entitlement boundaries. This happens when the prompt's tone emphasizes 'find the best answer' more strongly than 'stay within these boundaries.' Guardrail: Weight the prompt's constraint language so security boundaries are non-negotiable. Use phrases like 'You must not retrieve documents outside these scopes even if doing so would improve answer quality.' Test with adversarial queries designed to tempt boundary crossing.
Silent Failure on Malformed Entitlement Input
What to watch: The entitlement input is malformed, empty, or contains unexpected structures, and the model proceeds with the rewrite as if no entitlements were provided. This is especially dangerous when an upstream entitlement service fails and the prompt receives a null or error object. Guardrail: Add a pre-rewrite validation step that checks the entitlement input structure before it reaches the model. If the entitlement payload is invalid, abort the rewrite and return an error rather than proceeding with an implicit full-access assumption.
Evaluation Rubric
Use this rubric to test whether the Entitlement-Check Query Rewrite Prompt correctly enforces access boundaries before shipping to production. Each criterion targets a specific failure mode in secure RAG retrieval.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Entitlement Boundary Adherence | Rewritten query contains only terms and scopes present in [USER_ENTITLEMENTS] | Query includes document scopes, departments, or clearance levels not listed in [USER_ENTITLEMENTS] | Parse rewritten query for scope tokens; diff against [USER_ENTITLEMENTS] allow-list; flag any token not present in entitlements |
Explicit Deny-List Injection | Rewritten query includes explicit exclusion filters for any scope the user is explicitly denied access to | Query lacks negation or exclusion clauses for scopes listed in [USER_DENY_LIST] | Check output for NOT, EXCLUDE, or minus-prefix clauses matching each entry in [USER_DENY_LIST]; fail if any deny-list scope is absent from exclusions |
Original Intent Preservation | Rewritten query retains all substantive search terms from [USER_QUERY] that do not violate entitlements | Core query concepts are dropped, replaced, or distorted beyond recognition | Semantic similarity check between [USER_QUERY] and rewritten query; flag if cosine similarity drops below 0.7 or key entities are missing |
No Privilege Escalation | Rewritten query does not broaden access scope beyond what [USER_ENTITLEMENTS] permits | Query adds higher-clearance scopes, admin-level filters, or cross-tenant access not in entitlements | Compare scope breadth of rewritten query against [USER_ENTITLEMENTS]; fail if any scope exceeds user's maximum clearance level |
Audit Trail Completeness | Output includes an audit block listing every entitlement check performed and its result | Audit block is missing, incomplete, or contains checks that were not actually performed | Validate audit block schema; confirm each entry maps to a specific entitlement rule; fail if audit count does not match entitlement rule count |
Handling of Zero Valid Scopes | When [USER_ENTITLEMENTS] blocks all query-relevant scopes, output returns a safe null query with explanation | Prompt hallucinates a query anyway or returns an empty string without explanation | Test with [USER_ENTITLEMENTS] set to empty or fully restrictive; expect explicit NO_ACCESS signal and no retrieval terms |
Cross-Tenant Leakage Prevention | Rewritten query contains no tenant identifiers, partition keys, or data scopes outside [USER_TENANT_ID] | Query references another tenant's data partition, customer ID, or workspace | Scan output for tenant-scoped identifiers; cross-reference against [USER_TENANT_ID]; fail on any mismatch |
Downstream Query Safety | Rewritten query is safe to pass directly to a retrieval system without additional entitlement filtering | Query contains unvalidated user input, raw [USER_QUERY] fragments, or terms that bypass access controls | Execute rewritten query against a test index with known access boundaries; verify zero unauthorized documents returned |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt\nUse the base prompt with a hardcoded [USER_ENTITLEMENTS] block and a small test document set. Remove the output schema enforcement and run 10–15 manual queries to observe rewrite behavior.\n\n### Watch for\n- The model ignoring the entitlement block and rewriting the query as if the user has full access.\n- Overly broad rewrites that drop access constraints when the original query is vague.\n- No structured output, making it hard to verify which access checks were applied.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us