Skip to content
Knowlayer

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

  1. 01

    Ingestion

    Parse each source format into clean structured text, preserving headings, tables, and code.

  2. 02

    Chunking

    Split on meaning, then add the parent context to each chunk so it stands alone.

  3. 03

    Embeddings

    Convert chunks and queries to vectors with the same model so similarity is comparable.

  4. 04

    Indexing

    Store vectors with metadata in a vector database that supports approximate nearest-neighbour search and metadata filters.

  5. 05

    Hybrid retrieval

    Combine dense vector search with keyword search (such as BM25) and merge the rankings, for example with reciprocal rank fusion.

  6. 06

    Reranking

    A cross-encoder rescores the top candidates against the question for higher precision.

  7. 07

    Context assembly

    Deduplicate, order, and fit the selected chunks into the token budget, keeping source identifiers for citation.

  8. 08

    Evaluation

    Score retrieval and answers on a test set before and after every change.

A retrieval-ready chunk record (JSON)

A retrieval-ready chunk record (JSON)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

StrategySplits onStrengthWatch out for
Fixed-sizeToken or character countSimple and fastCuts procedures and tables mid-thought
RecursiveParagraph, then sentence, then wordRespects natural breaksStill blind to document structure
Heading-anchoredDocument structure (headings)Chunks match how authors organized meaningNeeds well-structured source content
SemanticEmbedding similarity between sentencesAdapts to topic shiftsCostlier; 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