liveBook · Manning

Text chunk

As explained in Build an AI Agent (From Scratch)

Text chunking is the division of source text into smaller segments for storage, retrieval, or processing.

A text chunk is a smaller search unit cut from a document. Embedding a whole document can exceed a model’s token limit, blur its different topics into one vector, and return more text than an agent needs. Embedding chunks separately lets vector search retrieve the relevant portions. section 5.3.2

Choose boundaries and size with retrieval in mind:

  • Too-small chunks lose context; too-large chunks mix topics. A starting range is 200–1,000 tokens, adjusted to the document. section 5.3.2
  • Fixed-length cuts can split a thought. Share roughly 10%–20% of each chunk with its neighbor to preserve context across boundaries. section 5.3.2
Listing 5.3 Fixed-length chunking function
def fixed_length_chunking(text, chunk_size=500, overlap=50):
    """Split text into fixed-length chunks."""
    chunks = []
    start = 0

    while start < len(text):
        end = start + chunk_size
        chunk = text[start:end].strip()
        if chunk:
            chunks.append(chunk)
        start = end - overlap if end < len(text) else end  #1

    return chunks

The Python function slices from start to start + chunk_size, then advances to end − overlap until it reaches the document’s end. Keep overlap smaller than chunk_size so the start position advances. section 5.3.2

Retrieve relevant web-search passages

Fetch full web-search pages, split each page into 500-character chunks with 50 characters of overlap, and retain its title and URL with every chunk. Embed the chunk texts, then search for the three most similar to “quantum computing.” In the exercise, the selected passages used 304 tokens instead of the full results’ 37,312. section 5.3.5 section 5.6

How Build an AI Agent (From Scratch) covers it

How deep it goes
Implements fixed-length slicing with overlap and explains size trade-offs; leaves optimal size dependent on document characteristics.
Examples
Tests overlap on 283 characters and retrieves quantum-related passages from chunked web-search results.
What it assumes
Uses Python slicing and loops while introducing chunking through the problems caused by embedding whole documents.
What you can do after
Build it from scratch with a Python chunking function, then apply it to embedding and searching web-page passages.
How it connects
Links text chunks to embeddings, vector search, cosine similarity, and retrieval-augmented generation.

Also explained in

Compare how 19 books explain text chunk →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.