Skip to content
Skip to lesson
Back to Topics
LearnCore LLM FoundationsChunking Strategies
🔍MediumRAG & Retrieval

Chunking Strategies

Turn clean documents into retrieval units that preserve answers, citations, and measurable search quality.

22 min read
Learning path
Step 58 of 199 in the full curriculum

Personalize this lesson

Adapt explanations and teaching visuals to your background and preferred voice.

In File Ingestion for AI, you already turned incident-runbook-v3.pdf into a located, checksummed record:

incident-runbook-v3.pdf, page 7: failure: page on-call within 15 minutes with deploy ID.

To answer from that sentence, the generator needs its condition, deadline, and required ID together in the returned context. Split it so one chunk has "Rollback failure" and another has "15 minutes with deploy ID," and a single search hit can retrieve only part of the rule.

is the cut that turns those records into searchable evidence units. In a system, a retriever selects passages from an external index and the generator answers from those passages.[1] Your chunk boundaries decide what a retrieved passage can prove.

Two size-6 windows overlap by two words: the first ends after “15,” and the next reuses “within 15” before completing “minutes with deploy ID.” A search hit on the first window carries the condition and number but not the complete deadline or ID. A heading-aware section keeps the rule and page=7 locator together.
The second six-word window reuses “within 15” and adds “minutes with deploy ID.” A hit on the first carries the condition and number but lacks the unit; both windows retain the page 7 locator.

What a useful chunk must preserve

Before comparing splitters, define success in answer terms. Suppose an engineer asks, "A rollback failed after deploy pay-742. When do I page, and what ID do I include?"

Candidate retrieval unitSearchable?Answerable?Problem
Rollback failure: page on-call withinyesnodeadline and required ID are missing
15 minutes with deploy ID. Routine deploy notes: archive within 14 days.yesnocondition is missing and a competing rule is present
Rollback failure: page on-call within 15 minutes with deploy ID.yesyespreserves condition, deadline, and required ID

For this question, a complete retrieval unit must connect the rollback condition to the deadline and required identifier.

The final context can contain one complete chunk or several complementary passages. Smaller fragments can be useful search candidates when retrieval reliably supplies their missing context through parent expansion or another explicit assembly step. Check the combined, permitted source passages under the generation budget; neither a parent pointer nor a hope that another hit will appear establishes sufficiency. The first lab deliberately evaluates one chunk at a time.

check-answerable-chunks.py
from dataclasses import dataclass

@dataclass(frozen=True)
class Chunk:
    chunk_id: str
    text: str
    source_id: str
    locator: str

def is_answerable(chunk: Chunk) -> bool:
    required = ["rollback failure", "15 minutes", "deploy id"]
    lowered = chunk.text.lower()
    return all(phrase in lowered for phrase in required)

chunks = [
    Chunk("broken-condition", "Rollback failure: page on-call within", "incident-runbook-v3.pdf", "page=7"),
    Chunk("broken-window", "15 minutes with deploy ID. Routine deploy notes archive within 14 days.", "incident-runbook-v3.pdf", "page=7"),
    Chunk("complete", "Rollback failure: page on-call within 15 minutes with deploy ID.", "incident-runbook-v3.pdf", "page=7"),
]

results = [is_answerable(chunk) for chunk in chunks]
for chunk, ok in zip(chunks, results):
    print(f"{chunk.chunk_id}: answerable={ok}")
assert results == [False, False, True]
Output
broken-condition: answerable=False
broken-window: answerable=False
complete: answerable=True

A substring check is only a fixture test, not a general answerability detector. Rollback failure: do not page within 15 minutes with deploy ID passes it while reversing the instruction. Review negation, exceptions, and which condition governs each value. If this chunk is the only passage the generator sees, can it cite the paging rule without guessing?

So what happens if you ignore the heading ingestion already preserved and cut the page by word count instead?

Fixed windows show the boundary failure

Start with a mechanical baseline: windows of . If a window has size NN and overlap OO, the next window starts after N−ON - O tokens. Overlap repeats boundary text, which can help continuity, but it can't guarantee that a complete policy rule survives.

The labs below use whitespace-separated words as visible token stand-ins. A production pipeline should measure the final input with the tokenizer used by its model, including added headings, prompts, and special tokens. Word counts aren't embedding-token counts. Oversized inputs may be rejected or truncated, depending on the model and API. Check before encoding; don't rely on either behavior to preserve a rule.

