Why Prompt Engineering Can't Fix Code Hallucinations
Published 22 June 2026The first thing every developer tries when their AI writes wrong code: write a better prompt. "Only use packages from my package.json." "Don't invent functions." "Check my imports before writing." It helps. It doesn't fix the problem.
The prompt can't give the model what it doesn't have
When you tell the AI "only import packages from my package.json," you're asking it to reference information it doesn't have at generation time. The model isn't reading your package.json while it writes code. It's generating tokens based on probability distributions from training data.
Your prompt becomes an instruction the model tries to follow without the data it would need to follow it correctly. It's like telling someone to "only use words from this dictionary" without giving them the dictionary.
Prompts are probabilistic, not deterministic
Even with a perfect prompt, the model's compliance is probabilistic. It will follow the instruction most of the time. But "most of the time" means hallucinated imports still slip through — just less often.
This is the fundamental difference between a prompt and a verification layer:
- Prompt: "Please only use real imports" → the model tries → it usually does → it sometimes doesn't
- Verification: code is written → imports are compared against package.json → mismatch is flagged → every time
A prompt reduces the probability of hallucination. Verification catches hallucinations that occur regardless of the prompt.
The same applies to function calls
You can tell the model "only call functions that exist in my codebase." The model will try. But it doesn't have a symbol table of your project. It's guessing which functions exist based on context clues and training patterns. Sometimes it guesses wrong.
No prompt can substitute for actually reading your source files and checking whether db.findUserByEmail() exists as an exported function in your project. That requires a verification layer that has access to your project state.
What a verification layer does differently
Check doesn't try to prevent hallucinations. It catches them after they happen. The AI writes code. Check reads your actual package.json and your actual source files. It compares what the AI wrote against what your project actually contains. Mismatches get flagged.
No AI inside. No probability. Your dependency list is a fact. Your exported symbols are facts. The comparison is deterministic.
Use both. Write good prompts to reduce hallucination frequency. Use Check to catch the ones that get through anyway.
Prompts reduce hallucinations. Check catches the rest.
120 free checks. $0.0068 AUD per check after. No subscription.
Get startedFrequently asked questions
Can prompt engineering prevent AI code hallucinations?
Prompts can reduce hallucinations but cannot eliminate them. The fundamental problem is that the model generates code without access to your project's dependency tree, source files, or installed packages. No prompt can give the model information it doesn't have at generation time.
What's the difference between a prompt and a verification layer?
A prompt is an instruction to the model before it generates code. A verification layer checks the generated code after the model writes it. Prompts are probabilistic — they influence but don't guarantee. Verification is deterministic — it compares against your actual project state.
How does Check verify AI code without using AI?
Check reads your actual package.json for dependencies and scans your source files for exported symbols. After the AI writes code, Check compares the imports and function calls against these real lists. No model, no probability — just comparison.