Cobra Developers
Blog, AI

How to reduce AI hallucinations in a real product

The short answer

To reduce AI hallucinations in a product, give the model your own data to answer from, narrow the task to one job, require it to point to its sources, keep fixed text and numbers in ordinary code, and have a person review output before it matters. These steps lower the error rate. None of them brings it to zero.

A hallucination is when a language model states something false as if it were true: a made-up policy, a case that does not exist, a detail that was never in the file. It happens because a model generates likely text. It is not looking facts up unless you build the product so that it does.

We have had to deal with this in two places where a wrong answer is not acceptable. Ask Tamou answers questions from visitors to a law firm's website. CaseCobra produces court documents. Neither can afford invented content, and in both the fix was mostly in the design around the model, not in the model.

Why do AI models hallucinate?

AI models hallucinate because they produce the most plausible continuation of the text they are given, and plausible is not the same as true. When a model lacks the information to answer, it does not reliably stop. It often fills the gap with something that sounds right.

This is a property of how the technology works, so it cannot be switched off with a setting. Newer models make fewer of these mistakes than older ones, and clear instructions help, but no provider promises an error-free model.

That shapes the whole approach. You do not try to make the model perfect. You arrange the product so the model has what it needs, has little room to improvise, and has its output checked.

How do you reduce AI hallucinations?

You reduce AI hallucinations with five measures used together: ground the model in your own data, narrow the task, require sources, keep fixed text out of the model, and review output before it matters. Each one removes a different way for an invented statement to reach a user.

  1. Ground it in your own data. Put the relevant facts in front of the model with the request, and instruct it to answer only from them.
  2. Narrow the task. Ask for one specific thing. A model asked to do one job has fewer chances to wander.
  3. Require sources. Have it point to the passage each statement came from, so a person or a program can confirm it.
  4. Keep fixed text out of the model. Anything that must be exact is stored and inserted by code.
  5. Review before it matters. A person checks the output at the point where an error would cause harm.

The sections below take the ones that need explaining.

What does grounding an AI in your own data mean?

Grounding means supplying the model with the specific information it should use, such as your service pages, a case record, or a set of documents, at the moment it answers. The model then works from text in front of it and does not rely on what it absorbed in training.

This is the single most effective step. A model asked about your business from memory will guess. A model handed your actual pages and told to answer from them has something real to work with.

Ask Tamou is an assistant that knows the firm's practice areas. A model does not arrive knowing a particular firm, so that knowledge has to be supplied. In CaseCobra, the caption, parties, and judge on a document come from the case record.

Grounding has two requirements that are easy to miss. The data has to be correct and current, because the model will repeat an outdated page faithfully. And the instructions have to cover the case where the answer is not in the data: say so, and offer a person. A grounded model can still misread what it was given, which is why the later steps exist.

How does narrowing the task reduce errors?

Narrowing the task reduces errors by shrinking the space of things the model could say. A model asked to draft two paragraphs from stated facts has far less room to invent than one asked to write a whole document, choose the law, and format the result.

Our document engine applies this strictly. The caption, parties, and judge are filled in by code from the case record. Standard language comes from templates. The model is asked only for the paragraphs that depend on the facts. It cannot get the caption wrong, because the caption never passes through it.

The general rule is that boilerplate must never pass through a model. We explain how to split a process this way in when to use AI and when plain code is better.

RiskWhat reduces it
Model invents facts about the businessGround it in your own pages and records
Model changes wording that must be exactKeep that text in templates, inserted by code
Model gives a wrong numberCalculate in code, pass the result in
Model answers outside its knowledgeInstruct it to say it does not know and hand off
A wrong statement goes unnoticedRequire sources and review before use

Why do you still need human review?

You still need human review because every other measure lowers the chance of an invented statement without eliminating it. A person who knows the subject, checking the output before it is filed, sent, or relied on, is the last control and the only one that carries responsibility.

In CaseCobra an attorney reviews the drafted paragraphs. In Ask Tamou, a person at the firm can step into the conversation from their phone, and the visitor sees the reply in the same chat window. In both cases the model produces a first pass and a person owns the outcome.

Review works best when sources are attached, so the reviewer is confirming and not researching. We go into where the reviewer should sit in human in the loop AI.

Frequently asked questions

Can AI hallucinations be eliminated completely?

No. They can be made much less frequent and much less harmful through grounding, narrow tasks, and review, but a language model can always produce an incorrect statement. Design the product on the assumption that it sometimes will.

Does a more expensive model stop hallucinations?

More capable models generally make fewer mistakes, but none is free of them. A capable model with no access to your data will still guess about your business. Product design matters more than model choice here.

How do I test whether my AI feature makes things up?

Collect real questions or inputs, including ones the system should not be able to answer, and read the outputs against the source material. Repeat the test whenever the instructions, data, or model change.

Is telling the model "do not make things up" enough?

No. The instruction helps a little, but a model cannot always tell what it does not know. Giving it the right data and checking the output do far more than a warning in the prompt.

What should a chatbot do when it does not know?

Say so plainly and offer a person. An honest "I do not have that" costs less than a confident wrong answer.

If you have an AI feature that sometimes invents things, show us what it is doing and we will look at where the gaps are.

Who wrote this

The Cobra Developers team. We are a software studio in Arizona that builds and runs its own products and builds websites, web apps, and automation for clients. This post draws on our work on Ask Tamou, CaseCobra. Everything here comes from projects we shipped; see all 30 of them.

guest@cobra: ~