Predict the first window before running the splitter. The two-rule fixture has 17 words. With N=6N = 6, that first window reads Rollback failure page on-call within 15. The cut falls between the number 15 and its unit minutes, already breaking the paging rule.

split-fixed-windows.py
def fixed_windows(text: str, size: int, overlap: int) -> list[str]:
    if size <= 0 or overlap < 0 or overlap >= size:
        raise ValueError("require size > overlap >= 0")
    words = text.split()
    step = size - overlap
    chunks = []
    for start in range(0, len(words), step):
        end = min(start + size, len(words))
        chunks.append(" ".join(words[start:end]))
        if end == len(words):
            break  # Don't emit a tail already covered by this window.
    return chunks

policy = (
    "Rollback failure page on-call within 15 minutes with deploy ID. "
    "Routine deploy notes archive within 14 days."
)

for index, chunk in enumerate(fixed_windows(policy, size=6, overlap=2)):
    print(f"{index}: {chunk}")
Output
0: Rollback failure page on-call within 15
1: within 15 minutes with deploy ID.
2: deploy ID. Routine deploy notes archive
3: notes archive within 14 days.

Window 3 (notes archive within 14 days.) exposes a second failure: a tail can lose the condition that gives a value meaning. The loop stops once a window reaches the end, so it doesn't emit an extra, fully duplicated days. chunk. Removing that redundancy still doesn't make the tail sufficient evidence. Rebalance the boundary, expand to its parent before generation, or send it for review. Merging without checking length can break the size cap.

Now measure what overlap bought you. The rollback rule is ten words long; no seven-word window can contain it, however far adjacent windows overlap. Repetition may help shorter facts cross a boundary, but it can't enlarge a window.

measure-overlap-cost-and-coverage.py
def fixed_windows(text: str, size: int, overlap: int) -> list[str]:
    if size <= 0 or overlap < 0 or overlap >= size:
        raise ValueError("require size > overlap >= 0")
    words = text.split()
    step = size - overlap
    chunks = []
    for start in range(0, len(words), step):
        end = min(start + size, len(words))
        chunks.append(" ".join(words[start:end]))
        if end == len(words):
            break
    return chunks

def carries_rollback_rule(text: str) -> bool:
    lowered = text.lower()
    return all(term in lowered for term in ["rollback failure", "15 minutes", "deploy id"])

policy = "Rollback failure page on-call within 15 minutes with deploy ID. Routine deploy notes archive within 14 days."
for overlap in [0, 2, 4]:
    chunks = fixed_windows(policy, size=7, overlap=overlap)
    complete = sum(carries_rollback_rule(chunk) for chunk in chunks)
    indexed_words = sum(len(chunk.split()) for chunk in chunks)
    print(f"overlap={overlap}: chunks={len(chunks)} indexed_words={indexed_words} complete={complete}")
    assert complete == 0
Output
overlap=0: chunks=3 indexed_words=17 complete=0
overlap=2: chunks=3 indexed_words=21 complete=0
overlap=4: chunks=5 indexed_words=33 complete=0
Three interval maps show size-7 windows over the same 17 runbook words. Overlap 0, 2, and 4 store 17, 21, and 33 words in 3, 3, and 5 chunks. A strip under each map darkens where words repeat across windows. The rollback rule spans words 1 to 10, so no seven-word window holds the whole rule at any overlap.
Overlap 0, 2, and 4 index 17, 21, and 33 words across 3, 3, and 5 chunks. Stronger coverage fills mark words that appear in more windows. The ten-word rule exceeds each seven-word window; these counts describe the fixture, not corpus quality.

For a long document, ignoring end effects, overlap fraction r=O/Nr = O/N multiplies window count and processed text by approximately 1/(1−r)1/(1-r). A 20% overlap therefore costs about 1.251.25 times as many windows. The short fixture has large end effects, so use its exact counts rather than that approximation.

🔬 Research insight: Bennani and Moslonka report no measurable answer-quality gain from overlap in their Natural Questions experiments using SPLADE retrieval and Ministral-8B generation.[2] Their result is specific to that Wikipedia setup, and they tie it to sentence-aware splitting and a sparse retriever. Compare complete evidence and duplication on your own labeled runbook questions.

