Picture a RAG chatbot for a bike rental help center. Someone asks about damage to a cargo bike, and the bot confidently answers "We do not charge for small scratches". The sentence is correct, just for a completely different product. The embedding model is fine, the vector database is fine. The problem happened earlier, when the documents were cut into pieces.
That step is called chunking, and in my experience it is the part of a RAG project nobody looks at until the answers get bad. I wrote a detailed guide on RAG chunking with TypeScript that builds a complete Markdown chunker; here are the core ideas.
Why the way you split documents decides answer quality
In retrieval-augmented generation you search your own documents and place the best matches into the prompt. Each searchable piece becomes one embedding. Storing a whole handbook as one vector works badly, so you split it, and the size of those pieces is a real trade-off:
- Small chunks match precise questions well, but often lose their context. "After the third extension you have to bring the bike back" means little on its own.
- Large chunks carry more context, but dilute the embedding and cost tokens. Ten chunks of 1,000 tokens means 10,000 input tokens per question, even if the answer is three sentences.
There is no universal number. The default strategy in Amazon Bedrock Knowledge Bases creates chunks of roughly 300 tokens and respects sentence boundaries, which is a reasonable starting point. FAQ-style content often works with 200 to 400 tokens, narrative texts can use more.
Four strategies, and the one I prefer for docs
You will mostly meet four approaches:
- Fixed size with overlap – cut every N tokens and repeat 10 to 20 percent at the boundary. More overlap mostly means paying twice for the same text.
- Structure-aware – cut at headings first, then paragraphs, then sentences, and treat code blocks and tables as units that are never split.
- Hierarchical – search small child chunks, then hand the larger parent chunk to the model.
- Semantic – detect topic shifts between neighbouring sentences.
For well-structured Markdown documentation, structure-aware chunking is my favourite. The authors already did the hard work by writing headings; you just have to use them. I am rather skeptical about semantic chunking for that kind of content, because a structure-aware chunker already cuts in the right places, reproducibly and without model costs. Long unstructured texts like minutes or contracts are a different story.
Give every chunk its address
A chunk without context is like a page that fell out of a binder. Anthropic described this in 2024 as "Contextual Retrieval": a model writes a short context sentence for each chunk before embedding. In their tests that reduced failed top-20 retrievals by 35 percent, and by 49 percent combined with BM25 keyword search.
A much cheaper first step, which needs no model call at all, is to prefix each chunk with its heading path:
interface Chunk { id: string; // hash of source + content, stable across edits headingPath: string[]; text: string; } function toEmbeddingInput(chunk: Chunk): string { // e.g. "Help Center > Returns > What if the bike is damaged?" return chunk.headingPath.join(" > ") + "\n\n" + chunk.text; } Two details matter here: the heading path counts against your token limit, because it lands in the embedding too. And if a table has to be split, every part gets its header row again; otherwise nobody knows whether "100 euros" is the price or the deposit.
Measure before you embed a thousand documents
The most common mistake I see is a chunk size that was guessed once and never checked. Before embedding anything, I generate a small report: how many chunks, and what the smallest and largest look like. Other classic problems are code blocks cut in half, PDF headers like "Page 3 of 17" repeated in every chunk, and IDs based on a running number that all shift when someone adds a paragraph at the top.
My advice: start simple with structure and heading paths, test with real questions, and only invest in model-written context or reranking once your measurements say you need it.
What the full article covers
- A structure-aware Markdown chunker in TypeScript that protects code blocks and tables
- A chunk report to validate your settings before embedding
- Tests with the built-in Node.js test runner
- A Lambda function with Serverless Framework v4 that chunks new files from S3 into JSONL
- Costs, security notes and an FAQ including Bedrock Knowledge Bases
Read the complete guide with all code: RAG Chunking: Split Documents Right with TypeScript
📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.
👉 Next step: Not sure which AWS service fits your use case? My AWS service finder asks a few questions and points you to the right one.
📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).
Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en