How it works
Chunking is the act of splitting a source document into smaller text units that become rows in the chunked Delta table and, after embedding, vectors in Mosaic AI Vector Search.
The exam treats chunking as a constrained optimization problem with four inputs:
- the structural shape of the document (paragraphs, headings, tables, code blocks, dialogues)
- the embedding model's maximum input length
- the generator model's context budget after accounting for the system prompt and the user message
- the retrieval policy expressed as top-k
A chunk too short loses context and forces the generator to rely on multiple co-retrieved fragments; a chunk too long wastes the embedding model's capacity by averaging too many ideas into a single vector and, at retrieval time, burns generator tokens on irrelevant prose. The correct chunk size is the one that lets a single chunk answer most user questions while still leaving room for k retrieved chunks plus the system prompt and the user input inside the generator window.
The embedding model imposes the first hard ceiling. The Foundation Model API databricks-gte-large-en accepts 8192 input tokens, while databricks-bge-large-en accepts 512 tokens.
A chunk that exceeds the embedding model's input length is silently truncated by the model server, which means content past the cutoff is invisible to the index even though it sits in the Delta row. Practitioners verify the cutoff with the model card, then size chunks well below the ceiling so that a header repeated across chunks (a parent document title, a section path) can be prepended without overflowing.
Chunk size is expressed in tokens for cost analysis and in characters for fast Spark UDF processing; a useful approximation for English prose is four characters per token.
The generator's context budget is the second ceiling. A pipeline that retrieves k=5 chunks of 1000 tokens each plus a 400-token system prompt and a 200-token user turn consumes 5600 input tokens before the model writes a single output token.
If the generator is databricks-meta-llama-3-3-70b-instruct with a context window in the tens of thousands of tokens, that allocation is comfortable; if the generator is a smaller chat model with a 4096-token window, the same retrieval policy overflows the context and the request fails.
Chunking decisions therefore propagate to retrieval policy.
Halving the chunk size doubles the number of rows in the index, doubles the embedding cost, and lets the application retrieve more diverse fragments at the same token budget; doubling the chunk size halves index size but risks one chunk dominating the context and crowding out competing evidence.
Document structure is the third input and the one that distinguishes a defensible chunking strategy from a fragile one. Splitting on a fixed character count breaks across sentence boundaries, splits tables, and severs code blocks; the resulting vectors are noisy and retrieval quality drops. Structured splitting respects the document grammar:
- paragraphs for free prose
- headings for technical reference documentation
- list items for FAQs
- dialogue turns for chat logs
- rows for tabular data
The LangChain text splitter family encodes this hierarchy through the RecursiveCharacterTextSplitter, which tries a list of separators in order (double newline, single newline, period, space) so that a chunk break falls at the largest natural boundary that still fits the size constraint.
For Markdown and HTML, the MarkdownHeaderTextSplitter and HTMLHeaderTextSplitter annotate each chunk with the heading path so that the embedding sees not only the body text but the hierarchical context.
Overlap is the fourth control. A chunk that ends mid-sentence loses the antecedent of any pronoun in the next chunk, so retrieval may surface the wrong fragment when a user asks about that sentence.
Adding an overlap of 10 to 20 percent of chunk size carries the trailing context forward, at the cost of duplicating tokens across rows. Overlap is cheaper to add than to remove, because removing it requires re-embedding the entire corpus while adding it only re-embeds the boundary windows.
The exam expects candidates to pick overlap based on whether queries reference cross-boundary continuity (legal contracts, narrative documents) or stand alone (FAQ entries, product catalog rows).
Mapping document structure to splitter choice
For technical reference documentation in Markdown, the MarkdownHeaderTextSplitter extracts headers as metadata, and a downstream RecursiveCharacterTextSplitter enforces a size cap on the body. The combination preserves the heading path (H1 > H2 > H3) as a metadata field on every chunk, which a retriever can use as a coarse filter and the generator can prepend as inline context.
- For HTML pages scraped from product portals, the
HTMLHeaderTextSplitterplays the same role and discards navigation. - For PDF whitepapers extracted as paragraphs of plain text, a
RecursiveCharacterTextSplitterwith separators["\n\n", "\n", ". ", " "]is the default. - For source code repositories, the
Language.PYTHONorLanguage.JAVAenum onRecursiveCharacterTextSplitter.from_languageinjects language-aware separators (class, def, import) so that chunks track function boundaries.
Working example: hierarchical chunking with overlap
from langchain.text_splitter import (
MarkdownHeaderTextSplitter,
RecursiveCharacterTextSplitter,
)
headers_to_split_on = [
("#", "h1"),
("##", "h2"),
("###", "h3"),
]
markdown_splitter = MarkdownHeaderTextSplitter(
headers_to_split_on=headers_to_split_on,
strip_headers=False,
)
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1500,
chunk_overlap=200,
separators=["\n\n", "\n", ". ", " ", ""],
length_function=len,
)
def chunk_markdown(doc_id: str, markdown_text: str) -> list[dict]:
header_chunks = markdown_splitter.split_text(markdown_text)
rows = []
for hc in header_chunks:
body_chunks = text_splitter.split_text(hc.page_content)
for idx, body in enumerate(body_chunks):
rows.append({
"doc_id": doc_id,
"h1": hc.metadata.get("h1"),
"h2": hc.metadata.get("h2"),
"h3": hc.metadata.get("h3"),
"chunk_index": idx,
"content": body,
})
return rows
Sizing rules for common embedding models
databricks-bge-large-en : max 512 tokens -> chunk_size <= 400 tokens
databricks-gte-large-en : max 8192 tokens -> chunk_size <= 1500 tokens typical
OpenAI text-embedding-3 : max 8191 tokens -> chunk_size <= 1500 tokens typical
📊 Limit:
databricks-bge-large-enaccepts 512 input tokens;databricks-gte-large-enaccepts 8192 input tokens. Chunks longer than the cap are truncated silently at embedding time.
💡 Exam Trap: A question that compares two chunking strategies often hinges on whether the embedding model can fit the chunk. A 2000-token chunk passed to
databricks-bge-large-enis truncated to 512 tokens, so the answer that uses a 1500-token chunk withdatabricks-bge-large-enis wrong even if it otherwise sounds reasonable.
💡 Exam Trap: Watch for distractors that increase
chunk_sizewithout raisingchunk_overlap. Larger chunks need proportionally larger overlap to preserve cross-boundary context; halving overlap while doubling chunk size erodes retrieval quality at boundary positions.
⚠️ Anti-Pattern: Fixed-size character splitting on Markdown technical documentation. Splitting every 2000 characters severs code blocks, breaks tables, and discards heading context, producing vectors that look semantically similar to each other but contain incomplete answers. Use structured splitters first, then enforce the size cap.
🎯 Scenario: A team indexes a 600-page PDF policy manual with chapter and section headings. They split on 1000-character windows with no overlap, and users report that the chatbot retrieves a chunk that says "this provision applies to" without naming the provision. The fix is to switch to a heading-aware splitter, attach the heading path as a metadata field prepended to the chunk text, and add a 200-character overlap so that the antecedent of "this provision" is present in both halves.
Decision Anchor: Choose a structure-aware splitter (Markdown, HTML, code) when the document carries machine-readable structural cues. Choose RecursiveCharacterTextSplitter with prose separators when the document is flat text. Choose fixed-size character splitting only when neither structure nor sentence boundaries are reliable, which is rare in production corpora.