Ingestion already kept headings and source locations. If word windows can't reconstruct the rule, those headings give us a better boundary to test.

Prefer policy structure when you have it

Overlap made more copies but never restored the rule. Use the source's own structure when it tells you which text belongs together.

LangChain's RecursiveCharacterTextSplitter is a generic-text baseline. It tries separators in order, defaulting to ["\n\n", "\n", " ", ""]: paragraph breaks, line breaks, spaces, then individual characters.[3] There's no sentence-boundary detector in that default list. Languages without space-separated words need different separators or a language-aware splitter.

That splitter measures size with length_function, which defaults to Python len (characters), not embedding tokens. Start with explicit heading boundaries when the source provides them. Use the smaller-cut fallback only for sections that still exceed your size limit, and measure that limit with the embedding tokenizer.

split-markdown-policy-sections.py
from dataclasses import dataclass

@dataclass(frozen=True)
class PolicyChunk:
    heading: str
    body: str
    indexed_text: str
    locator: str

def headed_chunks(markdown: str, source_locator: str) -> list[PolicyChunk]:
    chunks: list[PolicyChunk] = []
    heading = "Document"
    body: list[str] = []

    def flush() -> None:
        if any(line.strip() for line in body):
            body_text = "\n".join(body).strip()
            chunks.append(
                PolicyChunk(
                    heading,
                    body_text,
                    f"{heading}\n{body_text}",
                    f"{source_locator};section={len(chunks)}",
                )
            )
        body.clear()

    for line in markdown.strip().splitlines():
        if line.startswith("## "):
            flush()
            heading = line[3:]
        else:
            body.append(line)
    flush()
    return chunks

policy = """## Rollback failure
Page on-call within 15 minutes with deploy ID.
## Routine deploy notes
Archive within 14 days after release."""

for chunk in headed_chunks(policy, "page=7"):
    print(chunk.indexed_text.replace("\n", " | "), "|", chunk.locator)
Output
Rollback failure | Page on-call within 15 minutes with deploy ID. | page=7;section=0
Routine deploy notes | Archive within 14 days after release. | page=7;section=1

Each chunk's indexed_text includes its heading, so searchable text keeps the condition attached to the deadline. This small parser handles flat ## sections only, not arbitrary Markdown with nested headings or fenced code. Section ordinals distinguish repeated heading names within this version; they're application metadata, not native PDF anchors. In production, retain the ingestion record's raw-to-normalized offset map and page coordinates rather than rebuilding provenance from heading text.

Tables create the next boundary test. Splitting a row away from its column names turns exact information into ambiguous fragments.

keep-policy-table-with-header.py
table = [
    "| Condition | Window | Required evidence |",
    "| --- | --- | --- |",
    "| Rollback failure | 15 minutes | Deploy ID |",
    "| Routine deploy notes | 14 days | Release summary |",
]

def table_chunk(lines: list[str], heading: str) -> dict[str, str]:
    if len(lines) < 3:
        raise ValueError("need column names, separator, and at least one row")
    return {
        "heading": heading,
        "text": heading + "\n" + "\n".join(lines),
        "quality_check": "header_present" if lines[0].startswith("| Condition |") else "review",
    }

chunk = table_chunk(table, "Runbook windows")
print(chunk["heading"], chunk["quality_check"], f"rows={len(table) - 2}")
print("15 minutes" in chunk["text"] and "Deploy ID" in chunk["text"])
assert chunk["quality_check"] == "header_present"
Output
Runbook windows header_present rows=2
True

This check recognizes our fixture's header; it isn't a Markdown table validator. If a table exceeds the budget, repeat its column names with each row group. Keep row spans and the table locator so a value can be traced to its original cell.

For a long section, a hard size cap must remain hard. Fifteen words cut into windows of seven give lengths 7, 7, 1. Merging the last two produces 7, 8, which breaks the cap. The fallback below balances those fifteen words across three children of five words each. It preserves exact substrings and half-open character offsets into the normalized section, but it doesn't promise semantic boundaries.

split-long-section-with-provenance.py
import math
import re

