liveBook · Manning

Text chunk

As explained in AI Agents in Action, Second Edition

Semantic chunking is the segmentation of source material into sections that preserve meaningful units of content.

A text chunk is a section of source material kept as a usable unit for retrieval. Semantic chunking aims to preserve meaningful content together, while token-count chunking provides a practical size boundary. section 6.3.2 section 11.1.4

In retrieval-augmented generation, chunking sits between document loading and embedding: documents become chunks, chunks become vectors, and retrieved chunks provide context for the language model. section 6.1.1

A practical chunking workflow is:

Chunking a movie script

A simple implementation reads a movie script, accumulates words, and starts a new chunk when encoding the next word would exceed 200 tokens. The resulting chunks are embedded and indexed in Chroma DB for a script-search tool. section 6.3.2

Listing 6.6 02_RAG_agent_vector.py
script_text = Path("chapter_06/sample_documents/back_to_the_future.txt").read_text(
    encoding="utf-8"
)

def simple_chunk(text, max_tokens=200):    #1
    tokenizer = tiktoken.get_encoding("cl100k_base")
    words, chunk, chunks = text.split(), [], []
    for w in words:
        if len(tokenizer.encode(" ".join(chunk + [w]))) > max_tokens:
            chunks.append(" ".join(chunk))
            chunk = [w]
        else:
            chunk.append(w)
    if chunk:
        chunks.append(" ".join(chunk))
    return chunks

docs = simple_chunk(script_text, max_tokens=200)

client = chromadb.PersistentClient(    #2
    path="./chapter_06/chroma_script_store"  
)
collection_name = "bttf_script"    

try:
    collection = client.get_collection(collection_name)    #3
except Exception:     #4
    collection = client.create_collection(name=collection_name)     #4

if collection.count() == 0:    #4
    collection.add(ids=[str(uuid.uuid4()) for _ in docs], documents=docs)

@function_tool    #5
def search_script(query: str, top_k: int = 3) -> str:
    res = collection.query(query_texts=[query], n_results=top_k)
    if res and "documents" in res and res["documents"] and res["documents"][0]:
        return "\n\n".join(res["documents"][0])
    return "No relevant documents found."


agent = Agent(
    name="Script Agent",
    instructions=(    #6
        "You answer questions about the movie *Back to the Future*.\n" #7
        "When needed, call the `search_script` tool to fetch passages, " #7
        "then cite or paraphrase them in your answer." #7
        "Make sure your answers are grounded in the script text.\n"     #7
    ), #7
    tools=[search_script],
)

query = "Where does Doc tell Marty to meet him, and at what time?"
result = Runner.run_sync(agent, query)
print("\n--- ANSWER ---\n", result.final_output)

query = "What happens at 1:15AM"    #7
result = Runner.run_sync(agent, query)
print("\n--- ANSWER ---\n", result.final_output)

OUTPUT
--- ANSWER ---
 Doc tells Marty to meet him at the Twin Pines Mall parking lot at 1:15 AM.

--- ANSWER ---
 There's no specific event or mention of 1:15 AM in the script passages I retrieved. If you have a particular scene or context in mind, let me know, and I can help further!    #7

Inspect the complete token-limited chunking function and the surrounding vector-store setup. section 6.3.2

How AI Agents in Action, Second Edition covers it

How deep it goes
Explains token-limited implementation and semantic-coherence guidance, but does not detail advanced boundary algorithms.
Examples
Uses a movie script, a 200-token Python-style function, Chroma DB indexing, and retrieved context for question answering.
What it assumes
Assumes basic document handling and language-model retrieval concepts while showing the ingestion sequence explicitly.
What you can do after
Build it from scratch by splitting documents, embedding chunks, indexing vectors, and grounding responses in retrieved context.
How it connects
Connects chunking with RAG, embeddings, vector databases, similarity search, retrieval, and grounding.

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 AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.