We often think of prompt engineering as crafting creative instructions to elicit novel outputs from LLMs, but its real power lies in making LLMs reliable, and that starts with grounding their answers in verifiable sources.
Let’s see this in action. Imagine we’re building a customer support bot that needs to answer questions about product return policies. Without grounding, a model might hallucinate a policy that doesn’t exist.
User Query: "What’s your return policy for electronics?"
Un-grounded Prompt:
You are a helpful customer support assistant. Answer the user's question.
Potential (Bad) LLM Output: "Our return policy for electronics allows returns within 30 days of purchase with a valid receipt. A 15% restocking fee may apply to opened items."
This sounds plausible, but what if the actual policy is 15 days for electronics, and opened items are non-refundable?
Now, let’s ground the same query. We’ll feed the LLM relevant snippets of our actual return policy document.
Grounded Prompt:
You are a helpful customer support assistant.
Answer the user's question based ONLY on the following document snippets:
---
**Document Snippets:**
**Return Policy - General:**
All returns must be initiated within 15 days of the purchase date. Items must be in their original packaging and condition. A valid receipt or order number is required for all returns.
**Return Policy - Electronics:**
Electronics, including but not limited to laptops, TVs, and smartphones, can be returned within 15 days of the purchase date. Opened electronic items are eligible for return only if defective. A 15% restocking fee applies to all opened, non-defective electronics.
---
**User Query:** "What's your return policy for electronics?"
Grounded LLM Output: "Our return policy for electronics allows returns within 15 days of the purchase date. Opened electronic items can be returned if they are defective. A 15% restocking fee applies to all opened, non-defective electronics."
This output is accurate because it’s derived directly from the provided text. The key here is constraint. We’re not asking the LLM to know the policy; we’re asking it to report on the policy it’s given.
The mental model for grounded responses involves a three-part process:
- Retrieval: Before the LLM even sees the prompt, a retrieval system (often a vector database or a search index) finds the most relevant pieces of information from a larger knowledge base based on the user’s query. This is like an LLM’s librarian.
- Augmentation: The retrieved snippets are then injected into the LLM’s prompt, usually within a specific section marked for context or document content. This is the "context window" filling up.
- Generation: The LLM processes the augmented prompt, using the provided snippets as its sole source of truth to formulate the answer. It’s instructed to adhere strictly to this context.
The levers you control are primarily in the retrieval and augmentation stages.
- Knowledge Base Quality: The accuracy and completeness of the documents you feed into your retrieval system are paramount. If your return policy document is outdated or incorrect, the grounded answer will also be incorrect.
- Retrieval Strategy: How good is your system at finding the right snippets? This involves choosing the right embeddings, chunking strategy for your documents, and the similarity search algorithm. For example, a simple keyword search might miss relevant information if the query uses synonyms.
- Prompting for Citation: You can explicitly ask the LLM to cite its sources.
Grounded Prompt with Citation Request:
You are a helpful customer support assistant.
Answer the user's question based ONLY on the following document snippets.
If the answer is not found in the snippets, state that you cannot find the information.
Cite the document section(s) from which you derived your answer using the format "[Document Section Name]".
---
**Document Snippets:**
**Return Policy - General:**
All returns must be initiated within 15 days of the purchase date. Items must be in their original packaging and condition. A valid receipt or order number is required for all returns.
**Return Policy - Electronics:**
Electronics, including but not limited to laptops, TVs, and smartphones, can be returned within 15 days of the purchase date. Opened electronic items are eligible for return only if defective. A 15% restocking fee applies to all opened, non-defective electronics.
---
**User Query:** "What's your return policy for electronics?"
Grounded LLM Output with Citation: "Our return policy for electronics allows returns within 15 days of the purchase date [Return Policy - Electronics]. Opened electronic items can be returned if they are defective [Return Policy - Electronics]. A 15% restocking fee applies to all opened, non-defective electronics [Return Policy - Electronics]."
This adds a crucial layer of trust and auditability. The LLM isn’t just answering; it’s showing its work.
A common pitfall is relying solely on the LLM to "understand" the relationship between the query and the context. Even with grounding instructions, LLMs can sometimes misinterpret or over-extrapolate from the provided text. The prompt must be explicit about using only the provided text and how to use it (e.g., "based ONLY on," "cite your sources"). The quality of the retrieval system is often a more significant factor in the final answer’s accuracy than subtle prompt tuning.
The next step after ensuring grounded, cited answers is implementing a system for handling questions where the answer doesn’t exist in the provided documents.