def section_windows(
    text: str, heading: str, source: str, size: int, min_tail_words: int
) -> list[dict]:
    if not 0 < min_tail_words <= size:
        raise ValueError("require 0 < min_tail_words <= size")
    words = list(re.finditer(r"\S+", text))
    if not words:
        return []
    count = math.ceil(len(words) / size)
    base, extra = divmod(len(words), count)
    children = []
    cursor = 0
    for index in range(count):
        length = base + int(index < extra)
        start = words[cursor].start()
        end = words[cursor + length - 1].end()
        children.append({
            "text": text[start:end],
            "heading": heading,
            "source": source,
            "span": (start, end),
            "needs_review": length < min_tail_words,
            "chunk_id": f"{source}:chars={start}-{end}",
        })
        cursor += length
    return children

children = section_windows(
    "Page on-call within 15 minutes with deploy ID. Include the service name and rollback attempt.",
    heading="Rollback failure",
    source="incident-runbook-v3.pdf:page=7;section=0",
    size=7,
    min_tail_words=4,
)

for child in children:
    print(child["chunk_id"], "|", child["heading"], "|", child["text"])
Output
incident-runbook-v3.pdf:page=7;section=0:chars=0-30 | Rollback failure | Page on-call within 15 minutes
incident-runbook-v3.pdf:page=7;section=0:chars=31-58 | Rollback failure | with deploy ID. Include the
incident-runbook-v3.pdf:page=7;section=0:chars=59-93 | Rollback failure | service name and rollback attempt.

The heading and locator on a child record provide ; answerable text still needs the underlying facts. The first child now contains Page on-call within 15 minutes but omits deploy ID. Balancing repaired length without restoring evidence completeness. If the section is too short to meet min_tail_words, it stays under the hard cap and is flagged for review. The cap here counts body words only; the final embedding input needs a separate token-budget check. Before indexing children:

  1. Prepend the section heading (and any required condition) into the string you embed, as in the prepend-section-context lab below, and/or
  2. Treat incomplete children as search candidates only when parent expansion is mandatory on every hit.

Don't return an incomplete child as sufficient evidence merely because a heading or parent pointer exists elsewhere in storage.

The headed chunks look right on the page. Test whether the retriever selects them and whether selected text answers the question.

Evaluate boundaries with labeled questions

Once a chunk looks coherent, test whether retrieval can find it for a labeled question. A complete rule that's never selected won't help the answer.

Start with a tiny, transparent score before involving a database. For the query terms {rollback, failure, deploy, minutes}, the complete rollback-runbook chunk matches all four. A fragment that contains only {rollback, failure} may rank, but it can't answer the question.

ChunkMatching query termsContains answer?
complete rollback rule4 / 4yes
rollback-condition fragment2 / 4no
routine archive rule1 / 4no

This lexical scorer is deliberately simple. It isolates the effect of boundaries. Replace its score with real embeddings after the and expected evidence are stable.

retrieve-complete-evidence.py
import re

def terms(text: str) -> set[str]:
    return set(re.findall(r"[a-z0-9]+", text.lower()))

def retrieve(query: str, chunks: list[dict[str, str]]) -> dict[str, str]:
    query_terms = terms(query)
    return max(chunks, key=lambda chunk: len(query_terms & terms(chunk["text"])))

query = "rollback failure deploy minutes"
chunks = [
    {"id": "rollback-rule", "text": "Rollback failure: page on-call within 15 minutes with deploy ID."},
    {"id": "routine-rule", "text": "Routine deploy notes: archive within 14 days."},
]

hit = retrieve(query, chunks)
print(hit["id"], hit["text"])
assert hit["id"] == "rollback-rule"
Output
rollback-rule Rollback failure: page on-call within 15 minutes with deploy ID.

Now compare two hand-cut fixtures using the same question and expected evidence phrase. This isolates one boundary failure; benchmark library splitters separately on representative documents. Both broken fragments tie on this scorer, which selects the first. Neither contains the complete rule, so the conclusion doesn't depend on that tie.

compare-chunking-configurations.py
import re

def terms(text: str) -> set[str]:
    return set(re.findall(r"[a-z0-9]+", text.lower()))

def top_chunk(query: str, chunks: list[str]) -> str:
    query_terms = terms(query)
    return max(chunks, key=lambda text: len(query_terms & terms(text)))

