Back to blog

Why AI Lies — And How I Made It Stop

ai-engineeringhallucinationsobservability

There's one thing I understood after spending serious time working with language models.

AI is not a villain. It's not trying to deceive you. It just... makes things up when it's not sure.

You ask: "Where is this function defined?"

AI answers: "In utils.ts, of course!"

Except there's no utils.ts in the project. But it sure sounded confident.

That's a hallucination — the model filling gaps with fiction. And it's one of the most dangerous failure modes in AI-assisted engineering, because it looks exactly like a correct answer.

How I fixed it in KB Labs

I put guardrails around the model. Think of it like a playground fence — you can play, but you can't make stuff up.

The system does three simple things:

1. Fact-checks references. If the model mentions a file or a function, the system checks whether it actually exists. If it doesn't — that's a fabrication, and it gets flagged.

2. Validates fields and parameters. Models love inventing variable names, method signatures, config options that were never there. Now every claim like that gets verified against the actual codebase.

3. Forces honest "I don't know." When there's no data, instead of a creative fiction the model says: "This doesn't exist in the source code." And that's infinitely more useful than a confident lie.

What changed

The model started answering slower, but more accurately. Less confident nonsense. More facts. And most importantly — it stopped trying to be a fiction writer when the job required an engineer.

The real insight

The problem isn't that the model is "stupid." The problem is that we let it make things up.

Give it boundaries — it works honestly. Don't — and it keeps answering like it's taking a survival exam, where any answer is better than silence.

AI shouldn't guess. It should tell the truth — or honestly say nothing.