SevyDevy

Module 4 · Making AI know your stuff · Lesson 20 of 32

Chunking and vector search

11 minFree GenAI course · BeginnerLesson 4.3By Saket Bhatnagar

Researched and drafted with AI, reviewed and edited by Saket Bhatnagar.

In short

  • Before search, documents are cut into chunks: small pieces about a paragraph or a section long.
  • Each chunk becomes an embedding, and vector search finds the chunks whose meaning sits closest to the question.
  • Chunk size matters. Too small loses context, too big buries the answer. Medium pieces that each hold one whole idea usually win.
Soon

Video lesson · coming soon

Chunking and vector search, explained with a café

Lala, your AI mentor, will walk you through this lesson in a short animated video. Until then, everything is here to read and try.

  1. 01 Can I bring my dog on Sunday?
  2. 02 The librarian’s index cards
  3. 03 Chunk, embed, store, search
  4. 04 Dog finds pets
  5. 05 Too small, too big, or right?
  6. 06 Next: prompting, RAG or fine-tuning?

Asha’s handbook for Chai & Chapters has grown to forty pages: opening hours, pets, refunds, Wi-Fi, parking, book club, allergies. A customer types, “Can I bring my dog on Sunday?” The answer is in there somewhere. But where? And how much of the handbook should the app hand to the model?

In the last lesson, search “found the right chunks” as if by magic. Today we open that box. It comes down to two decisions: how you cut the book, and how you measure which piece is closest to the question.

Why cut the handbook into pieces?

Why not paste all forty pages into every prompt? Three reasons. It may not fit in the context window. Even if it fits, every question pays for thousands of extra tokens. And models often pay less attention to details buried in the middle of a very long prompt.

So we cut the handbook into small pieces, find the few that matter for this question, and send only those.

From chunk to a point on the meaning map

In the embeddings lesson, you saw that a list of numbers can place text on a map of meaning. “Dog” sits near “puppy” and “pet”, and far from “Wi-Fi”. The same trick works for whole paragraphs and whole questions.

So each chunk goes through an embedding model once, and gets its own point on the map. Those points, with the original text attached, go into a special store. When a question arrives, it goes through the same embedding model and becomes a point too.

How a question finds its chunks

The handbook is chunked and embedded once. Each question is embedded with the same model, and the vector database returns the chunks nearest in meaning.

Diagram connections:

  • Café handbook to Chunks (cut)
  • Chunks to Embedding model (embed once)
  • Question to Embedding model (embed)
  • Embedding model to Vector database (store, search)
  • Vector database to Top 3 chunks (nearest)
  • Café handbook: Forty pages of rules, from opening hours to parking.
  • Chunks: One piece per section or paragraph, each making sense alone.
  • Embedding model: Turns text into a list of numbers. The same one is used for…
  • Vector database: Keeps every chunk’s numbers and text, ready to search.
  • Question: Can I bring my dog on Sunday?
  • Top 3 chunks: The closest pieces, which go into the prompt.

How does vector search find the closest meaning?

Now search becomes a question of distance: which chunk points sit nearest the question’s point? The database measures closeness, often with a score called cosine similarity, and returns the best few. A higher score means closer meaning.

Here is the lovely part. The customer typed “dog”. The handbook says “pets”. A keyword search looking for the letters d-o-g finds nothing. Vector search knows that dog and pet live in the same neighbourhood of the map, so the pets section comes up first.

With millions of chunks, comparing the question with every single point would be slow. So vector databases use clever shortcuts that find almost-nearest points very fast, called approximate nearest neighbour search. The idea is enough for now: a tiny bit of accuracy is traded for a lot of speed.

Keyword search and vector search, side by side
QuestionKeyword searchVector search
What does it match?The exact wordsSimilar meaning
Does “dog” find “pets”?NoYes
Good atNames, codes, order numbers, exact phrasesQuestions asked in everyday words
Weak atSynonyms and other ways of saying the same thingExact IDs and rare names

Chunk size: not too small, not too big

Now the decision that quietly makes or breaks RAG: how big should each chunk be? Picture the pets part of the handbook. One line says well-behaved dogs are welcome. A nearby line adds a special rule for Sundays.

Cut too small, and those two lines land in different chunks. Search finds “dogs are welcome”, misses the Sunday rule, and the helper happily says yes. Cut too big, and the Sunday rule is in there, but buried among Wi-Fi details and parking, inside a long and costly prompt.

Try it yourselfFind the Sunday dog ruleTap Tiny, Medium and Huge to cut a short part of the handbook three ways. For “Can I bring my dog on Sunday?”, watch which chunks search finds, their match scores, and whether the answer comes out right.

Medium chunks win here because each one holds a whole idea: the pets rule and its Sunday exception, together, found with an 86% match. Huge chunks match only weakly, around 44%. That is the real goal. Not a magic number, but chunks that each make sense when read on their own.

  1. Cut along the document’s own shape

    Split by headings, sections and paragraphs first. A section on pets should stay one chunk if it fits.

  2. Start at a few hundred tokens

    Many teams begin somewhere around 200 to 800 tokens per chunk, then adjust. Short FAQs need less; dense legal text may need more.

  3. Add a little overlap

    Share roughly 10 to 20 percent between neighbouring chunks, so nothing important falls into the gap.

  4. Keep labels on every chunk

    Store the title, page and date with each chunk. They help search, and let the answer cite its source.

  5. Test with real questions

    Write twenty real customer questions and check that the right chunk lands in the top few. Change one thing at a time.

Check yourself

Check yourself

A customer asks, “Can I bring my puppy?” The handbook only ever says “pets”. Which search finds the right section?

Pick an answer to see why it is right or wrong.

Check yourself

With tiny chunks, the helper says “Yes, dogs are welcome any day!” What went wrong?

Pick an answer to see why it is right or wrong.

Check yourself

Why must the chunks and the questions go through the same embedding model?

Pick an answer to see why it is right or wrong.

Remember these three

  1. Chunking cuts documents into searchable pieces; each piece should make sense on its own.
  2. Chunks and questions become embeddings, and vector search returns the closest few.
  3. Too small loses context, too big buries the answer: test chunk size with real questions.

Questions people ask

What is chunking in RAG?
Chunking is cutting your documents into smaller pieces before storing them for search. Good chunks follow the document’s own sections and are often a few hundred tokens long, so each one holds a complete idea.
What is the best chunk size for RAG?
There is no single best size. Many teams start roughly between 200 and 800 tokens with a little overlap, then test with real questions. The right size keeps each rule and its exceptions in the same chunk.
What is a vector database?
A vector database stores embeddings, the lists of numbers that capture meaning, along with their text. It quickly finds the stored items closest to a new embedding, which is how a RAG app finds relevant chunks.
Is vector search better than keyword search?
Each is better at different things. Vector search understands similar meaning, while keyword search is better for exact names, codes and IDs. Many real systems combine the two, which is called hybrid search.