Lesson 2 of 4 Embeddings

Chunking Text for Embeddings

This lesson explains why long text is chunked, how to choose chunk size and overlap, and how AI_GENERATE_CHUNKS works.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why chunking improves retrieval.
  • Describe the trade-offs of chunk size and overlap.
  • Identify the arguments and output of AI_GENERATE_CHUNKS.

Why chunk

Embedding models accept a limited number of input tokens, and a single vector for a long document averages many topics into one point, so a question about one paragraph matches poorly. Splitting the document into chunks that each cover one idea produces precise matches, and in RAG only the relevant chunks are sent to the chat model, which reduces cost and keeps answers focused.

Select all that apply

Why chunk

Exam skills 3.1.5

Identify the reasons for chunking long documents.

Log in to try this activity Interactive activities are free with an account.

Chunk size, overlap and strategy

Chunk size is a trade-off: small chunks match precisely but lose context, and large chunks keep context but dilute meaning and consume prompt space. Typical sizes are a few hundred to about a thousand tokens. Overlap repeats the end of one chunk at the start of the next so sentences split at a boundary still appear whole in one chunk; 10 to 20 percent is common. Strategies range from fixed-size chunks to splitting on sentences, paragraphs or headings, which keeps units of meaning together. Each chunk can be prefixed with the document title so it carries context when retrieved alone.

The following diagram shows why overlap keeps a sentence whole.

Chunk size and overlap. Small chunks match precisely but lose context; large chunks keep context but dilute meaning and use prompt space, so typical sizes are a few hundred to about a thousand tokens. Without overlap, a sentence split at a boundary appears whole in neither chunk. An overlap of 10 to 20 percent repeats the end of one chunk at the start of the next, so the sentence appears whole in one of them.
Overlap of 10 to 20 percent is common.
Scenario questions

Sizing chunks

Exam skills 3.1.5

Choose the chunking adjustment for each problem.

Log in to try this activity Interactive activities are free with an account.

AI_GENERATE_CHUNKS

AI_GENERATE_CHUNKS is a table-valued function that splits text into fixed-size chunks. CHUNK_SIZE is in characters, OVERLAP is a percentage of the chunk size from 0 to 50, and ENABLE_CHUNK_SET_ID = 1 adds a column that groups the chunks of each source row. It returns chunk, chunk_order, chunk_offset and chunk_length, and requires compatibility level 170.

CREATE TABLE Support.ArticleChunk
(
    ChunkID    int IDENTITY PRIMARY KEY,
    ArticleID  int NOT NULL REFERENCES Support.Article (ArticleID),
    ChunkOrder int NOT NULL,
    ChunkText  nvarchar(max) NOT NULL,
    Embedding  vector(1536) NULL
);

INSERT INTO Support.ArticleChunk (ArticleID, ChunkOrder, ChunkText)
SELECT a.ArticleID, c.chunk_order, CONCAT(a.Title, N': ', c.chunk)
FROM Support.Article AS a
CROSS APPLY AI_GENERATE_CHUNKS(SOURCE = a.Body, CHUNK_TYPE = FIXED,
                               CHUNK_SIZE = 1500, OVERLAP = 15) AS c;

Storing the parent key and chunk order lets search results link back to the document and lets RAG include neighbouring chunks for more context. Chunking can also be done outside the database, for example with document-aware splitters in an application or pipeline, when structure such as headings or tables matters.

Match the pairs

AI_GENERATE_CHUNKS

Exam skills 3.1.5

Match each AI_GENERATE_CHUNKS argument or output with its meaning.

Log in to try this activity Interactive activities are free with an account.
Worked example

Answers cut off at chunk boundaries

A RAG assistant over policy documents often answers incompletely: the relevant rule starts at the end of one chunk and continues in the next, and only one of them is retrieved. Chunks are created with AI_GENERATE_CHUNKS, CHUNK_SIZE = 1000 and no overlap. What should the developer change first?

Add an OVERLAP of around 10 to 20 percent so text near boundaries appears in both chunks, and consider prefixing each chunk with the document title, then re-embed. Overlap directly addresses rules split across boundaries. Dramatically larger chunks would reduce boundary splits but dilute matches and enlarge prompts, and changing the embedding model does not fix text that is cut in half.

Check yourself

In what unit is AI_GENERATE_CHUNKS CHUNK_SIZE measured, and what is OVERLAP?

Show answer

CHUNK_SIZE is in characters; OVERLAP is a percentage of the chunk size from 0 to 50.

Exam Tips

Remember for the exam

  • Chunk long text so each vector represents one idea.
  • Overlap keeps boundary sentences whole; OVERLAP is a percentage from 0 to 50.
  • AI_GENERATE_CHUNKS: CHUNK_TYPE = FIXED, CHUNK_SIZE in characters; compatibility level 170.
  • Store parent key and chunk order with each chunk.