When Not to RAG - Don't Tell the LLM What It Already Knows

Umer Mansoor · Sep 18, 2024

Imagine a tutor who has read every academic textbook ever written. You ask them to describe photosynthesis and you get a really good answer. You didn’t need to show them your textbook or the Wikipedia page on photosynthesis. The tutor is super-intelligent and they don’t need to look up anything to answer your question.

Now, you ask the same tutor what’s on my BIO 101 midterm next week? They have no clue. They are super-ultra smart, but they have not seen your syllabus. So you ask again, but this time you hand over the BIO 101 syllabus. You get a really good answer.

The tutor is a large language model aka LLM. Giving it your syllabus alongside the question is known as RAG or Retrieval Augmented Generation which is a fancy way of saying: when you ask the model something it doesn’t already know, you go find some documents that might help and paste them into the prompt along with your question.

RAG is a good technique. It’s also easy to overuse. For example, imagine asking the tutor to “describe photosynthesis” but this time, you hand them a bunch of documents: slide 14 from somebody’s BIO 101 slidedeck, a highschooler’s lab report on growing cilantro leaves in their kitchen, a PhD’s essay on the latest advancements in our understanding of photosynthesis as tested by NASA on Mars and someone’s deep philosophical explanation, and insisting that the tutor uses the source material to answer. The tutor starts reading the evidence and after a long wait, you get this answer:

Photosynthesis is the biological process that plants use to convert sunlight into usable metabolic energy. It’s an important topic for exams. One experiment showed that cilantro does this well on a kitchen windowsill, though results on Mars remain preliminary. But in the end, we must ask: are we all just not photosynthesizing to convert light into something useful?

Noticed something? The answer described photosynthesis correctly but then it takes a weird angle.

In this blog post, let’s look at what overusing RAG did an AI chat for students, and how we fixed it.

Context

Suppose you are a hands-on manager of multiple teams at an Edtech company. After yet another reorg as AI eats away your business, you’ve just inherited the AI team. Some months earlier, the team shipped an AI chat for students, which is also yours now.

This is how the system works:

  1. A student sends a chat message.
  2. A tiny classifier model detects intent.
  3. Semantic search runs over the document library (RAG) to find relevant documents.
  4. The best chunks are sent to the LLM along with the system prompt and the student’s message (RAG).
  5. The LLM’s response goes through a judge (another LLM call).
  6. The response is sent to the student.

That’s a lot of steps for a single response. Worse yet, none of the calls are parallelized.

Let’s look at some rough numbers. The classifier is very fast and takes a few 100 milliseconds. The semantic search (Pinecone) adds another 300-500ms. The main LLM call, with 3-4k tokens of context, adds 6-8 seconds. Then comes the judge. An older, smaller model to keep costs down. It has a rubric in its system prompt and grades the response against the query and the rubric, adding another 3-5s. Average response takes about 10 seconds. Your design team is asking whether they should build a pizza tracker while students wait for chat messages. ChatGPT starts showing you a response in less than 2 seconds. You know you are in trouble.

The team had done a good job by 2023 standards. They indexed high-quality documents, which sounds easy but it isn’t: consider more than 50 million documents, sparsely (or sometimes incorrectly) tagged to courses and subjects, a sensible chunking strategy and a pass to strip out non-printable characters. They tested prompts. They executed against the mandate they were given: differentiate from ChatGPT and reduce hallucinations by grounding answers in millions of study documents that the company had. Nothing wrong with the thinking. The team built pristine notebooks and charts to show hallucination rates were down.

But Amplitude is showing something else: students are abandoning chats at an alarming rate. The product and design teams had done a good job of funnelling users to the new AI chat product. From there, it’s a different story: the feature is a leaky bucket with a giant hole in it. Students aren’t coming back to the feature and worse yet, most are abandoning it midway through the first couple of messages. You clearly have a problem.

You ask your UX researcher to talk to students who have been active on chat. 10 interviews later, the signal is clear and consistent:

  1. Responses take too long. Students switch to another tab and get the answer from ChatGPT, or in some cases Google.
  2. The answers are not better than ChatGPT and sometimes they are worse and plain weird.

The second one is news to you and you do some digging and find out what’s happening: student sends “describe photosynthesis.” Your AI chat system describes photosynthesis but then it takes a strange detour through whichever biology documents or slidedeck happened to score highest in search. Disclaimer: The example response in the intro is made-up and intended to be funny if you already didn’t guess.

