RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /LangChain for Local AI
  6. /Ch. 13
LangChain for Local AI

13. Text Splitters

Chapter 13 of 18 · 20 min
KEY INSIGHT

Text splitters chunk documents for embedding while `chunk_overlap` maintains cross-chunk context—setting chunk_size to 20-30% of your embedding model's context yields the best retrieval results.

Large documents must be chunked before embedding. Text splitters divide documents into smaller pieces that fit within embedding model limits (typically 512-8192 tokens) while preserving semantic coherence.

LangChain provides RecursiveCharacterTextSplitter as the default choice. It splits on paragraph breaks first, then sentences, then words—preserving natural language boundaries.

from langchain.text_splitter import RecursiveCharacterTextSplitter

with open("./article.txt") as f:
    text = f.read()

splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,      # Target tokens per chunk
    chunk_overlap=50,    # Overlap between chunks
    length_function=len,
    separators=["\n\n", "\n", " ", ""]
)

chunks = splitter.split_text(text)
print(f"Created {len(chunks)} chunks")
print(f"First chunk length: {len(chunks[0])}")

The chunk_overlap parameter matters more than most tutorials acknowledge. Without overlap, sentences split across chunk boundaries lose context. With 50-token overlap, a sentence starting at chunk boundary appears in both chunks.

# Verify overlap is working
print(chunks[0][-100:])   # End of chunk 0
print(chunks[1][:100])     # Start of chunk 1 - should overlap

For code repositories, use LanguageSplitter with language-specific separators.

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.text_splitter import Language

python_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.PYTHON,
    chunk_size=100,
    chunk_overlap=20
)

code_chunks = python_splitter.split_text(open("./processor.py").read())
print(f"Code split into {len(code_chunks)} chunks preserving function boundaries")

Token-aware splitting prevents embedding model truncation. Use tiktoken for accurate counting.

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.text_splitter import CharacterTextSplitter

# More accurate token counting
splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,
    chunk_overlap=50,
    length_function=lambda x: len(x) // 4  # Rough estimate: 4 chars per token
)

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Load a 2000+ word article, split it with chunk_size=300 and chunk_overlap=50, then verify that identical text appears at the end of chunk N and start of chunk N+1.

← Chapter 12
Document Loaders
Chapter 14 →
Simple RAG Pipeline