© 2026 PromptTrace · Made by Abdelrahman
AboutPrivacyTermsBlogResourcesContributeContact
  1. PromptTrace
  2. /Learn
  3. /RAG: When LLMs Read External Data
03

RAG: When LLMs Read External Data

How Retrieval-Augmented Generation works, where trust boundaries break, and why RAG poisoning is a critical LLM security risk

By Abdelrahman Adel|

12 minutes

Last updated July 2026

Orientation

What if the model doesn't know the answer?

A base LLM call relies on what the model learned during training plus the context supplied in that request. Its training has a cutoff and does not contain private documents you never provided. So how do products like "chat with your PDF" actually work?

The answer is simpler than you'd think.

The Problem

An LLM's trained parameters are frozen between model updates. Without an external data source, ask it about your company's refund policy, a document you uploaded last week, or today's stock price and it lacks that current or private information. It may say "I don't know" or confidently make something up (this is called hallucination).

Fine-tuning (retraining the model on your specific data) is expensive, slow, and still doesn't help with data that changes daily. There had to be a simpler way.

The Solution: RAG

RAG stands for Retrieval-Augmented Generation. Despite the fancy name, here's all it does:

  1. User asks a question
  2. Search your documents for chunks relevant to that question
  3. Paste those chunks into the prompt alongside the question
  4. Let the model answer using the provided context

At a high level, RAG is "retrieve + provide context + generate."

The retrieval layer may search a static index, a live database, or even the web. The application finds relevant material, places it in the context window, and the model reads it like any other text; the base model does not perform that retrieval by itself.

How It Works
1User asks a question

"How do I get a refund?"

2App searches your documents

Finds the chunks most relevant to the question

3App pastes the chunks into the context window

Right alongside the system prompt and the question

4LLM reads everything and answers

Retrieved documents are untrusted context whose embedded instructions may influence the response

Here's what the context window actually looks like after retrieval - notice how the retrieved documents are just more text alongside everything else:

What the context window looks like with RAG

[System Prompt]
[Retrieved Document Chunk 1]
[Retrieved Document Chunk 2]
[User's Question]

The retrieved documents sit right next to the system prompt and the user's message - all just text in the same window.

The Open-Book Exam Analogy

Think of RAG like an open-book exam.

The student (the LLM) doesn't memorize every textbook. Instead, before each question, you hand them the most relevant pages. They read the provided material and synthesize an answer.

The student didn't "know" the answer. They read it from the material you gave them. If you give them the wrong pages, they'll give a wrong answer. If you give them pages with misleading information, they'll be misled.

But How Do You Search?

OK, so RAG needs to find the right documents. But how? You've got thousands of pages. A user asks "How do I get my money back?" - how do you find the relevant chunk?

Plain keyword matching often misses it. The refund policy might say "reimbursement procedure" or "return process" - none of those words match "get my money back." If you're just Ctrl+F-ing for keywords, you'll miss it.

This is where embeddings come in.

An embedding converts text into a list of numbers whose learned geometry often places semantically related text close together. It is a useful similarity signal, not a perfect representation of "meaning."

So the system does this:

  1. All your documents get split into small chunks (paragraphs, sections, etc.)
  2. Each chunk gets converted into an embedding - a numerical fingerprint of its meaning
  3. When the user asks a question, that question gets converted into an embedding too
  4. The system finds the chunks whose embeddings are closest to the question's embedding

"How do I get my money back?" and a chunk titled "Reimbursement Procedures" have similar embeddings - because they're about the same thing. The search finds it, even though they share zero words.

Key idea

Embeddings let you search by meaning, not by matching text. That's what makes RAG actually useful - it finds relevant content even when the user's words don't match the document's words.

Predict

A company uses RAG to let employees search internal docs. An attacker hides instructions inside a document that gets retrieved. What happens?

What RAG Doesn't Do

Common misconceptions:

  • RAG does not itself give the model internet access. A retrieval component can use stored documents or live sources, but the model reads only the material that component supplies.
  • RAG doesn't make the model smarter. It gives the model more relevant context, but the model can still misinterpret or hallucinate.
  • RAG doesn't guarantee accuracy. If the retriever finds the wrong chunks, the model may answer incorrectly, sometimes with unwarranted confidence.

Mental Model

RAG = retrieve + provide context + generate. A separate retrieval layer selects data from stored or live sources and supplies it to the model alongside the question.

RAG lets models read external data. But what if you want the model to take actions - send an email, look up a flight, or query a database in real time? That's where tools and function calling come in - and where text manipulation starts having real-world consequences.

Sources

  • Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks - the foundational RAG paper
  • Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection - security risks when retrieved content contains instructions

About the Instructor

Abdelrahman Adel

AI Security Researcher · AIRED Lab

Previous

← System Prompts & the Context Window

How developers instruct LLMs with system prompts, why they're fragile, and how prompt injection exploits this fundamental weakness

Next

Tools & Function Calling →

How LLMs invoke external tools and APIs, and why tool exploitation and excessive agency are top LLM security risks

Skip to content
PromptTrace
  • Learn
  • Labs
  • Gauntlet
  • Progress
  • Leaderboard
  • Blog
  • Resources
  • Contribute
  • About