Now here’s a part that often gets glossed over. RAG definitely helps reduce hallucination when the model is missing knowledge. But when it already knows the answer and you hand it irrelevant or losely related chunks with instructions to use them, then one of two things happen: the LLM may ignore your instructions and answer from its own knowledge or it dutifully incorporates retrieved context. In your analysis, the latter is why source of ‘weird’ answers. You’re paying extra to confuse GPT.

What you change

After a lot of debate, you land on the following decisions:

  • Stop telling the model what it already knows. Your analysis shows that nearly two-thirds of messages can be answered without any of your documents. The team re-runs their earlier analysis and someone on the team also put together a spreadsheet to let humans annotate latest responses. You notice that the newer models aren’t hallucinating as much without help from RAG. Which makes sense because most academic topics haven’t changed in years and newer LLMs are handling these academic queries well on their own. Someone suggests that you should improve RAG: implement better re-ranking, better hybrid search, smarter chunking strategy. Another floats switching to the newest framework that’s trending on GitHub and going viral on X. But you decide not to pursue RAG major improvements because none of your users are asking for better retrieval (other reasons: tuning RAG on tens of millions of documents of questionable quality isn’t easy; your user interviews showed that students clearly preferred faster responses). But you also don’t drop RAG: you run it in parallel but only feed retrieved context to the LLM when you detect that the intent is to get course-specific info. To detect intent, you retrain your classifier.

  • Remove the blocking judge from the critical path. This is not an easy decision and also not yours to make. You need other departments (PR, legal and other PMs) on board. The judge is a safety net. There are valid fears that the AI chat will say something horrible and the screenshots will end up on X or Reddit and hurt the brand. But when your team reviews it, you find that the newer models have gotten better at following instructions and the judge isn’t adding that much value. So you move it off the critical path: it just no longer blocks responses and judges asynchronously. You set up a dashboard and alerts, and define a new process for humans to review a sample of responses. You get chat streaming and suddenly, your AI chat feels fast. As a cherry on top, you add a parallel call to the Moderations API.

  • Inject course-specific knowledge into the system prompt. An engineer on the team notices that for popular courses, the same chunks are retrieved most of the time. When you inspect those chunks and their documents, you realize the LLM only needs 3-4% of what’s in them: the key details. So the team builds a pipeline that runs an LLM over each course’s documents, extracts those details, and injects them straight into the system prompt. Think 5-7 bullets per course.

  • Run document search for every query, in parallel. You still search documents on every message, even the ones that don’t need retrieval. But instead of feeding the results to the LLM, you show them under the response as “Related Materials.” The search runs in parallel with the LLM call instead of before it.

You launch your changes as an A/B test. A few days later, you notice latency improvement and fewer abandonments (students navigating away to another tab) in treatment. The number of messages exchanged jumps as well. As an added bonus: the clickthrough rate on the documents shown under answers increases too. Is it enough to compete with ChatGPT? Will you hit your Day-30 retention target? Time will tell. You know it’s a very difficult task to compete with the top AI labs with top talent who are collecting an insane amount of data on what users are asking and doing to improve the next generation of their models. You go back to user interview transcripts to find some more answers on what to build, and you are a little happier because your team patched at least some holes in the leaky bucket.

When to RAG

RAG is a useful technology and here’s when I’d use it:

  • The information needed to answer a question is private: syllabus, company policy, your own files. The model doesn’t know it and no amount of intelligence will compensate for it (or maybe it’ll one day 🤔)
  • The information changes frequently: syllabus, schedules, calendars, etc.
  • You need to show citations and ground answers: when your users actually want information to be grounded (e.g. in their own documents), and you need to show sources. E.g. a PhD candidate wants answers grounded in the latest papers and journal articles. Note: if you are grounding answers in your source material, you must make sure that you have very high quality source material. In most cases, UGC (user generated content) doesn’t meet this criteria.

I’d still skip it when speed is the key feature and marginal gains are modest or when the system already knows the information well.

Lessons

  • RAG is not free. Every chunk you retrieve has a cost: latency, price of the API and a chance that it could confuse the model and degrade answer quality.

  • Guardrails and judges have a cost too. A judge disables streaming and streaming is how users perceive “speed”.

  • The AI world is improving at light-speed. What you knew or used last year may no longer be the optimal solution. The judge made sense for older models but wasn’t as effective with newer ones. Re-evaluate and check your design and assumptions regularly.

Note: With the exception of some key details, this has real lessons from the field.

Written by . I've been a CTO, Head of Applied AI, director, engineering manager and lead engineer at various companies, and I write about AI, software engineering and leadership.


You May Also Enjoy