Inferensys

Prompt

User Role-Based Intent Detection Prompt for Query Rewriting

A practical prompt playbook for using User Role-Based Intent Detection Prompt for Query Rewriting in production AI workflows.
Developer doing prompt engineering on laptop, prompt variations visible on screen, casual coding session.
PROMPT PLAYBOOK

When to Use This Prompt

Define the job-to-be-done, the ideal user, required inputs, and the hard boundaries where this prompt should not be deployed.

This prompt is designed for enterprise RAG system architects and platform engineers who serve multiple user personas from a single knowledge base. The core job-to-be-done is role-aware query rewriting: taking a surface-level user question and a known user role (such as 'support engineer', 'product manager', or 'compliance officer') and producing a rewritten retrieval query that is scoped to the permissions, terminology, and information needs of that specific role. Without this step, a question like 'What is the status of project Alpha?' retrieves the same generic documents for every user, ignoring the fact that a support engineer needs open incident records while an executive needs a milestone summary.

You should use this prompt when your retrieval system has a defined set of user roles, each with distinct access scopes or information priorities, and when the same natural-language query should produce materially different retrieval results depending on who is asking. The prompt requires two inputs at minimum: the raw [USER_QUERY] and a structured [USER_ROLE_CONTEXT] object containing the role name, a brief description of that role's responsibilities, and any explicit permission boundaries or document scopes. Optionally, you can provide a [ROLE_TAXONOMY] listing all valid roles to prevent hallucinated role interpretations. Do not use this prompt when your knowledge base is public and unsegmented, when all users share identical access permissions, or when the query itself already contains explicit role-specific jargon that makes rewriting unnecessary. It is also inappropriate for anonymous or unauthenticated user sessions where no role context exists.

Before wiring this into production, define your evaluation harness. The primary success criterion is role-appropriate retrieval divergence: given the same surface question and two different roles, the rewritten queries must retrieve measurably different document sets that align with each role's scope. A secondary criterion is permission boundary preservation: the rewritten query must never expand retrieval scope beyond what the role is authorized to see. Common failure modes include role hallucination when the provided role context is vague, over-narrowing the query so it misses relevant cross-functional documents, and producing rewritten queries that leak permission-scope hints into the query string itself rather than keeping those as separate filter metadata. Always pair this prompt with a downstream metadata filter enforcement layer; the prompt suggests scope, but the retrieval system must enforce it.

PRACTICAL GUARDRAILS

Use Case Fit

Where this prompt works and where it does not. Use these cards to decide if role-based intent detection is the right tool for your retrieval pipeline.

01

Good Fit: Multi-Persona Knowledge Bases

Use when: A single knowledge base serves multiple user roles (e.g., engineer, support agent, compliance officer) and the same surface question should retrieve different content per role. Guardrail: Provide a closed, well-defined role taxonomy in the prompt. Open-ended role descriptions cause inconsistent intent shifts.

02

Good Fit: Permission-Aware Retrieval

Use when: Retrieval results must be scoped by role-based access controls and the query rewrite needs to embed permission hints for downstream filtering. Guardrail: Always separate the rewrite step from the enforcement step. The prompt should output scope hints, not make access decisions. Enforcement belongs in the retrieval layer.

03

Bad Fit: Single-Role Applications

Avoid when: All users share the same role and access scope. Role-based intent detection adds latency and complexity with no retrieval quality gain. Guardrail: Default to a simpler intent classifier or direct query rewrite. Add role awareness only when retrieval divergence between roles is measurable.

04

Bad Fit: Undefined or Dynamic Roles

Avoid when: User roles are not known at query time, change mid-session, or cannot be mapped to a stable taxonomy. The prompt will produce inconsistent rewrites. Guardrail: Require a validated role label as a required input. If role is unknown, fall back to a role-agnostic rewrite prompt and log the gap.

05

Required Inputs

