Understanding why hallucination happens
When Meilisearch sends retrieved documents to the LLM, the model may:- Fill gaps in the provided context with its own training data
- Misinterpret ambiguous information in the documents
- Combine facts from different documents in incorrect ways
- Generate plausible-sounding but fabricated details
System prompt engineering
The system prompt is your first and most important line of defense. A well-crafted system prompt sets clear boundaries for the model.Be explicit about data boundaries
Specify how to handle uncertainty
Require source attribution
Forcing the model to cite its sources makes it harder to hallucinate, because fabricated information has no source to point to:Few-shot prompting
Few-shot prompting provides the model with examples of correct behavior directly in the system prompt. This is one of the most effective techniques for reducing hallucination.Show the model what good answers look like
Include 2-3 examples in your system prompt that demonstrate the expected behavior:Show the model what to avoid
Negative examples are equally powerful. Show the model what a hallucinated answer looks like:Guardrails in Meilisearch Cloud
Meilisearch Cloud provides built-in guardrail options through the workspace settings. These guardrails work by injecting carefully crafted instructions into the system prompt to guide the model’s behavior.Guardrails are prompt-based, meaning they shape the model’s behavior through instructions rather than through hard technical constraints. They significantly improve response quality but should be combined with monitoring for production use.
- Scope restriction: limits the topics the agent discusses
- Data grounding: forces the agent to only use retrieved documents
- Response formatting: controls the length and structure of answers
Combine techniques for best results
In production, use multiple techniques together. Here is an example of a system prompt that combines system prompt engineering, few-shot prompting, and source attribution:Monitor and iterate
No prompt configuration is perfect from the start. Build a feedback loop:- Log conversations: track questions and answers to identify hallucination patterns
- Test edge cases: regularly test with questions that should be refused or answered with uncertainty
- Refine prompts: update your system prompt based on observed failures
- Review source documents: sometimes hallucination occurs because the indexed data itself is ambiguous or incomplete. Improving document quality is often the most effective fix
Next steps
Configure guardrails
Set up scope restrictions and data grounding rules.
Display source documents
Let users verify AI responses by showing source documents.
Configure a chat workspace
Customize your workspace settings and system prompt.