Lesson 9

How does a chatbot answer from a 500-page PDF?

A chatbot does not read all 500 pages. Your code finds the few pieces that match the question and pastes only those into the prompt. This is called RAG. The lesson follows one question to a store's chatbot: who does each step, what it costs, and when it gives a wrong answer.

1Why not paste the whole PDF into every question?

Pasting the whole PDF is slow and costs a lot. A 500-page PDF is about 3 lakh tokens, and the model reads every one of them for every question.

2What is RAG?

RAG is a way to answer from your own documents: your code finds the few pieces that match the question and puts them into the prompt, next to the question.

You already do this with a textbook. To answer "What is photosynthesis?", you find it in the index, open page 112 and read one page. You do not read the whole book again.

RAG stands for Retrieval-Augmented Generation:

Adding the pieces is easy: your code joins texts together. The hard part is the search: which pieces match the question?

3How does RAG answer a question, step by step?

RAG works in two parts. The setup runs once, when you add the document. The second part runs for every question a customer asks.

First, two new ideas: one list of numbers for a whole text, and how close two lists are.

๐Ÿ› ๏ธ Setup, once:

  1. โœ‚๏ธ Cut the document into small pieces, called chunks.
  2. ๐Ÿ”ข Turn each chunk into one list of numbers, its embedding.
  3. ๐Ÿ—„๏ธ Store every list, with its chunk's text, in a database.

๐Ÿ’ฌ Every question:

  1. ๐Ÿ”ข Turn the question into one list of numbers, the same way.
  2. ๐Ÿ—„๏ธ Find the stored lists closest to the question's list.
  3. ๐Ÿง‘โ€๐Ÿ’ป Paste those chunks into the prompt, with the question.
  4. ๐Ÿค– The LLM reads the prompt and writes the answer.

In the example below, a customer asks an online store's chatbot about returns. Use the buttons to go through it one step at a time.

  • ๐Ÿง‘โ€๐Ÿ’ป Your code
  • โœ‚๏ธ Splitter
  • ๐Ÿ”ข Embedding model
  • ๐Ÿ—„๏ธ Vector database
  • ๐Ÿค– LLM

SETUP ยท ONCE

๐Ÿ“„ Step 1: the store's policy PDF

Who: ๐Ÿง‘โ€๐Ÿ’ป your code opens the PDF and copies its text.

Bazaar Box policy handbook ยท 500 pages

Electronics returns. Phones, laptops and headphones can be returned within 7 days of delivery, unopened and in the original box.

Faulty electronics. If an electronic item stops working within 30 days, we replace it free of charge.

โ€ฆ 498 more pages โ€ฆ

SETUP ยท ONCE

โœ‚๏ธ Step 2: cut it into chunks

Who: โœ‚๏ธ the splitter, a small function in your code. Not the LLM.

Chunk 1Electronics returns: 7 days, unopenedโ€ฆ Chunk 2Faulty electronics: replaced within 30 daysโ€ฆ Chunk 3Refunds: back to your card in 5-7 daysโ€ฆ Chunk 4Electronic items disposal: e-waste countersโ€ฆ Chunk 5Clothing returns: 15 days, tags onโ€ฆ Chunk 6Delivery: free above โ‚น499โ€ฆ

โ€ฆ 667 chunks for the whole PDF.

SETUP ยท ONCE

๐Ÿ”ข Step 3: each chunk becomes a list, and is stored

Who: ๐Ÿง‘โ€๐Ÿ’ป your code sends each chunk to ๐Ÿ”ข the embedding model, then saves the list in ๐Ÿ—„๏ธ the vector database.

Chunk 1[0.12, โˆ’0.48, 0.91, โ€ฆ] Chunk 2[0.10, โˆ’0.41, 0.77, โ€ฆ] Chunk 3[0.55, 0.20, 0.08, โ€ฆ] Chunk 4[0.09, โˆ’0.52, 0.30, โ€ฆ] Chunk 5[0.61, 0.33, โˆ’0.15, โ€ฆ] Chunk 6[โˆ’0.70, 0.44, 0.02, โ€ฆ]

Each row keeps the list, the chunk's text and its page number.

EVERY QUESTION

๐Ÿ™‹ Step 4: a customer asks

Who: ๐Ÿ™‹ the customer types in the chat. ๐Ÿง‘โ€๐Ÿ’ป Your code receives the message.

What's the return policy for electronic items?

EVERY QUESTION

๐Ÿ”ข Step 5: the question becomes a list too

Who: ๐Ÿ”ข the same embedding model as in step 3.

"What's the return policy for electronic items?"โ†’ ๐Ÿ”ข โ†’[0.11, โˆ’0.46, 0.85, โ€ฆ]

EVERY QUESTION

๐Ÿ—„๏ธ Step 6: find the 3 closest chunks

Who: ๐Ÿ—„๏ธ the vector database compares the question's list with every stored list.

