Skip to content

Why AI tools hallucinate about code, and how to catch it

Invented functions, wrong file paths and confident guesses about code the model never saw: why AI answers about code go wrong, and the checks that catch it.

October 13, 2026 · 2 min read · RepoGrasp team

AI tools can explain code remarkably well. They can also describe a function that doesn't exist, cite a file path that's slightly wrong, or explain behaviour the code doesn't have, with complete confidence. Understanding why helps you catch it.

Why it happens

The model didn't see the code

The most common cause. If the relevant file wasn't in the model's context (not pasted, not retrieved, not explored), the model can only infer from names and patterns. "This function is called validateOrder, so it probably validates the order" is a reasonable guess and often wrong in the details.

Names suggest more than the code does

Models know how typical code is written. Faced with sendWelcomeEmail, a model may describe templates, retries and logging that a typical implementation would have, even if this one only calls an API.

Similar code blends together

In a large context, two similar functions (an old and new version, a test helper and the real one) can be mixed up. Without line numbers, the model can't point at one specifically.

Pressure to answer

Models are trained to be helpful. When the honest answer is "the repository doesn't show this", a model may produce a plausible answer instead unless it's explicitly told to say when information is missing.

What hallucinations look like

  • A function, option or file that doesn't exist
  • A real function described with behaviour it doesn't have
  • Correct steps in the wrong order
  • Library behaviour described from documentation for a different version
  • Precise-sounding numbers (timeouts, limits) that aren't in the code

How to catch them

  1. Ask for citations, and open them. A claim tied to src/orders.ts:40-55 takes seconds to check. A claim tied to "the order service" doesn't.
  2. Check the surprising claims first. If an answer mentions something you didn't expect, verify that before anything else.
  3. Search for names. If a function is mentioned, search for it. Non-existent names are the quickest hallucination to catch.
  4. Ask the reverse question. "Where is retryLimit set?" If the answer can't find it, the earlier claim was suspect.
  5. Run it. For behaviour that matters, a log line or test settles it.

How tools reduce them

  • Put the right code in context. Whole-repository context or good retrieval means fewer guesses. See RAG vs long context.
  • Number lines so the model can cite precisely, and readers can check precisely.
  • Instruct the model to admit gaps: say when the repository doesn't contain the answer, and point to the closest relevant code.
  • Separate evidence from inference so readers can see which claims are read directly from code.

RepoGrasp does all four: it sends every kept source file with line numbers, requires inline file:line citations, and tells the model never to invent files, functions or behaviour. That reduces hallucinations; it doesn't remove them. Check the citations.

See why citations matter.