The AI explained a loop that does not exist. One line stops that
An AI coding assistant can describe code in a file it never opened, and the guess sounds as sure as a real answer. Anthropic's prompting docs give a line that makes it read the files first.
Why it matters
In the story, the AI blamed a loop in the export file. The file was 11 lines long and had no loop.
Anthropic's prompting docs list this under minimizing hallucinations in agentic coding.
Reading files costs time and tokens, so tell it where to start.
Do this
- Ask your question about the code as usual.
- Add the line below to the prompt, or put it in your CLAUDE.md so every session gets it.
- Name the first file to read, then let it follow what that file calls.
- Check the answer names the files it opened. No file names means it may still be guessing.
Add this to your prompt or CLAUDE.md
Never speculate about code you have not opened. If I reference a specific file, read the file before answering. Start with the file I name, then follow what it calls.
One thing to keep in mind: On a big project, "read the files first" can make it open far too many files. That costs time and tokens. Naming the first file keeps the search short.
The first two sentences are adapted from Anthropic's sample prompt (investigate_before_answering). The last sentence is our addition. Source: Anthropic, Claude prompting best practices: minimizing hallucinations in agentic coding.
Free live lesson · Nov 24Stop Measuring AI by Tokens. Measure What It EarnsOne hour, live, with Saurav. Bring your own bill. Free.Save my free seat → Learn it live, then run it yourselfLoop and harness engineering, AI agents on AWS, AI cost and ROI.See the workshops →