Closeness of each chunk's list to the question's list (1 is the highest)

  • โœ… Chunk 1 ยท Electronics returns0.91
  • โš ๏ธ Chunk 4 ยท Electronic items disposal close, wrong topic0.84
  • โœ… Chunk 2 ยท Faulty electronics0.81
  • Chunk 3 ยท Refunds0.63
  • Chunk 5 ยท Clothing returns0.57
  • Chunk 6 ยท Delivery0.22

Rows on a green background: the 3 closest chunks, which your code takes. The โœ… and โš ๏ธ marks are ours. The database sees only the numbers.

EVERY QUESTION

๐Ÿง‘โ€๐Ÿ’ป Step 7: build the prompt

Who: ๐Ÿง‘โ€๐Ÿ’ป your code joins three parts into one text.

You are Bazaar Box's support bot. Answer only from the policy text below, and give the page. Chunk 1 (page 12): Phones, laptops and headphones can be returned within 7 daysโ€ฆ Chunk 4 (page 47): Drop old phones, chargers and batteries at any e-waste counterโ€ฆ Chunk 2 (page 13): If an electronic item stops working within 30 days, we replace itโ€ฆ Question: What's the return policy for electronic items?

Grey line: the instruction. Green lines: the 3 chunks. Orange line: the question.

EVERY QUESTION

๐Ÿค– Step 8: the LLM answers

Who: ๐Ÿค– the LLM. It sees only this prompt, about 1,500 tokens, not the 500 pages.

You can return phones, laptops and headphones within 7 days of delivery, if they are unopened and in the original box (page 12). If an item stops working within 30 days, we replace it free (page 13).

The disposal chunk was in the prompt, but it does not answer the question, so the LLM did not use it.

A made-up store, policy text, page numbers, number lists and closeness numbers. A real list has hundreds or thousands of numbers, not 3. Each step says who does the work, and that worker is dark in the row at the top.

Step 1 of 8

4Who cuts the PDF into chunks?

Your own code does, with a small program. The LLM is not involved.

One page of the handbookPhones can be returned within 7 days. They must be unopened and in the original box. Laptops follow the same rule. Faulty items are replaced within 30 days. Bring the bill to any store. Refunds take 5 to 7 working days.
Chunk 1Phones can be returned within 7 days. They must be unopened and in the original box. Laptops follow the same rule.
Chunk 2Laptops follow the same rule. Faulty items are replaced within 30 days. Bring the bill to any store.
Chunk 3Bring the bill to any store. Refunds take 5 to 7 working days.

Made-up text. Here one chunk is 3 sentences and the overlap (highlighted) is 1 sentence, so each highlighted sentence appears in two chunks. A real splitter uses bigger sizes, like 500 tokens with 50 tokens of overlap (our example numbers).

A simple splitter in JavaScript, which cuts by words:

function split(text, size, overlap) {
  const words = text.split(" ");
  const chunks = [];
  for (let start = 0; start < words.length - overlap; start += size - overlap) {
    chunks.push(words.slice(start, start + size).join(" "));
  }
  return chunks;
}

console.log(split("a b c d e f g h i j", 4, 1));
// [ 'a b c d', 'd e f g', 'g h i j' ]

Each chunk is 4 words, and "d" and "g" appear twice: that is the overlap of 1 word. This splitter counts words to keep the example short. Real splitters usually count tokens, like the 500 above. The idea is the same.

5Who makes the embeddings, and who does the search?

A separate embedding model, usually a small one, makes the lists. A vector database does the search. The LLM does neither.

An LLM does have embeddings inside it, but they are not the lists RAG uses:

The five workers, and what each worker does:

More about the embedding model and the database:

6Does RAG use a smaller model, and is the LLM the only cost?

Yes, the embedding model is usually much smaller than the LLM, because its job is easier. No, the LLM is not the only cost: the embedding model costs money too, but far less.

Why a small model is enough:

What costs money:

Input cost on Claude Opus 5.5 ($4 per 10 lakh tokens). Our example: a 500-page PDF and 3 chunks of about 500 tokens.
Paste the whole PDFRAG: paste 3 chunks
Tokens sent per question3,00,000about 1,500
Cost of one question$1.20$0.006
1,000 questions a day$1,200$6
Embedding the PDF (setup, once)noneabout $0.0067

7When does RAG give a wrong answer?

RAG gives a wrong answer when the search misses the right chunk. The LLM can only use what your code puts into the prompt, so a missed chunk means a missing fact.

So far, a model only reads text and writes text. Can it actually do things, like check the weather or send an email? Lesson 10 starts there.

← Lesson 8  ·  All lessons  ·  Next lesson →