What you need: A user query string, a validated role label from a known taxonomy, and a description of what each role is permitted or expected to retrieve. Guardrail: Validate the role input against an allowlist before it reaches the prompt. Reject unknown roles rather than letting the model guess intent from an arbitrary string.

06

Operational Risk: Role Leakage

Risk: The rewritten query may embed role-specific terminology that leaks access scope into logs, caches, or shared retrieval indices. Guardrail: Treat the rewritten query as sensitive metadata. Redact role hints from logs and avoid caching rewritten queries across roles. Test that different roles produce observably different retrieval queries from the same input.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt template for detecting user intent based on role context and rewriting queries for role-appropriate retrieval.

This prompt template is the core instruction set for a system that rewrites a user's query based on their organizational role. The goal is to take the same surface-level question—such as 'What's the status of Project Alpha?'—and produce different retrieval queries for an engineer, a product manager, and a finance lead. The template uses square-bracket placeholders for all dynamic inputs, making it ready to be wired into an application's prompt assembly pipeline. Before copying, ensure you have defined your role taxonomy, the knowledge base's permission model, and the expected output schema.

text
You are a query rewriting agent for an enterprise knowledge base. Your task is to detect the underlying intent of a user's question based on their role and rewrite the query to retrieve role-appropriate content.

## INPUT
User Role: [USER_ROLE]
User Query: [USER_QUERY]

## ROLE-INTENT MAPPING
Use the following role definitions to infer intent:
[ROLE_DEFINITIONS]

## OUTPUT SCHEMA
Return a valid JSON object with the following fields:
{
  "detected_intent": "string",
  "role_aware_query": "string",
  "permission_scope_hints": ["string"],
  "reasoning": "string"
}

## CONSTRAINTS
- The `role_aware_query` must be a rewritten string optimized for a vector or keyword search over the knowledge base.
- The `permission_scope_hints` must be a list of access control tags (e.g., document labels, team names) that a search engine can use to filter results.
- Do not answer the user's question. Only output the rewritten query and metadata.
- If the role is unknown or not in the provided mapping, set `detected_intent` to "general_inquiry" and do not apply role-specific scoping.

## EXAMPLES
[FEW_SHOT_EXAMPLES]

To adapt this template for your system, start by populating the [ROLE_DEFINITIONS] placeholder with a structured mapping of roles to their typical intents and information needs. For example, an 'Engineer' role might map to intents like 'debugging', 'implementation_detail', or 'system_status', while a 'Sales' role maps to 'deal_status', 'customer_history', or 'competitive_positioning'. The [FEW_SHOT_EXAMPLES] placeholder is critical for consistent behavior; provide at least three examples that show the same query being rewritten for different roles. The [USER_QUERY] and [USER_ROLE] placeholders should be injected by your application at runtime. If your knowledge base uses a specific taxonomy for access control, ensure the permission_scope_hints output aligns with those exact tags to enable automated filtering in your retrieval pipeline. For high-stakes deployments, add a [RISK_LEVEL] constraint that forces the model to flag queries that might cross permission boundaries, triggering a human review step before retrieval.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the User Role-Based Intent Detection Prompt. Each variable must be populated before the prompt is sent to the model. Validation notes describe how to programmatically check the input quality.

PlaceholderPurposeExampleValidation Notes

[USER_QUERY]

The raw, unmodified question from the end-user.

What is the current status of the Acme project?

Check string length > 1 and < 2000 chars. Reject empty or whitespace-only input. Log if query contains only stop words.

[USER_ROLE]

The authenticated persona or job function of the user making the request.

field_engineer

Must match an entry in the authorized roles enum: [admin, field_engineer, project_manager, compliance_officer, read_only_guest]. Reject unknown roles.

[ROLE_DEFINITIONS]

A structured mapping of each role to its access scope, terminology, and retrieval priorities.

{"field_engineer": {"access_level": "technical_specs", "priority": "implementation_details"}}

Parse as JSON. Validate that every role in [USER_ROLE] has a definition. Schema check for required keys: access_level, priority.

