惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
博客园 - 司徒正美
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
博客园 - 聂微东

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Why My RAG App Kept Hallucinating (and How I Fixed It)
Pallavi Sharma · 2026-06-22 · via DEV Community

A few months ago I was demoing my RAG-powered support bot to a colleague, feeling pretty confident about it.

Then it confidently told her our refund policy was “30 days, no questions asked.”

Our actual policy is 14 days, with conditions.

The bot didn’t hedge. It didn’t say “I’m not sure.” It just made it up and said it with the same calm tone it uses for everything else.

That demo stung.

RAG was supposed to fix hallucinations, not just relocate them.

Here’s what I learned debugging it, roughly in the order I learned it.

1. My chunks were too big, and too dumb

I was splitting documents by character count, 1000 chars with slight overlap.

It felt efficient. It wasn’t.

A single chunk often contained unrelated sections. For example, the end of a “Shipping Policy” and the start of a “Returns Policy” could sit together in the same block.

So when the retriever saw a query about returns, it would grab that chunk and the model would blend both sections into one confident but wrong answer.

Fix: I switched to semantic chunking based on headings and paragraphs instead of raw character limits.

More work upfront, but it stopped feeding the model Frankenstein context.

2. I trusted top-k similarity way too much

My retriever was pulling the top 3 chunks by cosine similarity and passing them straight into the prompt.

The problem: “similar” is not the same as “relevant.”

A chunk can be semantically close to the query but still not actually contain the answer. The model doesn’t know that, it just assumes everything in context is true.

Fix: I added a reranking step using a cross-encoder and started logging retrieval scores properly.

That alone made it obvious when the system had no real answer but was still trying to act confident.

3. I never told the model it was allowed to say “I don’t know”

My prompt was basically:

“Use the context to answer the question.”

That’s it.

No instruction on what to do when the context is insufficient.

So the model did what LLMs do when under-specified: it filled the gaps with something plausible.

Fix: I explicitly added:

If the answer is not clearly present in the context, say you don’t know.

Hallucinations dropped immediately after this. It was almost embarrassing how effective this was.

4. No retrieval, no answer (I wasn’t enforcing it)

Even with better prompting, the model would still sometimes answer from general knowledge when retrieval quality was weak.

I wasn’t actually gating anything. I was just hoping the prompt would enforce behavior.

Fix: I added a real threshold.

If the top retrieval score is below a cutoff, the system doesn’t proceed normally. It returns a fallback instead of letting the model improvise.

No relevant context → no forced answer.

5. I wasn’t testing the cases that actually break systems

All my testing was on “happy path” questions, things I already knew the documents covered well.

I wasn’t testing:

  • ambiguous queries
  • missing information cases
  • partially covered topics
  • multi-part questions

And that’s exactly where hallucinations show up.

Fix: I built a small evaluation set of “trap questions”, cases where the correct answer is not in the system and started running it regularly against changes.

That exposed weaknesses immediately.

Where it stands now

It’s not perfect.

RAG doesn’t eliminate hallucinations, it just makes them more controllable if you pay attention to how the system is built.

How you chunk.
How you retrieve.
How you decide what not to answer.

The bot still doesn’t know our refund policy is 14 days.

But now, when it’s unsure, it actually says so.

And honestly, that’s the part that made it usable.