query = "rollback failure deploy id"
expected = "Rollback failure: page on-call within 15 minutes with deploy ID."
configs = {
    "broken-fixed": [
        "Rollback failure: page on-call within",
        "15 minutes with deploy ID. Routine deploy notes: archive within 14 days.",
    ],
    "section-aware": [
        "Rollback failure: page on-call within 15 minutes with deploy ID.",
        "Routine deploy notes: archive within 14 days.",
    ],
}

for name, chunks in configs.items():
    hit = top_chunk(query, chunks)
    print(f"{name}: complete={expected in hit}")
Output
broken-fixed: complete=False
section-aware: complete=True

One question proves little. This three-case smoke test checks the paging rule, archive deadline, and commander instruction. Extend it with policy exceptions, tables, and boundary failures before comparing real splitters.

run-chunk-regression-suite.py
import re

def terms(text: str) -> set[str]:
    return set(re.findall(r"[a-z0-9]+", text.lower()))

def retrieve(query: str, chunks: list[dict[str, str]]) -> dict[str, str]:
    query_terms = terms(query)
    return max(chunks, key=lambda chunk: len(query_terms & terms(chunk["text"])))

chunks = [
    {"id": "rollback", "text": "Rollback failure: page on-call within 15 minutes with deploy ID."},
    {"id": "routine", "text": "Routine deploy notes: archive within 14 days."},
    {"id": "commander", "text": "Commander handoff requires the active incident channel."},
]
cases = [
    ("rollback failure deploy id", "rollback", "15 minutes"),
    ("routine deploy archive", "routine", "14 days"),
    ("commander incident channel", "commander", "active incident"),
]

passed = 0
for query, expected_id, evidence in cases:
    hit = retrieve(query, chunks)
    ok = hit["id"] == expected_id and evidence in hit["text"]
    passed += int(ok)
    print(query, "PASS" if ok else "FAIL")
print(f"summary={passed}/{len(cases)}")
assert passed == len(cases)
Output
rollback failure deploy id PASS
routine deploy archive PASS
commander incident channel PASS
summary=3/3

When you replace this lexical baseline with embeddings, retain the two checks: correct evidence source and sufficient returned text. These fixed fixtures can assert chunk IDs. Across splitter configurations, label stable source versions and answer-bearing spans instead, since chunk IDs and boundaries change. Score the final context after any parent expansion, under the same generation-token budget. Equal top-k isn't equal context when chunks have different lengths. Also measure duplicate source spans, retrieval latency, and index size.

Sometimes the right search unit is smaller than the unit a faithful answer needs. That's a different failure from a broken boundary, and it needs a different design.

Search small, answer with enough context

Some questions match a narrow sentence, while a faithful answer needs its surrounding section. A parent-child design indexes small children for search and stores a pointer to the larger source section returned for generation.[4]

The parent can be a section or the whole document. For this runbook, expand to the section, not the entire handbook.

A query about a failed rollback on deploy pay-742 matches the short indexed child 15 minutes with deploy ID. After checks that the link is current, the caller may read the parent, and the section fits the budget, its pointer returns the complete rollback section from page 7 of incident-runbook-v3.pdf.
Page 7 contains the full rollback rule. The index matches a child excerpt, then follows its pointer to return the complete cited section and page locator.
expand-child-match-to-parent-context.py
parents = {
    "rollback": {
        "text": "Rollback failure: page on-call within 15 minutes with deploy ID. Include the service name.",
        "source": "incident-runbook-v3.pdf", "locator": "page=7", "version": 3,
    },
}
children = [
    {"text": "15 minutes with deploy ID", "parent_id": "rollback", "version": 3},
    {"text": "Include the service name", "parent_id": "rollback", "version": 3},
]

def expand(matches: list[dict], allowed_parent_ids: set[str], word_budget: int) -> list[dict]:
    selected = {}
    for match in matches:
        parent_id = match["parent_id"]
        if parent_id not in allowed_parent_ids:
            raise PermissionError("parent access denied")
        parent = parents[parent_id]
        if match["version"] != parent["version"]:
            raise ValueError("stale child-to-parent link")
        selected[parent_id] = parent  # Two child hits need one parent copy.
    if sum(len(parent["text"].split()) for parent in selected.values()) > word_budget:
        raise ValueError("context budget exceeded; select smaller complete evidence")
    return list(selected.values())

