Context EngineeringLesson 22 of 87

Contents

Context Engineering

Lesson 22 of 87. Sign in to keep your place.

Module 3 · Retrieval as a context pipeline

Chunking that does not destroy meaning

9 min read

Four ways to cut the same policy corpusFixed 512-token split61%Split on structure first72%Structure plus headingpath79%Plus a generated situatingline86%Recall at ten on one forty-thousand-chunk corpus. The numbers are from one measurement and will not beyours; the ordering is what travels. Carrying the heading path down onto every chunk is two lines ofcode, and it is what stops a chunk reading that refunds take fourteen days when the section it camefrom applies only outside the EU.
Recall at ten on one forty-thousand-chunk corpus. The numbers are from one measurement and will not be yours; the ordering is what travels. Carrying the heading path down onto every chunk is two lines of code, and it is what stops a chunk reading that refunds take fourteen days when the section it came from applies only outside the EU.

Figure

The default is wrong for a reason worth understanding

Split at every 512 tokens with 50 tokens of overlap. It is the default in every framework, and it is wrong in a specific way: it treats a document as a string when a document is a structure.

A fixed split cuts tables away from their headers, separates a clause from the section that qualifies it, and orphans every pronoun. The chunk reads fine to a human skimming, which is why the bug is hard to see, and it is missing the exact word that makes it correct.

Here is the failure that costs money. A refund policy opens with “The following applies to customers outside the European Union.” Three pages later, chunk 41 reads “Refunds are processed within 14 days of receipt.” Retrieved alone, that chunk is a confident, well-formed, wrong answer for a customer in Dublin. No overlap setting reaches three pages up.

Split on structure, then on size

The order matters. Find the boundaries the document already has (headings, sections, list items, table rows, code blocks, slide breaks) and only then merge or split to hit a token budget.

Python
def chunk(doc, target=700, floor=200):
    blocks = parse_structure(doc)        # [{level, heading_path, text, kind}]
    out, buf, path = [], [], None
    for b in blocks:
        if b.kind == "table":            # never merge a table into prose
            out += table_rows(b)         # one chunk per row, header attached
            continue
        if path and b.heading_path[:2] != path[:2]:
            out.append(flush(buf, path)); buf, path = [], b.heading_path
        buf.append(b.text); path = path or b.heading_path
        if tokens(buf) >= target:
            out.append(flush(buf, path)); buf = []
    return [c for c in out if tokens(c) >= floor or c.kind == "table_row"]

def flush(buf, path):
    return " > ".join(path) + "\n\n" + "\n".join(buf)

The last line is the important one. Every chunk carries its heading path (Refunds > Outside the EU > Timelines) so the qualifier travels with the text. This is a two-line change that fixes the Dublin bug.

Add the context the chunk lost

Go further: before embedding, prepend one or two sentences that situate the chunk in its document. Anthropic published this as “contextual retrieval” in 2024 and reported, on their benchmark, that contextual embeddings cut top-20 retrieval failures by about 35%, and by about 49% when combined with contextual BM25. Treat the exact figures as theirs rather than yours, and treat the direction as reliable.

Text
This passage is from the 2026 EMEA refund policy, in the section covering
non-EU customers. It follows the eligibility criteria.

Refunds are processed within 14 days of receipt...

You generate that preamble once per chunk with a cheap model, at ingestion. With prompt caching against the full document, the cost is small (well under a dollar per million tokens of corpus on current small models) and it is paid once, not per query.

Index small, return large

A short passage embeds precisely. A long passage answers completely. You do not have to choose: embed the small unit, return its parent.

Index each paragraph or each summary. Store a pointer to the enclosing section. At query time, match on the small thing, retrieve the big thing, deduplicate parents so you do not return the same section four times. Frameworks call this parent-document or small-to-big retrieval; it is thirty lines of code without one.

The cases that break naive splitters

  • Tables. One chunk per row, with the header row re-attached and the table’s caption in the heading path. A table split at row 40 is unreadable to the model and to you.
  • Code. Split on function and class boundaries. Attach the file path and the import block. A function without its imports is missing its type information.
  • Boilerplate. Strip repeated headers, footers, page numbers and navigation before embedding. Otherwise every chunk in a 400-page manual shares 30 identical tokens and the embeddings drift towards each other.
  • Scanned documents. OCR output has no structure to split on. Recover it (heading detection, layout parsing) or accept that this corpus will retrieve badly.
  • Conversations. Split on speaker turns and topic shifts, never on token count. Half a question is worse than nothing.

How to check your work

Sample 30 chunks at random and read them cold, without the source document open. For each, ask: could a careful colleague answer a real question from this alone, and would they know what it applies to?

If the answer is no, no amount of reranking downstream will save you. Retrieval quality is bounded by the quality of the units you built.

Every lesson here is open. Your progress is recorded once you pass the Data, SQL and Getting to the Answer exam.

Check your understanding

A policy corpus is chunked at 300 tokens with 60 tokens of overlap. The system confidently gives EU customers a 14-day refund figure that only applies outside the EU: the qualifier appears once, about 900 tokens earlier under a section heading. A engineer proposes raising the overlap to 150 tokens. What should you expect?

Pick the one you would defend. Your answer stays private.

Next lesson · 9 minGiving each chunk its own context

Stuck on something?Ask Sayali, your AI tutor

How to cite: Addaly (2026) ‘Chunking that does not destroy meaning’, in Context Engineering. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/context-engineering/chunking-without-losing-meaning (Accessed: 31 October 2026).