Where this comes from

  • The return-policy example: dump everything vs find the related part; split into rough chunks; embed the query and the chunks; top-k nearest; the LLM ignores an unrelated "electronic items disposal" chunk: the course transcript (notes/course-transcript-llm-basics.md), Part 6. The store, Bazaar Box, and all its policy text are made up.
  • RAG = retrieval, then grounded generation; chunk, embed, store in a vector database; the prompt is the question plus retrieved text: Hands-On Large Language Models (Alammar, Grootendorst; O'Reilly 2024), ch.8, PDF p.316-322 and p.349-352 (figures 8-24 and 8-26). Rahul's course blueprint, Module 9.1-9.2 (ingest once, query each request; "literally string concatenation, plus a smart search").
  • Chunking by size or paragraph, with overlap; sentences too small: Hands-On Large Language Models, ch.8, PDF p.330-333 (figures 8-8 and 8-9: character, token and overlapping-token splits; sentence, paragraph and overlapping-window chunks). Blueprint 9.2. The 500-token size and 50-token overlap are our example numbers. The split function was run with Node.js on 2026-10-31 and printed the output shown. The same loop, run over 3,00,000 tokens with size 500 and overlap 50, gives 667 chunks holding 3,33,300 tokens.
  • Anthropic has no embedding model and points to Voyage AI; voyage-4-lite gives 1,024 numbers by default: Claude docs, "Embeddings": "Anthropic does not offer its own embedding model. One embeddings provider with a wide variety of models and capabilities is Voyage AI by MongoDB." Model table: voyage-4-lite, "1024 (default), 256, 512, 2048", "Optimized for latency and cost". platform.claude.com/docs/en/build-with-claude/embeddings. The claude-api skill does not cover embeddings.
  • voyage-4-lite costs $0.02 per 1 million (10 lakh) tokens: MongoDB, Voyage AI model pricing (voyage-4-lite $0.02, voyage-4 $0.06, voyage-4-large $0.12 per 1M (10 lakh) tokens). mongodb.com/docs/voyageai/management/billing; the same figures at docs.voyageai.com/docs/pricing.
  • all-MiniLM-L6-v2: 384 numbers, 22.7 million (2.27 crore) parameters: its Hugging Face card: "maps sentences & paragraphs to a 384 dimensional dense vector space"; "22.7M params". huggingface.co/sentence-transformers/all-MiniLM-L6-v2. The same model is used in Hands-On Large Language Models, ch.10, PDF p.426-428. Llama 3 8B: 8,030,261,248 parameters (Hugging Face, lesson 2); 8,030,261,248 รท 22,700,000 โ‰ˆ 354, "about 350 times".
  • Embedding models give one list for a whole text; trained for that job: Hands-On Large Language Models, ch.2, PDF p.100-102 (text embedding models; all-mpnet-base-v2 gives 768 numbers); ch.10, PDF p.399-402. An LLM's token embeddings: lesson 4.
  • Closeness numbers vs lesson 4's gap: the Claude docs "Embeddings" page says Voyage embeddings work "with dot-product similarity, cosine similarity, or Euclidean distance" (same URL as above). Lesson 4 used a gap (sum of differences); a closeness number such as cosine similarity is bigger when lists are closer, with 1 as the highest.
  • Computing embeddings still runs a model; only the distance maths is cheap: Rahul's blueprint, Part 5 (fact-check log, L22).
  • Vector databases; Postgres + pgvector: Hands-On Large Language Models, ch.8, PDF p.333-334 (vector databases such as Weaviate and Pinecone). Blueprint 9.3 (Pinecone, Qdrant, Weaviate, Milvus, Chroma, Postgres + pgvector).
  • Search always returns something; a closeness cut-off; exact-word search alongside: Hands-On Large Language Models, ch.8, PDF p.328-330 ("What is the mass of the moon?" still returns results; set a threshold; hybrid search for exact phrases). Blueprint 9.4.
  • Answers that name their source page: Hands-On Large Language Models, ch.8, PDF p.319 (figure 8-3, answers with numbered citations). Page 12 matches lesson 10's example.
  • Prices and context window: Claude Opus 5.5 $4 input / $20 output per million (10 lakh) tokens, 1M (10 lakh) context; Anthropic Claude API model table via the claude-api skill, cached 2026-10-06. Dollars are not converted to rupees.
  • The worked sums (checked with a Node.js script on 2026-10-31): 600 ร— 500 = 3,00,000 tokens. 3,00,000 ร— $4 รท 10,00,000 = $1.20; ร— 1,000 = $1,200. 1,500 ร— $4 รท 10,00,000 = $0.006; ร— 1,000 = $6. 3,00,000 รท 1,500 = 200. 4 ร— 3,00,000 = 12,00,000. Chunks: a new one every 450 tokens gives 667 chunks; 667 โˆ’ 3 = 664. Embedding the PDF: 3,33,300 ร— $0.02 รท 10,00,000 = $0.0067. Embedding questions: 10,000 ร— $0.02 รท 10,00,000 = $0.0002. $4 รท $0.02 = 200.
  • Made up on purpose: Bazaar Box, every policy line and chunk, the number lists, the closeness numbers (0.91, 0.84, 0.81, 0.63, 0.57, 0.22), the page numbers (12, 13, 47), the 0.5 cut-off, the "Gadget care" and "BX-40912" examples, the bot's answer, 600 tokens a page (our estimate, stated in section 1), 500 tokens a chunk and about 1,500 tokens for the RAG prompt, 10 tokens a question, the 100-token answer, the textbook page 112. All web pages accessed 2026-10-31.