context = expand(children, allowed_parent_ids={"rollback"}, word_budget=20)
print(f"child_hits={len(children)} parent_copies={len(context)}")
print(context[0]["text"])
assert "Rollback failure" in context[0]["text"]

failure_cases = [
    ("stale", [{**children[0], "version": 2}], {"rollback"}, 20,
     ValueError, "stale child-to-parent link"),
    ("denied", children, set(), 20, PermissionError, "parent access denied"),
    ("too small", children, {"rollback"}, 10,
     ValueError, "context budget exceeded; select smaller complete evidence"),
]
for name, matches, allowed, budget, expected_error, expected_message in failure_cases:
    try:
        expand(matches, allowed, budget)
    except expected_error as error:
        assert str(error) == expected_message
        print(f"{name}: {error}")
    else:
        raise AssertionError(f"{name} returned context instead of rejecting")
Output
child_hits=2 parent_copies=1
Rollback failure: page on-call within 15 minutes with deploy ID. Include the service name.
stale: stale child-to-parent link
denied: parent access denied
too small: context budget exceeded; select smaller complete evidence

The allowed IDs come from a serving-time authorization decision. Production links need immutable document and chunking versions, and access checks must cover the expanded parent as well as its child. This lab deduplicates parents and rejects oversized context using words for visibility. Use the generator's tokenizer for the real context budget, reserving space for instructions, question, and output. Never silently truncate the deadline to make an expansion fit. The three rejection traces exercise separate invariants: the child points to the current parent, that parent is permitted, and its complete text fits. More child hits don't satisfy any of those checks.

Child windows inside a longer section also benefit from their section label. Test contextual headers on your labeled set and compare retrieval quality before adopting them.

prepend-section-context.py
def indexed_text(heading: str, text: str, source: str) -> str:
    return f"Source: {source}\nSection: {heading}\n{text}"

child = indexed_text(
    heading="Rollback failure",
    text="Page on-call within 15 minutes with deploy ID.",
    source="Incident Runbook",
)
print(child)
assert "Section: Rollback failure" in child
Output
Source: Incident Runbook
Section: Rollback failure
Page on-call within 15 minutes with deploy ID.

Measure the full string printed above. The heading and source label consume budget along with the body. Keep this retrieval representation separate from the original evidence span so an added label doesn't become a fabricated source quotation.

Some leftover failures aren't a size problem. A paragraph switches topic, or a local span is unclear without a name that appeared pages earlier. Those are candidates for a more expensive splitter after measuring the baseline.

Semantic boundaries and late chunking

Structure-aware chunks handle many handbooks and policy pages. Some corpora force different choices:

Failure after measuring baselineCandidate experimentWhat must still be checked
one paragraph shifts between multiple topicssemantic boundary detectionhard size cap and labeled-query score
tiny match lacks surrounding explanationparent-child expansiondeduplication and generation budget
short child is ambiguous without its sectioncontextual headerretrieval comparison against no-header baseline
meaning depends on far-away document contextlate chunkingmodel support, latency, and measured retrieval gain

Bennani and Moslonka report that sentence-preserving splits matched semantic chunking up to roughly 5k retrieved-context tokens in their Wikipedia/Natural Questions setup.[2] That is a context budget, not a 5k-token chunk size. A headed runbook is a different document. Use the headings first, and treat semantic splits as an experiment for leftover topic shifts.

Semantic chunking proposes boundaries where neighboring sentence representations change sharply. The next lab uses transparent topic vectors, so you can see the boundary without trusting an external embedding service.

Count whole words, not substrings. Otherwise id matches incident, and the cosine stops meaning what you think.

find-semantic-boundary-candidates.py
import math
import re

def vector(sentence: str) -> list[float]:
    words = set(re.findall(r"[a-z0-9]+", sentence.lower()))
    return [
        float(sum(word in words for word in ["rollback", "deploy", "minutes", "id"])),
        float(sum(word in words for word in ["commander", "handoff", "channel"])),
    ]

def cosine(left: list[float], right: list[float]) -> float | None:
    if len(left) != len(right):
        raise ValueError("vector dimensions differ")
    dot = sum(a * b for a, b in zip(left, right))
    left_norm = math.sqrt(sum(a * a for a in left))
    right_norm = math.sqrt(sum(b * b for b in right))
    return dot / (left_norm * right_norm) if left_norm and right_norm else None

