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.
The Hallucination Problem in AI Coding
AI hallucinations in code are different from hallucinations in general text. In coding, hallucinations manifest as:
- Invented APIs: The model calls
response.getBody()when the actual method isresponse.content - Wrong function signatures: Passing 3 arguments to a function that takes 2
- Fabricated file paths: Referencing
src/utils/auth.tswhen the file is actuallylib/authentication.ts - Non-existent imports:
from sklearn.llm import Compressor— looks plausible, doesn't exist - Outdated patterns: Using deprecated APIs from training data instead of current versions
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.
- Extract claims: WITNESS decomposes the LLM's response into individual factual claims
- Match evidence: Each claim is matched against the source code files, documentation, and context that was provided to the model
- Score entailment: Using STAVE (Statistical Textual Assessment and Verification Engine), each claim gets a support score
- 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
| Approach | Cost | Latency | Accuracy | Requires 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:
code: Strict — fails closed on unsupported code claims. Best for auto-merge pipelinesrag: Strict — for retrieval-augmented generation, suppresses unsupported retrievalsqa: Strict — for question-answering, flags fabricated answerschat: Warn — for conversational use, warns but doesn't blocksummary: Warn — for summarization, flags unsupported inferences
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?
Can I use WITNESS without context compression?
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?
Is there an API cost for hallucination detection?
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 Join Discord Compare All Tools