[KNOWLEDGE_BASE_DESCRIPTOR]

A brief description of the corpus content and structure to ground the rewrite.

Internal engineering wiki, project plans, and Jira ticket summaries.

Check string length > 10. Ensure it describes the source material, not the user. Log a warning if it contains PII-like patterns.

[OUTPUT_SCHEMA]

The exact JSON schema the model must use for its response.

{"type": "object", "properties": {"detected_intent": {"type": "string"}, "rewritten_query": {"type": "string"}}}

Parse as valid JSON Schema. Validate that it includes required fields: detected_intent, rewritten_query, permission_scope_hint. Reject if schema is not an object type.

[FEW_SHOT_EXAMPLES]

A set of 2-3 examples demonstrating how the same query should be rewritten for different roles.

Query: 'project timeline', Role: 'project_manager' -> Rewrite: 'Acme project Gantt chart and milestone dates'

Validate as a JSON array of objects with keys: query, role, rewritten_query. Check that at least 2 distinct roles are represented. Ensure examples do not contain PII.

[MAX_QUERY_LENGTH]

An integer constraint to limit the length of the generated rewritten query.

256

Check that the value is an integer between 50 and 1000. If not set, default to 256. Log if the model output exceeds this limit for retry or truncation.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the role-based intent detection prompt into a production RAG pipeline with validation, logging, and permission-aware retrieval.

This prompt is not a standalone chatbot. It is a pre-retrieval transformer that must sit between the user input and your retrieval system. The application layer is responsible for injecting the correct [USER_ROLE] and [PERMISSION_SCOPE] from your identity provider or session context before calling the model. The prompt output—a rewritten query and permission-scope hints—should be passed directly to your retrieval backend as filter constraints, not displayed to the user. Treat the permission_scope_hints field as structured metadata that your vector database or search engine uses to enforce document-level access control at query time.

Validation and retry logic: Parse the model output as JSON and validate that the rewritten_query field is a non-empty string and that permission_scope_hints is an array of strings matching your known access control labels. If the model returns malformed JSON, retry once with a stricter output constraint appended to the prompt. If the intent_shift_summary is empty but the query was rewritten, accept the output but log a warning. Do not proceed to retrieval if rewritten_query is missing or identical to the raw user input without any role adaptation—this indicates the model failed to apply role context and should trigger a fallback to the original query with a default broad-scope filter.

Model choice and latency budget: This prompt works best with models that have strong instruction-following and JSON output discipline, such as GPT-4o, Claude 3.5 Sonnet, or Gemini 1.5 Pro. Expect 200-500ms latency for this pre-retrieval step. If your retrieval pipeline has a strict sub-100ms budget, consider caching rewritten queries by (raw_query, user_role) pairs for frequently asked questions from the same role. For high-throughput systems, batch role-identical queries where possible, but never batch across different roles—the whole point is role-specific rewriting.

Logging and observability: Log the raw user query, injected role, rewritten query, intent shift summary, and permission scope hints as a single trace span. This is critical for debugging retrieval failures: if a user says 'I can't find the policy,' you need to know whether the role was correctly detected, whether the query was rewritten appropriately, and whether the permission scope hints excluded the right documents. Attach the intent detection confidence to your retrieval metrics so you can correlate intent misclassification with answer quality degradation.

Human review gates: For enterprise deployments where role misclassification could expose sensitive documents, add a confidence threshold check. If the model's intent shift summary contains hedging language like 'possibly' or 'unclear,' or if the permission scope hints include a catch-all wildcard, route the query to a human reviewer or apply the most restrictive scope as a safe default. Never use the permission scope hints as the sole access control mechanism—your retrieval backend must still enforce document-level ACLs independently. The prompt's scope hints are an optimization to reduce retrieval noise, not a security boundary.

IMPLEMENTATION TABLE

Expected Output Contract