sentences = [
    "Rollback failures require deploy IDs.",
    "Page on-call within 15 minutes.",
    "Commander handoff uses the active incident channel.",
]

for left, right in zip(sentences, sentences[1:]):
    similarity = cosine(vector(left), vector(right))
    if similarity is None:
        print("unknown: review boundary")
    else:
        print(f"{similarity:.2f}", "boundary" if similarity < 0.50 else "keep together")
Output
1.00 keep together
0.00 boundary

The first two sentences activate the hand-written rollback/deploy axis, so cosine is 1.00. The commander sentence activates the other axis, giving cosine 0.00. This hand-written example shows threshold behavior. An unfamiliar sentence can map to [0, 0]; its cosine is undefined and gives no evidence of a topic change. With real embeddings, tune the threshold on labeled cases and inspect boundaries that detach exceptions or antecedents.

Late chunking is a different escalation. Günther et al. still run a chunker to get boundary cues. They encode the longer text through the first, then mean-pool the token embeddings that fall inside each span.[5]

The pooled ranges can stay the same while each child vector gains surrounding context. In the paper's example, the representation of "the city" gains information from the earlier name "Berlin." That doesn't insert Berlin into the returned chunk text. A generator receiving only the ambiguous sentence still needs the antecedent through explicit context or parent expansion.

The mean-pooling method needs an embedder that exposes contextual token states. Map source spans to tokenizer offsets and pool the corresponding content tokens, handling special tokens and model-specific normalization deliberately. Whole-document context is available only when the input fits the encoder and its attention pattern allows it. The paper's long-document variant uses overlapping larger windows, so context is still bounded.[5] Compare its retrieval quality, encoding cost, and returned evidence against the baseline.

Averaging independently encoded chunk vectors isn't late chunking: those vectors were already formed without the other chunks' context. Late chunking pools token states after the shared encoder pass. It also differs from ColPali/ColBERT late interaction in the ingestion lesson: this mean-pooling method stores one vector per chunk, while late interaction retains multiple vectors for token-level matching.[5]

The shared input introduces an access boundary too. By construction, a public span encoded alongside restricted text can have a vector influenced by that restricted context. Filtering the returned chunk text leaves that dependence in the vector. Keep encoder contexts within the appropriate permission and trust scope, and retain the source/version of the context used to produce each vector. This risk follows from the encoding mechanism; evaluate extraction attacks separately on the target system.

The same eight-word sentence is split into spans of three, three, and two words. In the early mask, each output position can use input positions from its own span; in the late mask, each output position can use the whole sentence. Dashed rows 4–6 are mean-pooled into the middle chunk vector in both methods.
Eight display words stand in for model tokens. Early encoding limits each state to its 3/3/2 span; late encoding lets each state use the whole sentence. Both methods pool rows 4–6, while late-pooled states carry document-wide context. Cells show availability, not attention weights.

Those cuts answer different failures, and several of them add cost. For this runbook, which of them belong in the first index you would ship?

A release manifest for the runbook

For this runbook, a first release can look like this:

Design choiceInitial decisionEvidence to collect
Default boundaryheading-aware sections, recursive fallbackanswerable-chunk rate and retrieval regression suite
Size budgetmeasure final input with the embedding tokenizerreject or split overflow before encoding; token length ≠ word count
Tableskeep header plus rows togetherexact-value questions preserve correct row meaning
Overlapoff for complete policy sections; test for fallback textindex size, duplicate hits, and labeled-query results
Metadatasource, locator, heading, cited answer can return to original record
Escalationparent expansion before semantic or late chunkingfailure examples that justify extra complexity

The manifest check below enforces required metadata and a prior review decision. It doesn't calculate answerability: quality="ready" must come from the evidence checks, not from the splitter declaring itself successful.

gate-indexable-policy-chunks.py
from hashlib import sha256

chunks = [
    {
        "id": "rollback",
        "text": "Rollback failure: page on-call within 15 minutes with deploy ID.",
        "source": "incident-runbook-v3.pdf",
        "locator": "page=7;section=0",
        "heading": "Rollback failure",
        "quality": "ready",
    },
    {
        "id": "broken",
        "text": "15 minutes with deploy ID.",
        "source": "incident-runbook-v3.pdf",
        "locator": "page=7;section=1",
        "heading": "Fragment",
        "quality": "review",
    },
]

