Back to blog posts

AI Engineering / RAG

RAG: Chunking & Embedding

Updated Jul 2026 10 min read Built from handwritten notes

Mental Model

Chunking splits your documents into pieces. Embedding converts those pieces into vectors so the RAG system can search by meaning instead of exact words.

Where chunking and embedding fit in RAG

Ingest Docs
Chunking
Embedding
Store in Vector DB
Retreive

Chunking

- Chunking splits a document into chunks. A good chunk keeps related information together, fits inside the model context window and is small enough for accurate retrieval.

- The hard part is that the splitter does not always know where human meaning begins and ends. If it cuts in the wrong place like cutting a policy statement or a code function or an API example, LLM can lose the context needed to answer correctly.

1. Fixed-size chunking

Fixed-size chunking splits documents after a fixed number of characters or tokens. For example, chunk size might be 500 characters, 10 words, or page-wise chunks.

Problem 1

It may cut sentences in the middle.

Problem 2

It may separate related topics.

Problem 3

It does not understand document structure.

Chunk size = 8 words

chunk 1: Leave Policy: Employee gets 10 paid leaves per
chunk 2: year. No carry forward. Laptop Reimbursement Policy: Up
chunk 3: to $1000 allowed. Only MAC allowed.

2. Overlap chunking

Overlap chunking is fixed-size chunking where chunks share some text. For example, chunk size = 8 words and overlap = 4 words. This increases the chance that related text falls in one chunk.

chunk size = 8 words, overlap = 4 words

chunk 1: Leave Policy: Employee gets 10 paid leaves per year. No carry forward.
chunk 2: year. No carry forward. Laptop Reimbursement Policy: Up to $1000 allowed. Only
chunk 3: to $1000 allowed. Only MAC allowed

Pros

Higher chance related text falls in one chunk.

Cons

Not guaranteed to solve all problems with fixed-size chunking. It stores duplicate text, which can increase embedding cost and database storage cost.

3. Sentence-based chunking

Sentence-based chunking keeps full sentences together.

Sentence 1
Sentence 2
Sentence 3
Sentence 4
Sentence 5
Sentence 1 + 2 + 3 → chunk 1
Sentence 4 + 5 → chunk 2

Pros

Better meaning because it does not cut sentences.

Cons

It may still not convey the context of a section or paragraph of text.

4. Paragraph-based chunking

Paragraph-based chunking splits by paragraphs.

Para 1 → chunk 1
Para 2 → chunk 2
Para 3 → chunk 3

Pros

Easy to understand and usually has better semantic context.

Cons

Some paragraphs are too long or too short. They may still miss section-level context.

5. Section/header-based chunking

- Section/header-based chunking splits documents using section headings.
- Use section-based chunking for documents with fixed stucture like markdown files, technical docs, API docs, HR policies, legal docs.

# Leave Policy -> chunk 1
# Travel Policy -> chunk 2
# Laptop Refresh Policy -> chunk 3

Pros

Preserves document structure and maintains semantic data in one chunk.

Cons

Long sections may still need sub-chunking. Some documents may not be organized clearly by sections.

6. Recursive chunking

Recursive chunking tries to split a document using the best natural separator first, then falls back to smaller separators.

1st: split by section headings
2nd: if still too large, split by paragraph
3rd: if still too large, split by sentence
4th: if still too large, split by tokens / characters

Pros

Best general-purpose strategy for many text documents.

Cons

More complex and needs fine-tuning.

7. Semantic chunking

- Semantic chunking splits a document based on meaning. It tries to keep related topics together.
- Works well for systems where high accuracy is needed in responses.

Docs
LLM creates embeddings
Group related data in 1 chunk using vector similarity

para 1: Leave Policy
para 2: Carry-forward Rules
para 3: Sick Leave Process
para 4: Laptop Reimbursement

chunk 1 = para 1 + 2 + 3, as all 3 are semantically similar
chunk 2 = para 4

Pros

Better semantic quality and improved retrieval accuracy.

Cons

More expensive and higher latency because documents go through an LLM to create embedding and then similarity search before chunks are created.

8. Custom / structure-aware chunking

- Chunk based on the document type or document structure.
- It is mostly used in production systems where higher accuracy is needed.

Resume: Chunk By
- Summary
- Skills
- Experience per company
- Education
API doc: Chunk By
- Endpoint
- Parameters
- Request example
- Response example
- Errors
Codebase: Chunk By
- File
- Module
- Class
- Function

Metadata

- Chunks should also store metadata.
- Metadata helps with citation, filtering, debugging, and source tracking.

{
  "id": "employee-policy-leave-001",
  "text": "Employees get 20 paid leaves per year",
  "embedding": [0.12, -0.44, 0.9],
  "metadata": {
    "source": "employee-handbook.pdf",
    "section": "HR - Leave Policy",
    "page": 12,
    "chunkIndex": 1
  }
}

Embedding

Embedding converts text to vectors.

Text
Embedding model
Vector [0.12, -0.44, 0.90, ...]
Similar meaning tokens → nearby vectors → high cosine similarity

Which model to choose?

  • - No single model is best. Choose one that fits your document types and use case.
  • - Test embedding models with your documents and retrieval results.
  • - The MTEB leaderboard on Hugging Face compares embedding models.
  • - Trade-offs to consider: latency, dimensions and quality. Dimensions such as 256, 768, or 1024 mean the text is converted into that many vector numbers.
  • - Always use the same model to embed chunks and the user query.
  • - If your document has legal jargon, choose a model trained for legal language. If your document has images, choose a model that can embed images and compare their vectors.

Parser

- For documents with code or complex structure, you need a parser before chunking. A parser understands the document shape so chunks are not created from broken pieces.
- For PDFs, you might use a PDF parser. For codebases, an AST parser creates an abstract syntax tree representation so the chunker can split by class, module, function, or component.

Codebase
AST parser
Create chunks by class / module / function / component
Function Declaration
    |-- name: sum
    |-- params:
    |     |-- a
    |     |-- b
    |-- body
    |     |-- Return Statement
    |     |       |-- Binary Expression
    |     |       |       | -- left: a
    |     |       |       | -- operator: +
    |     |       |       | -- right: a
            

Takeaways

  • - Chunk quality controls retrieval quality.
  • - Section, recursive, semantic and custom chunking usually beat naive fixed-size chunks.
  • - Metadata makes RAG answers easier to cite, filter, debug and trace.
  • - Use the same model to embed chunks and user queries.