New Tool Radar

AI Concepts

How to Split Markdown Documents on Headings for RAG

Split Markdown documents on their headings for RAG so each chunk keeps one section together.

Schematic illustration of split markdown documents on headings for rag: step one find the headings, step two split on structure, step three keep the heading with the chunk.

When a document has structure, such as Markdown headings, split along that structure. Structural boundaries keep related content together, and they let you capture the structure itself as metadata. A fixed-size split can cut a section in half; a heading split keeps each section whole, so the retriever returns complete ideas.

Prerequisites

  • Markdown documents with heading structure.
  • A structure-aware splitter, such as a Markdown text splitter.
  • A token counter, so a very long section can still be split by size.

Steps

Step 1: Find the headings

List the heading levels in your documents: one top heading, then section headings, then subsections. Note which level marks a real topic change in your documents.

Step 2: Split on structure

Split the document at the headings, so each chunk is one section. If a section is longer than your chunk target, split it further by paragraphs inside the section, never across sections.

Step 3: Keep the heading with the chunk

Store the section heading as the chunk’s title or metadata. When the chunk is retrieved, the model sees which section it came from, which helps it judge whether the chunk answers the question.

Worked example: Bluebell Bakery staff handbook

The fictional Bluebell Bakery handbook has 4 sections under one title. Splitting on headings gives 4 chunks.

Chunk Heading kept Tokens Topic
1 Opening the shop 310 Opening routine
2 Food safety 480 Hygiene rules
3 Handling allergens 420 Allergen list
4 Closing the shop 290 Closing routine

The handbook becomes 4 chunks totalling 1,500 tokens, each carrying its heading, and no section is split across two chunks.

Expected result. Each chunk is one whole section with its heading attached, and the chunk count matches the section count. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • Documents without headings need a different splitter; this guide only fits structured documents.
  • A section much longer than the chunk target still needs splitting inside the section.
  • Heading text is metadata, not content; a chunk that is only a heading is a sign the split went wrong.

Sources

See all