ContentsRAG from Scratch to TRECAdisak Sukul · Iowa State University

Start here

Part 1 · Foundations

Part 2 · Finding the evidence

Part 3 · Writing the answer

Part 4 · Judging RAG

Part 5 · TREC and the 2026 tracks

Part 6 · Case studies: our submissions

Part 7 · Beyond text: video

Part 8 · Streams and advanced RAG

Back matter

Part 1 · Foundations · Chapter 2

What RAG is

In one sentence: retrieval-augmented generation is a loop of three moves: retrieve documents for the question, put them in front of the model, and have the model write an answer that cites them.

Why you should care

RAG is the most common way to make a language model useful on documents it was not trained on: a company's manuals, this week's news, a library, a legal archive. It is also simple enough to build in an afternoon, and hard enough to do well that a research track at TREC is devoted to it.

Words you need

Retriever
The search part. It takes the question and returns documents.
Generator
The language model that writes the answer.
Corpus
The collection of documents the retriever searches.
Passage or chunk
A piece of a document, small enough to hand to the model.
Context
The retrieved text that is placed in the prompt.
Citation
A marker in the answer that points to the document a sentence came from.
Attribution
The property that each claim can be traced to a source.
Fine-tuning
Training a model further on your own data, so that it changes its weights.

Core The idea

  1. 1RetrieveSearch the corpus with the question. Keep the best few documents.Part 2
  2. 2AugmentBuild a prompt: instructions, the retrieved text with numbers, then the question.Ch 12, 13
  3. 3GenerateThe model writes the answer and marks each sentence with the numbers of the documents it used.Ch 12 to 15

The name lists the same three moves in a different order: retrieval, augmented, generation.

A whole RAG system in a few lines

This is the complete idea in Python. The search here only counts shared words, and ask_model stands for a call to any language model. Real systems replace both parts with better ones. The shape stays the same.

docs = {
    "D1": "The council approved the bridge repair budget on 3 March.",
    "D2": "The new bridge lane was postponed until next year.",
    "D3": "The library extended its opening hours in April.",
}

def retrieve(question, k=2):
    q = set(question.lower().split())
    overlap = lambda text: len(q & set(text.lower().split()))
    ranked = sorted(docs, key=lambda d: -overlap(docs[d]))
    return ranked[:k]

def build_prompt(question, ids):
    sources = "\n".join(f"[{i}] {docs[i]}" for i in ids)
    return ("Answer only from the sources. "
            "After each sentence, cite the source id in brackets.\n\n"
            f"Sources:\n{sources}\n\nQuestion: {question}")

question = "What did the council decide about the bridge?"
prompt = build_prompt(question, retrieve(question))
answer = ask_model(prompt)   # e.g. "The council approved the repair budget [D1]. ..."

Read the prompt it builds. The instruction says two things that matter: answer only from the sources, and cite. Those two phrases are the difference between RAG and simply pasting text into a chat.

RAG and its two neighbours

There are three ways to give a model knowledge it lacks. They are often confused.

RAGFine-tuningLong context
What you doSearch first, give the model what you foundTrain the model further on your dataPaste all the documents into the prompt
New documents tomorrowAdd them to the corpusTrain againPaste them again
Can cite sourcesYes, by designNoYes, if you ask
Works on a huge collectionYesPartlyNo, the window is finite and long prompts cost money
Best forFacts that change, and answers that must be checkedStyle, format and skillsA few documents you already have in hand
Main riskThe search misses the right documentCost, and facts go staleThe model loses track in a very long prompt

These are not rivals. A real system can use all three: a fine-tuned model, fed by retrieval, with a long context.

Where RAG systems fail

Almost every failure is one of two kinds, and you should learn to tell them apart:

  • Retrieval failure. The right document was not in the context. The best writer in the world cannot fix this.
  • Generation failure. The right document was there, and the model ignored it, misread it, or added something from memory.

When an answer is wrong, look at the retrieved documents first. This one habit saves days.

Watch and read

How we did it at TREC 2026

Our RAG system has the three moves, with more steps inside the first one.

MoveIn our systemChapter
RetrieveSeven versions of each narrative, BM25 on ClimbMix, fused with RRF, then a reranker. 100 documents kept.5, 7, 8, 9
AugmentSnippet run: the first 500 characters of each document. Full-text run: up to 1,000 words of each.13
GenerateGPT-5 mini writes cited sentences, at most three documents per sentence. A second pass fits the answer into the word limit.12, 14

The two runs show the “long context” column of the table above in practice. The snippet run gave the writer about 7,900 words of evidence per narrative. The full-text run gave it about 90,000 words. Same writer, same prompt, same 100 documents.

What surprised us

Most of the work is in “retrieve”. We planned the year around the writer. In the end, the writer's prompt stayed almost fixed, and nearly every experiment was about which documents it saw, how many, how long and in what order. If you are new to RAG, spend your first week on retrieval and on looking at what was retrieved.

Try it

Lab 1. Build the ten-line RAG above.

  1. Replace ask_model with a call to any model you can use. Run the bridge question.
  2. Ask a question whose answer is in D3. Does the retriever return D3? Print what it retrieved before you read the answer.
  3. Ask a question that no document answers. Does the model say so, or does it make something up? Change the instruction until it says so.
  4. Ask “Was the crossing fixed?” The answer is in D1, but the question shares no useful word with it. Watch the word-overlap retriever fail. This is the reason for Chapters 5 to 7.

Check yourself

  1. Name the three moves of RAG in order.
  2. Which two phrases in the prompt turn “pasting text” into RAG?
  3. Your facts change every day. RAG or fine-tuning, and why?
  4. An answer is wrong. What do you look at first?
  5. Why is “paste everything into the prompt” not a solution for a large collection?
Answers
  1. Retrieve, augment, generate.
  2. “Answer only from the sources” and “cite the source”.
  3. RAG. You add documents to the corpus instead of training again.
  4. The retrieved documents, to see whether the right one was there at all.
  5. The context window is finite, long prompts are slow and costly, and models lose track of details in very long prompts.

Go deeper

  • The original RAG was trained end to end. In the 2020 paper the retriever and the generator were trained together. Most systems today are not trained at all. They connect a ready-made search engine to a ready-made model with a prompt. The name stayed and the method changed.
  • Retrieve once, or many times? The pipeline in this chapter retrieves once. An agent can search, read, notice what is missing and search again. Chapter 41 covers these loops.
  • What is a good unit to retrieve? Whole documents, fixed-size chunks, sentences or facts. The choice changes both retrieval and writing. See Chapters 13 and 40.
  • Project idea. Take 20 questions and one corpus. Compare a closed-book model, a RAG system and a long-context prompt on correctness, on cost per answer, and on how many claims you can verify.