Defines the exact JSON fields, types, and validation rules for the role-aware intent detection and query rewriting prompt. Use this contract to parse, validate, and integrate the model's response into your retrieval pipeline.

Field or ElementType or FormatRequiredValidation Rule

detected_intent

string

Must match one of the predefined intent labels in [INTENT_TAXONOMY]. If confidence is below [CONFIDENCE_THRESHOLD], set to 'ambiguous'.

confidence_score

number (0.0-1.0)

Must be a float between 0.0 and 1.0. If below [CONFIDENCE_THRESHOLD], the requires_clarification flag must be true.

role_aware_query

string

Must be a non-empty string. Must incorporate role-specific terminology from [USER_ROLE] and [ROLE_CONTEXT]. Must not be identical to the original [USER_QUERY].

permission_scope_hints

array of strings

If present, each string must match a valid access control tag from [PERMISSION_TAGS]. Use null or omit if no role-specific permission constraints are detected.

requires_clarification

boolean

Must be true if confidence_score is below [CONFIDENCE_THRESHOLD] or if multiple conflicting intents are detected. Otherwise false.

clarification_question

string or null

Required if requires_clarification is true. Must be a direct question addressed to the user. Must be null if requires_clarification is false.

role_specific_entities

array of objects

Each object must have 'entity' (string) and 'canonical_form' (string). Use to map user terms to role-specific canonical entities from [ENTITY_MAP]. Omit or use empty array if none found.

PRACTICAL GUARDRAILS

Common Failure Modes

Role-based intent detection fails in predictable ways. These are the most common production failure modes and how to guard against them before they degrade retrieval quality.

01

Role Context Overrides Query Semantics

What to watch: The prompt over-weights the role label and rewrites the query to match role stereotypes rather than the user's actual words. An engineer asking 'What is the refund policy?' gets a technical API refund query instead of a customer-facing policy query. Guardrail: Always anchor the rewrite to the original query tokens. Require the model to justify any semantic departure from the surface question in a reasoning field before producing the final rewrite.

02

Permission Scope Leakage

What to watch: The rewritten query embeds permission-scope hints that are too broad or too narrow, causing the retriever to surface documents the user should not see or to hide documents they need. A manager role hint like 'retrieve strategic content' may expose pre-release roadmaps. Guardrail: Validate permission-scope hints against a known access control list before retrieval. If the hint cannot be mapped to an existing permission group, fall back to a role-agnostic query rewrite.

03

Role Ambiguity for Overlapping Personas

What to watch: Users with multiple roles or hybrid responsibilities produce inconsistent rewrites. A technical product manager may be classified as either 'engineer' or 'product' on different calls, causing retrieval jitter. Guardrail: Support multi-role input. If multiple roles are supplied, generate a separate candidate query per role and merge or rank results. Log which role produced which rewrite for debugging.

04

Missing Role Context Causes Generic Fallback

What to watch: When the role field is empty, null, or unrecognized, the prompt produces a generic rewrite that performs worse than a purpose-built intent detection prompt without role awareness. The role feature becomes a liability when context is missing. Guardrail: Test the prompt explicitly with an empty role input. The output must degrade gracefully to a standard intent-based rewrite, not produce a garbled or overly cautious query. Include a 'role_available' boolean in the output for downstream routing decisions.

05

Role-Specific Terminology Hallucination

What to watch: The model invents domain-specific jargon or entity names that do not exist in the knowledge base because it associates certain terms with a role. A finance role rewrite may inject 'EBITDA reconciliation' into a query about quarterly results when the knowledge base uses different terminology. Guardrail: Cross-reference rewritten query terms against a known term index or entity catalog. Flag novel terms for human review or strip them before retrieval. Include a 'term_confidence' field for each injected domain term.

06

Role Bias Amplifies Retrieval Echo Chambers

