How to Prevent AI Hallucinations in Code ($0, local, 3ms)

AI coding tools like Cursor, Claude Code, and Cline are incredibly productive — until they hallucinate. They invent APIs that don't exist, reference functions with wrong signatures, and fabricate file paths. A single hallucinated import can cost hours of debugging.

WITNESS, built into Entroly, checks every LLM response against the actual source code before it reaches your editor. It runs locally, costs nothing, and catches hallucinations in 3 milliseconds.

AUROC Score
0.844
HaluEval-QA benchmark
Cost Per Check
$0
Runs locally, no API
Latency
~3ms
Per decision

The Hallucination Problem in AI Coding

AI hallucinations in code are different from hallucinations in general text. In coding, hallucinations manifest as:

These are hard to catch because they look right. The syntax is valid. The naming conventions match. But the code doesn't work.

How WITNESS Works

WITNESS uses Natural Language Inference (NLI) — a well-established NLP technique — to check whether claims in the LLM's output are entailed by (supported by) the evidence provided in the context.

  1. Extract claims: WITNESS decomposes the LLM's response into individual factual claims
  2. Match evidence: Each claim is matched against the source code files, documentation, and context that was provided to the model
  3. Score entailment: Using STAVE (Statistical Textual Assessment and Verification Engine), each claim gets a support score
  4. Flag or suppress: Unsupported claims are flagged (warn mode) or suppressed (strict mode) before reaching the developer
entroly witness --context-file evidence.txt --output-file answer.txt --mode strict

Comparison: Hallucination Detection Approaches

ApproachCostLatencyAccuracyRequires API?
WITNESS + STAVE $0 ~3ms 85.8% (0.844 AUROC) No
GPT-4o-mini judge $0.15/1K checks ~800ms 86.3% Yes
GPT-3.5-turbo judge $0.05/1K checks ~500ms 62.6% Yes
Manual review Developer time Minutes Variable No

WITNESS achieves statistically comparable accuracy to GPT-4o-mini as a grounded judge — but at $0 cost and 250× lower latency.

Profiles for Different Workloads

WITNESS ships with tuned profiles for different use cases:

entroly proxy --witness strict --witness-profile code    # suppress hallucinations in coding
entroly proxy --witness strict --witness-profile rag     # suppress in RAG pipelines

Setup: 30 Seconds

pip install entroly
cd /your/repo
entroly go

That's it. WITNESS automatically runs on every LLM response when Entroly is active. You can also use it standalone:

entroly witness --context-file evidence.txt --output-file answer.txt --mode strict

FAQ: AI Hallucination Prevention

What causes AI hallucinations in code?
AI hallucinations in code occur when the LLM generates plausible-looking but incorrect code. This happens because LLMs are probabilistic text generators — they predict likely next tokens, not verified facts. When context windows are stuffed with irrelevant code, models are more likely to hallucinate because they cannot attend to the right evidence. Entroly addresses both causes: it compresses context to improve signal-to-noise ratio, and WITNESS verifies the output.
Can I use WITNESS without context compression?
Yes. WITNESS works as a standalone verification tool. You can run entroly witness --context-file evidence.txt --output-file answer.txt on any LLM output + evidence pair. However, combining compression (which improves input quality) with verification (which checks output quality) gives the best results.
Does WITNESS work with Claude? With GPT-4? With Gemini?
Yes. WITNESS checks the output of any LLM, regardless of provider. It runs as a post-processing step — after the model generates a response, WITNESS verifies it against the evidence. Works with Claude (Anthropic), GPT-4/o (OpenAI), Gemini (Google), and any other LLM.
Is there an API cost for hallucination detection?
No. WITNESS runs entirely locally using lightweight NLI models. $0 per check, ~3ms latency, no internet required. This is fundamentally different from "LLM-as-judge" approaches (like using GPT-4o-mini to check GPT-4 outputs), which require a second API call and double your costs.

Stop Hallucinations in 30 Seconds

No config. No API key. No signup. Runs locally.

pip install entroly && cd /your/repo && entroly go

⭐ Star on GitHub    Compare All Tools