Reducing Hallucinations with Repo Grounding
Ground answers in repo facts — quoting sources, verification chain.
What You Will Learn
- Ground Copilot answers in repo facts.
- Use citation-style prompts.
- Build a verification chain.
- Distinguish grounded vs ungrounded.
- Measure hallucination reduction.
Why This Matters
Hallucinations (BE-31) are the biggest risk with Copilot. Grounding — anchoring answers in repo facts with citations — reduces hallucinations dramatically. This lesson builds the grounding habit.
Concept Explained
Grounding means asking Copilot to cite specific files and lines for its claims. 'Where in the codebase is X defined?' produces grounded answers; 'what does X do?' may produce hallucinated explanations.
How It Works
Add 'cite file:line for each claim' to prompts. Ask Copilot to distinguish what it found in the repo vs what it inferred. Verify citations by reading the actual code.
Step-by-Step Tutorial
1. Ask for citations
'@workspace How does auth work? Cite file:line for each claim.'2. Distinguish grounded vs inferred
'For each claim, mark [GROUNDED] if found in code, [INFERRED] if from general knowledge.'3. Verify citations
Open each cited file:line. Confirm the claim matches.4. Reject unverified
If Copilot can't cite, treat as hallucination. Don't trust.5. Iterate
If grounded claims are wrong, attach the correct file and re-ask.Real-World Example
A developer asked '@workspace how does the rate limiter work?' Got a 5-paragraph explanation. Asked again with 'cite file:line for each claim'. Got 3 grounded claims (with citations) and 2 marked INFERRED. Verified the 3 grounded — all correct. The 2 inferred were hallucinated. Grounding caught them.
Example Prompts / Commands / Code
# Citation style:
@workspace How does the auth middleware work?
For each step, cite the file:line where it's implemented.
Mark [GROUNDED] if you found it in code, [INFERRED] if from general knowledge.
# Verification chain:
@workspace Where is the function getUser defined? Cite the file:line.
Then: Where is getUser called? List each call site with file:line.
Then: What does getUser return? Cite the type definition.
# Distinguishing:
For your answer above:
- Which parts did you find in the codebase? Cite them.
- Which parts did you infer? Mark them [INFERRED].
Common Mistakes
- Not asking for citations — easy to accept hallucinations.
- Not verifying citations — Copilot can cite wrong lines.
- Treating INFERRED as GROUNDED — different confidence levels.
- Not iterating when grounded claims are wrong.
Best Practices
- Always ask for file:line citations.
- Use [GROUNDED] vs [INFERRED] markers.
- Verify citations by reading actual code.
- Reject unverified claims as hallucinations.
- Iterate when grounded claims are wrong — attach correct file.
Troubleshooting
| Problem | How to Fix |
|---|---|
| Copilot can't cite | It's hallucinating. Don't trust. Attach the relevant file and re-ask. |
| Citations are wrong | Verify file:line yourself. Re-ask with correct context. |
Practical Exercise
Your Turn
Ask Copilot a question about your codebase without citations. Then re-ask with 'cite file:line'. Compare. Verify the citations are correct.
Professional Challenge
Build a 'grounded answers only' policy for your team: any Copilot answer in a PR review must cite file:line. Measure hallucination rate before/after.
Key Takeaways
- Grounding anchors answers in repo facts.
- Always ask for file:line citations.
- Use [GROUNDED] vs [INFERRED] markers.
- Verify citations by reading code.
- Reject unverified claims.
Frequently Asked Questions
Does grounding eliminate hallucinations?
What if Copilot cites a wrong line?
Further Reading
Official References
SEO Metadata
SEO title: Reducing Hallucinations with Repo Grounding
Meta description: Ground answers in repo facts — quoting sources, verification chain.
Primary keyword: reducing hallucinations with repo grounding
Secondary keywords: reducing hallucinations with repo grounding
Search intent: Informational
URL slug: /reducing-hallucinations-repo-grounding-copilot
Categories: AI Tools, GitHub Copilot
Tags: GitHub Copilot, Intermediate, Hallucinations, Grounding, Verification, IMCSEIAN, Tutorial, IMCSEIAN
Featured image concept: IMCSEIAN lesson card for Reducing Hallucinations with Repo Grounding
Comments
Comments
Post a Comment