Intermediate7 min

Reference-Based Prompting — Anchoring to Existing Code

The most effective way to prevent Claude from inventing nonexistent APIs is to reference existing code that uses them correctly. Reference-based prompting anchors Claude to your actual codebase rather than training data patterns that may be outdated.

Quick Reference

  • @file reference: Claude reads the file before responding — processed before the prompt is sent
  • 'Follow the pattern in X': reference an existing implementation Claude should match
  • Multiple references: 'use the error format from @src/errors.ts and the auth pattern from @src/auth'
  • Anchoring to real imports: 'use the ioredis package in package.json — follow src/lib/cache.ts'
  • Why this works: your codebase has correct current patterns; training data may be outdated
  • CLAUDE.md rule: 'ALWAYS read a file before modifying. NEVER invent utility functions — check src/lib/ first'

Why Hallucination Happens

Claude's training data includes code from thousands of repositories, many of which use outdated library versions, deprecated APIs, or patterns that have since changed. When Claude writes code without being anchored to your specific codebase, it draws on that training data — and sometimes invents APIs that used to exist, exist in a different library, or never existed at all.

The fix is not a better prompt explaining what not to do. The fix is giving Claude a concrete reference in your actual codebase — existing code that uses the correct current patterns.