This prompt rewrites a user's natural language query to incorporate their organizational role, department-specific terminology, and access boundaries before it hits a retrieval engine. The core job-to-be-done is ensuring that the same surface question—such as 'show me the latest product specs'—returns different documents for a sales engineer, a compliance officer, and a support agent, even when they query the same knowledge base. The ideal user is an enterprise RAG developer or search infrastructure engineer who already has a validated user role object and a defined permission model available at inference time. Without this role context, the prompt cannot perform its function, and you should not use it as a general-purpose query expander.
Prompt
Role-Scoped Query Rewriting Prompt Template

When to Use This Prompt
Defines the precise job this prompt performs, the required context, and the boundaries where a different prompt is needed.
Use this prompt when you need to inject role-specific terminology (e.g., 'pricing sheet' vs. 'rate card'), scope constraints (e.g., 'only North America region docs'), and access boundaries (e.g., 'up to confidentiality level 3') into a query. It assumes the user's role and permissions are resolved upstream and passed in as structured input. If you need to extract permissions or access constraints directly from the user's natural language query, use the Permission-Aware Metadata Filter Generation Prompt. If your primary challenge is resolving anaphora or ellipsis across conversation turns, use the History-Conditioned Query Reformulation Prompt. This prompt is not a synonym expander, a session-context resolver, or a general query decomposition tool. It does one thing: scope a query to a role.
Before wiring this into production, confirm that your application layer can supply a validated role object with at least a role name, department, and access level. The prompt will fail silently or produce unhelpful rewrites if these fields are missing, stale, or incorrect. For high-stakes deployments—such as legal, financial, or healthcare retrieval—add a post-rewrite validation step that checks whether the rewritten query contains terms or scope constraints that exceed the user's documented permissions. A rewritten query that accidentally broadens access is a more dangerous failure mode than one that is overly restrictive. Start with a narrow set of roles and departments, evaluate the rewrite quality with role-specific test queries, and expand coverage only after you have evidence that the rewrites are both permission-safe and retrieval-effective.
Use Case Fit
Where Role-Scoped Query Rewriting works well and where it introduces risk. Use these cards to decide if this prompt template fits your retrieval architecture.
Good Fit: Enterprise RAG with Role-Based Access
Use when: your retrieval corpus contains documents with varying sensitivity levels and users have clearly defined roles, departments, or clearance tiers. Why: the prompt injects role-specific terminology and scope constraints so retrieval returns only authorized documents. Guardrail: validate that the role-to-terminology mapping is maintained and updated when organizational structures change.
Bad Fit: Public-Facing Search Without Authentication
Avoid when: all users have identical access to the same document corpus with no permission boundaries. Why: role-scoped rewriting adds unnecessary complexity and latency without improving retrieval quality. Guardrail: fall back to a simpler query expansion or decomposition prompt when no user profile is available.
Required Inputs: User Profile and Permission Map
What you need: a structured user profile containing role, department, clearance level, and access scope boundaries. Why: the prompt cannot inject role-appropriate constraints without knowing what the user is allowed to see. Guardrail: implement a profile freshness check to prevent stale permissions from leaking into query rewrites.
Operational Risk: Over-Constraining Retrieval
What to watch: role-scoped rewrites can inject overly restrictive filters that exclude relevant documents the user is authorized to see. Why: conservative permission mapping may interpret scope boundaries too narrowly. Guardrail: monitor zero-result rates by role and implement query relaxation fallbacks when strict role filters produce empty result sets.
Operational Risk: Permission Boundary Leakage
What to watch: the rewritten query may inadvertently include terms or filters that cross permission boundaries, exposing document scopes the user should not access. Why: ambiguous role mappings or conflicting department terminology can produce over-broad rewrites. Guardrail: run post-rewrite validation against the user's explicit permission list before executing retrieval.
Operational Risk: Role Terminology Drift
What to watch: role-specific terminology injected into queries becomes stale as organizational language evolves. Why: teams rename, restructure, or adopt new jargon that the prompt's static mapping does not reflect. Guardrail: schedule regular audits of role-to-terminology mappings and log query rewrites to detect when injected terms no longer match corpus vocabulary.
Copy-Ready Prompt Template
A ready-to-adapt prompt template that rewrites a user query to incorporate role-specific terminology, scope constraints, and access boundaries.
This template provides a complete system and user message pair for role-scoped query rewriting. The system prompt establishes the rewriting policy, instructing the model to act as a retrieval pre-processor that injects role-appropriate terminology and enforces access boundaries. The user message supplies the raw query, the user's role profile, and the output schema. Replace every square-bracket placeholder with your application's runtime values before sending the request to the model.
textSYSTEM: You are a retrieval query rewriter for an enterprise knowledge base with role-based access controls. Your job is to rewrite a user's raw query so that retrieval returns documents the user is authorized to see and that match their role-specific terminology. Follow these rules: 1. Inject role-appropriate synonyms, acronyms, and domain terms from the user's [ROLE_TERMINOLOGY_MAP]. 2. Add explicit scope constraints derived from the user's [ACCESS_BOUNDARIES] (e.g., department, project, clearance level). 3. Do not introduce terms that would expand the query beyond the user's authorized document scope. 4. Preserve the original query intent. Do not answer the query. 5. If the original query contains terms that conflict with the user's access boundaries, rewrite to the closest authorized equivalent or flag the conflict. 6. Output only the rewritten query string in the specified JSON format. USER: Raw Query: [USER_QUERY] User Role: [USER_ROLE] Department: [USER_DEPARTMENT] Access Boundaries: [ACCESS_BOUNDARIES] Role Terminology Map: [ROLE_TERMINOLOGY_MAP] Output must be valid JSON matching this schema: { "rewritten_query": "string", "injected_terms": ["string"], "scope_constraints_applied": ["string"], "conflicts_flagged": ["string"] }
To adapt this template, start by defining your [ROLE_TERMINOLOGY_MAP] as a structured dictionary mapping user roles or departments to canonical terms, acronyms, and synonyms. The [ACCESS_BOUNDARIES] placeholder should receive a list of explicit scope constraints such as ["department:finance", "clearance:internal-only", "project:acme-q4"]. In production, these values should be assembled server-side from your identity and entitlements system—never accept them directly from the client. Before deploying, run the prompt against a golden dataset of role-query pairs and validate that injected_terms are role-appropriate and scope_constraints_applied match the user's actual permissions. If your application handles regulated data, add a human review step for any rewrite where conflicts_flagged is non-empty.
Prompt Variables
Inputs the prompt needs to work reliably. Validate each before constructing the prompt.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[USER_QUERY] | The original user question or search string to be rewritten. | Show me the Q3 revenue numbers for my division. | Required. Must be a non-empty string. Reject null or whitespace-only input before prompt assembly. |
[USER_ROLE] | The canonical role identifier for the current user, used to select terminology and scope constraints. | sales_manager_west | Required. Must match an entry in the authorized roles registry. Validate against an allowlist before injection. |
[USER_PERMISSIONS] | A structured list of document scopes, data classifications, or access boundaries the user is authorized to see. | ["sales:west", "revenue:internal_only", "classification:pii_allowed"] | Required. Must be a non-empty array of permission strings. Validate each string against the organization's permission catalog. |
[ROLE_TERMINOLOGY_MAP] | A dictionary mapping generic terms to role-specific jargon, acronyms, or controlled vocabulary preferred by the user's department. | {"revenue": "bookings", "client": "account", "Q3": "FY24Q3"} | Optional. If provided, must be a valid JSON object. If null, the prompt should fall back to the original query terms without expansion. |
[ACCESS_BOUNDARY_STATEMENT] | A natural-language description of the maximum retrieval scope allowed for this user, derived from permissions. | User is authorized to access sales data for the West region only, including internal revenue reports. | Required. Must be a non-empty string generated from [USER_PERMISSIONS]. Validate that it does not expand scope beyond the permission list. |
[RECENT_USER_HISTORY] | A compact summary of the user's last N queries or document interactions to resolve ambiguous references. | Previously viewed: West Region FY24 Forecast. Last query: 'breakdown by product line'. | Optional. If provided, must be a string under 500 tokens. Validate that history entries are within the current session and not stale. |
[OUTPUT_SCHEMA] | The expected JSON structure for the rewritten query and its metadata. | {"rewritten_query": "string", "injected_terms": ["string"], "scope_constraints": ["string"]} | Required. Must be a valid JSON Schema object. Validate that the schema includes required fields for the rewritten query and scope constraints. |
Implementation Harness Notes
How to wire the role-scoped query rewriting prompt into a secure, observable retrieval pipeline.
This prompt is not a standalone chatbot. It is a preprocessing step inside a retrieval pipeline. The application layer must supply the user's role, access scope, and query before calling the model, and it must validate the rewritten query before it touches any retrieval index. Treat the rewritten output as untrusted input to your search system—never pass it directly to a database or vector store without post-processing.
Integration pattern. Place this prompt between the user input handler and the retrieval orchestrator. The application should: (1) resolve the user's identity and fetch their role, group memberships, and permission boundaries from your identity provider or access control system; (2) assemble the prompt with [USER_QUERY], [USER_ROLE], [ACCESS_SCOPE], and [ORGANIZATION_TERMINOLOGY]; (3) call the model with temperature=0 and a low max_tokens ceiling (200–400 tokens is usually sufficient for a rewritten query string); (4) parse the output and validate that the rewritten query does not introduce terms or scopes outside the user's permission boundary; (5) log the original query, rewritten query, role, and validation result for audit; (6) pass only the validated rewritten query to your retrieval backend. For high-risk domains, insert a human approval step before retrieval when the rewrite introduces new scope constraints or when the validator flags a boundary violation.
Validation and guardrails. Implement a post-generation validator that checks: (a) the rewritten query does not contain permission escalation markers (e.g., terms like all, unrestricted, admin unless explicitly allowed by the user's role); (b) any injected metadata filters or scope constraints are a subset of the user's granted access; (c) the output is structurally a single query string, not an explanation or refusal. If validation fails, log the failure, fall back to the original user query with strict permission filters applied, and alert the operations team. For retrieval systems using structured filter clauses, parse the rewritten query to extract any filter directives and cross-reference them against the user's access control list before constructing the final search request.
Model choice and latency. This prompt is latency-sensitive because it sits on the critical path of every user search. Use a fast, instruction-tuned model (e.g., GPT-4o-mini, Claude Haiku, or a fine-tuned small model) rather than a large reasoning model. Cache frequent [ORGANIZATION_TERMINOLOGY] blocks per role to avoid redundant token processing. If your identity provider supports it, cache the assembled user profile context with a short TTL (e.g., 5 minutes) to reduce per-request assembly overhead. Monitor rewrite latency and set a timeout—if the model call exceeds 500ms, fall back to the original query with permission filters applied.
Observability and eval. Log every rewrite with the original query, rewritten query, user role, access scope, model version, and validation result. Use these logs to build a regression test suite: sample 100–200 real user queries across roles, manually verify that rewrites respect permission boundaries, and store them as golden test cases. Run this suite on every prompt or model change. Set up an eval that measures two failure modes: permission leakage (the rewrite introduces terms or scopes the user should not access) and over-restriction (the rewrite strips legitimate access). Both are production-regression signals that should block a deploy.
Expected Output Contract
Validation rules for the rewritten query object returned by the role-scoped query rewriting prompt. Use this contract to parse, validate, and route the model output before retrieval execution.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
rewritten_query | string | Must be non-empty. Must differ from [ORIGINAL_QUERY] in at least one token. Must not contain placeholder tokens from the prompt template. | |
injected_role_terms | array of strings | Each term must appear in [ROLE_TERMINOLOGY_LIST]. Array length must be >= 1. No duplicate terms allowed. | |
access_scope_constraints | object | Must contain 'allow_scopes' (array of strings) and 'deny_scopes' (array of strings). At least one scope must be present across both arrays. No scope may appear in both arrays. | |
permission_boundary_check | boolean | Must be true. If false, reject the output and trigger a retry or escalation. This is a self-attestation field that must be cross-validated against [USER_PERMISSION_PROFILE]. | |
query_intent | string | Must match one of the allowed intent labels defined in [INTENT_TAXONOMY]. Enum check required before routing. | |
confidence_score | float between 0.0 and 1.0 | Must be >= [MIN_CONFIDENCE_THRESHOLD]. If below threshold, route to human review queue. Null not allowed. | |
disambiguation_notes | string or null | If not null, must contain a non-empty explanation of any ambiguous terms resolved during rewriting. Null allowed when no ambiguity was detected. |
Common Failure Modes
Role-scoped query rewriting fails in predictable ways. These cards cover the most common production failure modes, why they happen, and how to guard against them before they degrade retrieval quality or cause permission violations.
Role Terminology Over-Injection
What to watch: The rewrite injects role-specific jargon that narrows retrieval too aggressively, excluding relevant documents that use different terminology. A finance user searching for 'revenue' gets a rewrite that forces 'recognized revenue per ASC 606' and misses documents using 'sales income' or 'top-line growth.' Guardrail: Implement a recall-penalty check that flags rewrites where injected terms have zero or near-zero match rates against the target index. Maintain a role-to-synonym map that includes broader alternatives alongside canonical terms. Test rewrites against a golden set of queries where over-specialization is a known risk.
Permission Boundary Leakage
What to watch: The rewrite expands a query in ways that cross permission boundaries, either by introducing terms associated with restricted document scopes or by relaxing constraints that were implicitly enforcing access limits. A user with domestic-only access searches for 'customer data' and the rewrite adds 'global customer records' as an expansion term. Guardrail: Run every rewritten query through a permission-aware term validator before retrieval execution. Maintain an explicit deny-list of terms, entities, and scopes that must never appear in queries for specific roles. Log and alert on any rewrite that introduces a deny-listed term. Include adversarial test cases that probe boundary conditions.
Stale Profile Context Poisoning
What to watch: The rewrite incorporates outdated user profile data—old role assignments, stale preferences, or expired access grants—causing retrieval to reflect who the user was rather than who they are. A user who changed departments last week still gets rewrites scoped to their previous team's terminology and document access. Guardrail: Attach a profile_freshness_threshold to every rewrite request and reject rewrites when the user profile's last update exceeds the threshold. Implement a profile version token in the rewrite context so stale profiles are detectable. Build eval cases that simulate role changes and verify that rewrites reflect the current profile, not the cached one.
Over-Constrained Query Collapse
What to watch: The rewrite layers role constraints, permission filters, and scope boundaries so aggressively that the resulting query returns zero or near-zero results. A user with a narrow access scope searches for a broad topic and the rewrite produces a query so constrained that no documents match. Guardrail: Implement a pre-retrieval result-count estimator that checks whether the rewritten query's constraints are likely to produce an empty result set. If the estimate is below a minimum threshold, trigger a query relaxation path that progressively removes non-essential constraints while preserving permission boundaries. Log every zero-result event with the original query, rewritten query, and applied constraints for audit.
Cross-Role Term Collision
What to watch: The rewrite injects terminology that has different meanings across roles or departments, causing retrieval to surface documents intended for a different audience. The term 'compliance' for a legal user means regulatory filings, but for an engineering user it means code standards—and the rewrite picks the wrong sense. Guardrail: Maintain a role-disambiguated term registry that maps ambiguous terms to their role-specific meanings and preferred document scopes. When a term has multiple role-specific senses, the rewrite must either select the correct sense based on the user's role or generate multiple candidate queries with explicit scope labels. Test with a collision matrix of ambiguous terms across all active roles.
Implicit Access Assumption Drift
What to watch: The rewrite makes assumptions about what the user is allowed to see based on role patterns rather than explicit permission checks, gradually drifting toward broader access than the user actually has. Over time, rewrites for 'manager' roles start including executive-level document scopes because the pattern associates management with broad access. Guardrail: Require every rewrite to reference explicit permission tokens, not role-based heuristics. Run periodic audits comparing rewritten queries against actual permission boundaries to detect drift. Implement a permission-boundary regression test suite that runs on every prompt change and flags any rewrite that accesses scopes beyond the user's explicit entitlements.
Evaluation Rubric
Run these checks against a golden dataset of 50-100 query-role pairs to validate the prompt before deployment. Each criterion targets a specific failure mode in role-scoped query rewriting.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Role-Appropriate Term Injection | Rewritten query contains at least one domain term from the user's role profile when the original query is ambiguous | Rewritten query uses generic terms or terms from a different role profile | Keyword overlap check between rewritten query and role-specific terminology list from golden dataset |
Permission Boundary Adherence | Rewritten query introduces no terms or constraints that would retrieve documents outside the user's explicit access scope | Rewritten query includes a department, project, or classification term the user's role cannot access | Parse rewritten query for filter clauses; cross-reference each constraint against the user's permission allow-list in the golden dataset |
Original Intent Preservation | Rewritten query retrieves the same top-3 relevant documents as the original query when tested against an unfiltered index | Rewritten query changes the semantic meaning such that top-3 results diverge completely from original query results | Cosine similarity between original and rewritten query embeddings exceeds 0.85; manual spot-check on 10% of divergent cases |
Scope Constraint Explicitness | Rewritten query includes at least one explicit scope constraint when the user's role has restricted access | Rewritten query contains no scope-limiting language despite the user having a constrained role | Regex match for scope constraint patterns; compare against expected constraint presence in golden dataset labels |
No Over-Permission Expansion | Rewritten query does not broaden the retrieval scope beyond what the original query plus role permits | Rewritten query adds generalizing terms that would pull in documents from unauthorized scopes | Diff the set of document IDs retrieved by original query vs. rewritten query under the user's permission filter; flag any net-new scope expansion |
Role-Irrelevant Term Avoidance | Rewritten query contains no terms that are irrelevant or misleading given the user's role context | Rewritten query injects terminology from a different department or function that would confuse retrieval | LLM-as-judge pairwise comparison: rewritten query scored against a rubric for role-context relevance; fail if score below 4/5 |
Structured Filter Merge Correctness | Any structured metadata filters extracted from the query merge correctly with role-based filters without conflict | Output contains contradictory filter values or drops a required role-based filter clause | Schema validation of the output filter object; assert no key-value conflicts between query-derived and role-derived filters |
Empty Query Handling | When the input query is empty or pure noise, the rewritten query defaults to a safe role-scoped retrieval baseline | Rewritten query is empty, hallucinates a query, or produces an unconstrained broad retrieval | Input empty string; assert output is non-empty and contains only terms from the user's role profile allow-list |
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
Use the base prompt with a single hardcoded role and lighter validation. Replace [USER_ROLE] with a fixed string like "engineer" and [ACCESS_SCOPES] with a simple list. Skip structured output enforcement initially.
codeYou are a query rewriter for an internal knowledge base. The user's role is: "engineer" The user can access documents in these scopes: ["engineering", "product"] Rewrite the following user query to incorporate role-appropriate terminology and scope constraints: User query: [USER_QUERY] Rewritten query:
Watch for
- Missing scope boundaries causing over-retrieval
- Role terminology injection that sounds plausible but is wrong
- No structured output, making downstream parsing fragile

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