A technique that splits long documents into small pieces (chunks). Performed before generating embeddings in RAG (retrieval-augmented generation) systems, and chunk size and overlap settings have a decisive effect on retrieval quality.
Chunking is a preprocessing technique that splits long text documents into smaller units called "chunks." LLMs (large language models) and embedding models have limits on the number of tokens they can process, and embedding a long document as a whole dilutes its meaning, so splitting it into appropriately sized pieces is essential. The search quality of a RAG system depends heavily on the chunking strategy.
The need for chunking stems from the context window limits of LLMs and the characteristics of embeddings. For example, OpenAI's text-embedding-3-small model can only process up to 8191 tokens, and compressing text that is too long into a single embedding causes loss of detailed information. Splitting text into appropriately sized chunks lets each chunk focus on a specific topic, which improves retrieval accuracy.
There are several chunking methods. The most basic is fixed-size splitting, which cuts text at a set number of characters or tokens. However, this can cut in the middle of a sentence and break the meaning. To address this, structure-based chunking is used, such as splitting by sentence, paragraph, or Markdown header. Recently, semantic chunking (splitting by units of meaning) has also been attracting attention, and it is also important to set an overlap between chunks to preserve the continuity of context.
In practice, the chunking strategy varies depending on the characteristics of the data. It is effective to split technical documents and legal documents by section/clause, conversation logs by utterance, and code by function/class. Generally, a chunk size of 500-1500 tokens and an overlap of about 10-20% are recommended, but you need to find the optimal values by measuring actual retrieval performance. Frameworks such as LangChain and LlamaIndex provide tools that make it easy to apply various chunking strategies.
๐ป Code Example
This is Python code that implements various chunking methods.
Team Lead"The RAG search quality isn't good. Irrelevant content is often retrieved. How can we improve it?"
Developer"The current chunk size is 2000 characters, and I think that's too large. Multiple topics are mixed in a single chunk, so the embeddings are being diluted. I think it would be good to reduce it to 500-800 characters and add about 20% overlap."
Team Lead"Won't the context be cut off if the chunks get smaller?"
Developer"Overlap plays that role. Also, if you fetch the top 3-5 chunks during retrieval and combine them, you can provide sufficient context. Alternatively, you can use the Parent Document Retriever pattern, which searches with small chunks and returns the larger original document."
Interviewer"Please explain why chunking is important in RAG."
Candidate"There are three reasons. First, embedding models have token limits. Second, compressing text that is too long into a single vector causes loss of detailed information. Third, you need appropriately sized chunks to accurately retrieve only highly relevant information during retrieval."
Interviewer"How do you decide the chunk size?"
Candidate"It depends on the characteristics of the data and the use case. Generally 500-1500 tokens is recommended, but in practice you should build an evaluation set and measure retrieval performance with various chunk sizes. Also, if the document has a structure, splitting by section/header is effective."
Senior"You're using CharacterTextSplitter with only separator='\n'. That's a problem."
Junior"What's the problem? It seems to split well on line breaks."
Senior"If there's a long paragraph without line breaks, it just passes it through even if it exceeds chunk_size. Using RecursiveCharacterTextSplitter is safer because it tries multiple separators in sequence. Also, the overlap is 0, so the context is cut off at chunk boundaries."
Junior"I'll switch to RecursiveCharacterTextSplitter and set chunk_overlap to 10-20% of chunk_size. Thank you!"
โ ๏ธ Cautions
โ๏ธ
Context Fragmentation Problem
Cutting blindly at a fixed size can break text in the middle of a sentence or separate important context. Use RecursiveCharacterTextSplitter to respect sentence/paragraph boundaries, and set an appropriate overlap (10-20%). Consider semantic chunking as well.
๐
Chunk Size Trade-offs
If chunks are too small, context is lacking; if too large, meaning is diluted and token limit problems arise. There is no single right answer, and you need to find the optimal value by evaluating actual retrieval performance. Generally, 500-1500 tokens is a good starting point.
๐ค
Tokens vs. Characters
Character-count-based chunking differs greatly from token counts for Korean. One Korean character is usually 1-3 tokens. To accurately respect an embedding model's token limit, calculate the actual token count with tiktoken or similar, or use TokenTextSplitter.