Skip to main content
Large language models can sometimes generate information that is not present in the source documents. This is known as hallucination. While it cannot be fully eliminated, several techniques significantly reduce its occurrence in Meilisearch’s conversational search.

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
The key principle is: the LLM should only use information from the documents Meilisearch retrieves, not its general knowledge. By default, LLMs may draw on their training data to fill gaps. All the techniques below help enforce this boundary and keep responses grounded in your indexed data.

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.
Configure guardrails through the chat workspace settings or the Meilisearch Cloud UI. Available guardrails include:
  • 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
For detailed configuration examples, see the Configure guardrails guide.

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:
  1. Log conversations: track questions and answers to identify hallucination patterns
  2. Test edge cases: regularly test with questions that should be refused or answered with uncertainty
  3. Refine prompts: update your system prompt based on observed failures
  4. 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.