What to watch: The role-aware rewrite consistently steers retrieval toward content that confirms role-specific expectations, suppressing contradictory or cross-functional evidence. An engineering role query about system reliability may never retrieve customer-impact reports. Guardrail: For high-stakes queries, generate a second role-agnostic rewrite and compare result sets. If the role-specific results diverge significantly from the agnostic baseline, surface both sets with a divergence flag for human review.

IMPLEMENTATION TABLE

Evaluation Rubric

Use this rubric to test whether the User Role-Based Intent Detection Prompt produces safe, role-appropriate, and correctly scoped query rewrites before deploying to production.

CriterionPass StandardFailure SignalTest Method

Role-Aware Query Scoping

Different roles produce materially different rewritten queries from the same surface input, reflecting role-appropriate retrieval needs.

All roles produce identical or near-identical rewrites, indicating the prompt ignored the [USER_ROLE] input.

Run the same [USER_QUERY] with 3 distinct roles. Assert that the resulting query strings differ in terminology, scope, or constraints.

Permission-Scope Hint Accuracy

The output [PERMISSION_SCOPE_HINTS] align with the role's expected access level and do not suggest escalation beyond the role's privileges.

Hints suggest retrieving admin-level or out-of-scope content for a restricted role, or fail to include necessary scope constraints.

Define a permission matrix for test roles. For each role, check that hints do not include forbidden scopes and do include required restrictions.

Intent Preservation Under Role Shift

The core user intent is preserved even when the query is rewritten for a specific role; no user requirements are dropped.

The rewritten query for a role omits a key entity, constraint, or question component present in the original [USER_QUERY].

Decompose the original query into required semantic components. Assert that each component is represented in every role's rewritten query.

Role Metadata Extraction

The prompt correctly extracts and normalizes the [USER_ROLE] into a canonical role identifier used consistently in the output.

The output uses a different role name than the input, invents a role, or conflates multiple roles.

Provide roles with typos or synonyms. Assert that the output [DETECTED_ROLE] maps to the correct canonical role from a predefined taxonomy.

Output Schema Compliance

The output is valid JSON matching the [OUTPUT_SCHEMA] with all required fields present and correctly typed.

Output is missing required fields, contains extra untyped fields, or is not parseable JSON.

Validate output against the JSON Schema definition. Assert no missing required fields and correct types for [REWRITTEN_QUERY], [PERMISSION_SCOPE_HINTS], and [DETECTED_ROLE].

Role Conflict Handling

When [USER_QUERY] conflicts with [USER_ROLE] permissions, the output flags the conflict and rewrites the query to the closest permissible scope.

The prompt either refuses entirely for a valid edge-case query or generates a query that violates the role's permission boundary.

Submit a query requesting restricted data for a low-privilege role. Assert that the output includes a conflict flag and a rewritten query scoped to permissible content.

Confidence Calibration

The output [CONFIDENCE_SCORE] is high when role and intent are unambiguous and low when the query could reasonably belong to multiple roles.

Confidence is always high or always low regardless of input ambiguity, or the score is missing.

Run a set of ambiguous and unambiguous query-role pairs. Assert that unambiguous pairs score above 0.8 and ambiguous pairs score below 0.6.

Idempotency and Stability

The same input produces semantically equivalent output across multiple runs, with stable role detection and query rewriting.

Role detection flips between runs, or the rewritten query changes meaning substantially across identical calls.

Run the same input 5 times. Assert that [DETECTED_ROLE] is identical and that [REWRITTEN_QUERY] has high semantic similarity across runs.

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a single hardcoded role taxonomy and lighter output validation. Start with two or three roles (e.g., admin, viewer, support) and a small set of test queries. Skip structured logging and eval harnesses initially.

code
[ROLE_TAXONOMY]: ["admin", "viewer", "support"]
[USER_QUERY]: [INPUT]
[USER_ROLE]: [ROLE]

Watch for

  • Role taxonomy drift when adding new personas
  • Overly broad rewrites that lose the original query's specificity
  • No confidence signal, so bad rewrites pass silently
Prasad Kumkar

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.