Split documents along their own structure, not at a fixed character count. Keep each chunk to one idea, carry the document title and section path with it, and keep tables and lists whole. Then test retrieval on real questions every time you change the rule, because chunking decides what a RAG agent can find at all.
Why chunking decides what an agent can find
Many RAG systems retrieve document chunks rather than whole documents, then pass the retrieved passages to a model. The retrieval unit depends on the system design. When an answer is split across chunks or buried in one dominated by unrelated text, search may rank it too low to be used.
That makes chunking one of the first design choices in a RAG system, and one of the least visible. An ineffective rule can fail without an obvious error. The agent may answer from a near miss, while the team investigates the prompt or model instead. When an answer looks wrong, measure the search step on its own before changing anything else.
The common ways to split a document
There are several approaches, and a system can combine more than one.
Fixed-size chunks
Cut the text every so many tokens, usually with some overlap between neighbours. It is quick to build and works as a first baseline. Its weakness is that it ignores meaning: a cut can fall in the middle of a sentence, a table or a numbered procedure, leaving each half incomplete.
Recursive splitting
Try to split on large boundaries first, such as headings or blank lines, and only fall back to sentences or words when a piece is still too long. This keeps more passages whole than a fixed cut, for little extra effort, and is a sensible default for plain text.
Structure-aware chunking
Use the document's own structure: headings, sections, clauses, list items and table rows. Microsoft's guidance on the RAG chunking phase recommends working out that structure from headings and subheadings, using a document analysis tool where the format makes it hard. Research on enterprise documents in the oil and gas sector found structure-aware chunking retrieved better than the other strategies tested, at a lower compute cost than semantic chunking.
Semantic chunking
Use an embedding model to find where the topic shifts, and cut there. It can help with long, unstructured text such as transcripts. It costs more to run, and its boundaries are harder to explain when a reviewer asks why a passage was split where it was.
Keep context with every chunk
A chunk that reads "the limit is raised to the higher tier" means little on its own. Which limit, in which policy, for which customers?
Add the document path to the chunk
Prepend the document title and the section headings above the chunk, for example "Expenses policy > Travel > Overseas trips". It is cheap, it needs no model call, and it gives both the embedding and the model the context the chunk lost when it was cut out.
Generated summaries, where they earn their cost
Some teams go further and have a model write a short note on where each chunk sits in the wider document, then store it with the chunk. This can help with documents that have weak headings. It adds a model call for every chunk at indexing time, so try the cheaper document path first and measure the difference.
Small chunks for search, larger ones for the answer
A parent-child setup searches small, precise chunks but passes the larger section around them to the model. Search stays sharp, and the model still sees enough surrounding text to answer correctly. This sits well alongside deciding what goes in the context window.
Content that needs its own rules
Not every part of a document should be split the same way.
- Tables: keep a table whole where it fits, or split by row and repeat the header row in every chunk.
- Numbered procedures: keep the steps together, because step four rarely makes sense without step three.
- Contracts and policies: split by clause and keep the clause number, so the agent can cite it.
- Code and configuration: split by function, class or block, never through the middle of one.
- Scanned files and PDFs: check the extracted text first. Chunking cannot repair columns read in the wrong order.
Spreadsheets and other tabular data often suit a different approach altogether, such as letting an agent query the data directly instead of embedding rows as text.
Change chunking like code, not like a setting
Changing the chunking rule means rebuilding the index, and it can quietly make some questions worse while making others better. Treat it as a release:
- Keep the chunking rules in version control, next to the code that runs them.
- Store the rule version with every chunk, so you can tell which rule produced it.
- Run the retrieval test set before and after, and compare question by question, not only the overall score.
- Build the new index alongside the old one and switch over only when the results hold.
Chunking also carries permissions. If a document has restricted sections, make sure each chunk keeps the access rules of the section it came from, so an agent cannot reach data a user is not allowed to see.
CodeDTX treats chunking as part of the AI integration work rather than a library default, because it decides what an agent can find. If your RAG agent keeps missing answers that are in your documents, talk to us.
Frequently asked questions
What chunk size should a RAG system start with?
There is no single right size. Start with chunks that hold one complete idea, such as a paragraph or a short section, rather than picking a number first. Then test a smaller and a larger setting against your retrieval test set. Questions that need exact facts tend to favour smaller chunks, while questions that need explanation favour larger ones, so the right size depends on what your users ask.
Should chunks overlap with each other?
A little overlap can preserve surrounding text across fixed-size or recursive chunk boundaries. It adds duplicate text to the index and can return near-identical results, so keep it modest. With structure-aware chunking, overlap may matter less because cuts can follow natural breaks such as headings and clauses. Test whether it improves retrieval on your own queries.
Do we need to re-chunk when we change embedding model?
Not always, but you will need to re-embed every chunk, so it is a good moment to review the chunking too. Some embedding models handle longer passages than others, which can change the size that retrieves well. Whatever you change, rebuild the index alongside the old one, run the retrieval tests on both, and switch only when the new index performs at least as well.
Is semantic chunking worth the extra cost?
Sometimes. It can help with long text that has no headings, such as meeting transcripts or chat logs. For documents with clear structure, such as policies, manuals and contracts, splitting by headings and sections is a useful baseline and easier to explain. One study of oil and gas documents found structure-aware chunking outperformed semantic chunking at lower compute cost in its test set. Compare both methods on your own documents and questions before adopting either across the system.



