Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 4 · 3. Data Ingestion & Preparation
Lesson 26 of 68

Token-Aware & Syntax-Aware Splitting

Chunks must respect model limits and language rules.

Derpx0610 Jan · 1 min read
LangChain Mastery
The Document Object & MetadataLoaders: PDF, HTML, Text, WebCleaning & NormalizationChunking: Why It ExistsRecursiveCharacterTextSplitterToken-Aware & Syntax-Aware SplittingMarkdown & Code SplittingMetadata Enrichment StrategiesDeduplication via Content HashingLazy Loading & Generators
Contents
1. The Character Count Trap2. TokenTextSplitter3. Syntax-Aware (Code) Splitting4. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
The Document Object & MetadataLoaders: PDF, HTML, Text, WebCleaning & NormalizationChunking: Why It ExistsRecursiveCharacterTextSplitterToken-Aware & Syntax-Aware SplittingMarkdown & Code SplittingMetadata Enrichment StrategiesDeduplication via Content HashingLazy Loading & Generators
Contents
1. The Character Count Trap2. TokenTextSplitter3. Syntax-Aware (Code) Splitting4. Summary

1. The Character Count Trap

You split your text at 1000 characters. You think you are safe. But 1000 characters could be 200 tokens (simple English) or 800 tokens (code/unicode). If your embedding model has a hard limit of 512 tokens, your "safe" character chunks might crash the API.

2. TokenTextSplitter

To be safe, we split by tokens, not characters.

token_split.py
from langchain_text_splitters import TokenTextSplitter

splitter = TokenTextSplitter(
  chunk_size=500, # Actual tokens
  chunk_overlap=50
)

# This guarantees compatibility with the model
docs = splitter.split_text(long_text)

Trade-off: It is slower (needs to run a tokenizer) and it might split words in half (meaning visual cut), but it guarantees mathematical safety.

3. Syntax-Aware (Code) Splitting

Splitting code is harder. If you split a Python function in the middle: Chunk 1: def calculate_tax(income): Chunk 2: return income * 0.2

Chunk 2 has lost the context of what it is calculating. It is orphaned code. We need Language-Aware splitting.

code_split.py
from langchain_text_splitters import RecursiveCharacterTextSplitter, Language

py_splitter = RecursiveCharacterTextSplitter.from_language(
  language=Language.PYTHON,
  chunk_size=500
)

# It knows to split at "def", "class", and indentations.

4. Summary

Don't assume characters map to tokens. Don't split code like prose. Use the splitter designed for your data type.

Key Intuition: "Split code by logic, text by tokens."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonRecursiveCharacterTextSplitterNext lesson →Markdown & Code Splitting