for chunk in chunks:
    chunk["chunk_checksum"] = sha256(chunk["text"].encode()).hexdigest()

required_metadata = ("source", "locator", "heading", "chunk_checksum")
indexable = [
    chunk for chunk in chunks
    if chunk.get("quality") == "ready"
    and bool(chunk.get("text", "").strip())
    and all(chunk.get(field) for field in required_metadata)
]
print(f"indexable={[chunk['id'] for chunk in indexable]}")
print(f"blocked={len(chunks) - len(indexable)}")
assert [chunk["id"] for chunk in indexable] == ["rollback"]
Output
indexable=['rollback']
blocked=1

The chunk checksum detects changes to this text; it isn't the original document checksum and doesn't certify meaning. Retain the ingestion checksum and immutable source version separately. Include the splitter configuration and source span in an index manifest so re-ingestion can replace old chunks rather than mixing versions.

The section ordinals are application locators within this fixture, not clickable anchors embedded in a PDF. This gate checks metadata presence, nonempty text, and the prior quality decision. Production records must also carry the ingestion trust labels and authorization scope, with quarantined evidence blocked and permissions enforced on the final expanded context.

Practice: try to break the boundaries

The manifest records a decision. These changes ask whether that decision still holds when a boundary moves.

  1. Change the fixed-window size to ten and overlap to zero. Predict chunk lengths and complete rollback rules before running it.
  2. Run the balanced fallback on fifteen words with size seven and minimum tail six. Can every child satisfy both length rules? Which rule must win?
  3. Add No action is required. to the semantic lab. What happens to its topic vector, and why shouldn't that alone trigger a split?
  4. In the parent-expansion lab, change one child's version to two, then remove permission, then reduce the budget to ten. Each run should fail before returning context. Restore valid inputs and confirm two hits still produce one parent.

A span can rank well and still fail to prove the paging rule. The repair is a structural cut, a required parent, or a semantic or late-chunking experiment you still have to measure.

Complete the lesson

Mastery Check

Answer every question, then check your score. Score 75% or higher to mark this lesson complete.

1.A backup-recovery query asks: Backup job B873 failed;when do I page,and which identifier is required? Retrieval returns10 minutes with job ID,but omits the clause saying which failure that deadline governs. Which invariant fails?
2.A fixed splitter processes 20 words with size 8 and overlap 3. It stops when a window reaches the source end. Which start offsets and indexed-word total follow?
3.A 23-word document contains a 12-word rule. A size-9 splitter with overlaps0,3,6 indexes23,32,53 words. Every individual window still contains zero complete rules. Which conclusion follows?
4.A Markdown runbook has separate Rollback failure and Routine deploy notes sections,and each complete section fits the embedding budget. The baseline must preserve each condition with its rule in a separate indexed unit,retain citation provenance,and avoid parent-expansion work or duplicated text. Which representation satisfies that contract?
5.A returned table row group must be self-contained in its supplied indexed text,with no later metadata or header expansion. It contains Backup failure | 10 minutes | Job ID but omits Condition | Window | Required evidence. Which correction restores its semantics under this contract?
6.A supplied embedder-tokenizer measurement gives47 body tokens,4 added heading tokens and2 special tokens. The model limit is48. Which ingestion result follows?
7.A labeled case expects chunk rollback and evidence containing 15 minutes. Retrieval returns chunk rollback, but its text ends at page on-call within. How should the regression score it?
8.Two child hits share current permitted parentP containing18 generator tokens. Another hit resolves to current permitted parentQ containing11. The available evidence budget is25. This host returns whole deduplicated parents. Which result follows?
9.A semantic splitter receives neighboring vectors[2,0],[1,1],[0,0]. It proposes a cut for cosine strictly below0.8 and sends undefined cosine to review. What happens at the two boundaries?
10.An embedder encodes an approved public child alongside a restricted parent passage,then pools the public child into one vector. A later filter returns only public text. Which boundary remains unresolved?

10 questions remaining.

Discussion

Questions and insights from fellow learners.

Discussion loads when you reach this section.