Retrieval-augmented generation
RAG Engineering
RAG answers are only as good as the chunks that reach the model. Most gains come from how content is split, described, retrieved, and measured, not from the model itself.
The pipeline, stage by stage
- 01
Ingestion
Parse each source format into clean structured text, preserving headings, tables, and code.
- 02
Chunking
Split on meaning, then add the parent context to each chunk so it stands alone.
- 03
Embeddings
Convert chunks and queries to vectors with the same model so similarity is comparable.
- 04
Indexing
Store vectors with metadata in a vector database that supports approximate nearest-neighbour search and metadata filters.
- 05
Hybrid retrieval
Combine dense vector search with keyword search (such as BM25) and merge the rankings, for example with reciprocal rank fusion.
- 06
Reranking
A cross-encoder rescores the top candidates against the question for higher precision.
- 07
Context assembly
Deduplicate, order, and fit the selected chunks into the token budget, keeping source identifiers for citation.
- 08
Evaluation
Score retrieval and answers on a test set before and after every change.
A retrieval-ready chunk record (JSON)
{
"chunk_id": "api-auth-001",
"context": "Orders Platform > API > Authentication",
"text": "Authenticate every request by passing your token in the Authorization header.",
"tokens": 268,
"metadata": {
"product": "Orders Platform",
"version": "4.2",
"topic_type": "reference",
"audience": "developer"
},
"source": "docs/api/authentication.md"
}Illustrative record. The context line and metadata fields are what make filtering, citation, and version-correct answers possible.
Chunking strategies compared
| Strategy | Splits on | Strength | Watch out for |
|---|---|---|---|
| Fixed-size | Token or character count | Simple and fast | Cuts procedures and tables mid-thought |
| Recursive | Paragraph, then sentence, then word | Respects natural breaks | Still blind to document structure |
| Heading-anchored | Document structure (headings) | Chunks match how authors organized meaning | Needs well-structured source content |
| Semantic | Embedding similarity between sentences | Adapts to topic shifts | Costlier; boundaries are harder to explain |
Context optimization
- Context enrichment
- Prefix each chunk with its title path (for example product > section > topic) so retrieval and the model know where it came from.
- Metadata filters
- Filter by product, version, and audience before similarity ranking, so the right topic for the wrong version is never returned.
- Token budgeting
- More context is not always better. Trim to the chunks that answer the question and keep them in a sensible order.
When it earns its cost
Reach for it when
- Answers must be grounded in your own, changing documentation
- Users need citations they can verify
- Content spans several products or versions
Hold off when
- The knowledge is small and stable enough to place directly in a prompt
- No one can maintain a test set to measure quality
- The source content is too unstructured to chunk meaningfully; fix that first