BJ.
Back to blog
2 min read

Building a Retrieval-Augmented Generation Pipeline From Scratch

RAGLLMEmbeddings

Retrieval-Augmented Generation (RAG) is one of the fastest ways to get an LLM to answer questions about data it was never trained on — your internal docs, a knowledge base, a codebase — without fine-tuning. This is a placeholder post; replace it with your own write-up.

Why RAG

Large language models are frozen at training time and have no idea about your private data. RAG fixes this by retrieving relevant context at query time and feeding it into the model's prompt, so answers stay grounded in real, citable sources instead of the model's parametric memory.

The core pipeline

  1. Chunk your source documents into overlapping windows that preserve local context.
  2. Embed each chunk with a sentence embedding model.
  3. Index the embeddings in a vector store for fast approximate nearest neighbor search.
  4. Retrieve the top-k chunks most relevant to a user's query.
  5. Generate an answer by passing the retrieved chunks plus the query to an LLM, and cite the sources.
def answer_question(query: str, top_k: int = 5) -> str:
    query_embedding = embed(query)
    chunks = vector_store.search(query_embedding, top_k=top_k)
    context = "\n\n".join(chunk.text for chunk in chunks)
    prompt = build_prompt(query, context)
    return llm.generate(prompt)

Things that actually move the needle

  • Chunking strategy matters more than people expect — naive fixed-size chunks often split context in unhelpful places.
  • Hybrid retrieval (dense + keyword/BM25) tends to outperform pure vector search on queries with exact terms, like product names or error codes.
  • Re-ranking retrieved chunks with a cross-encoder before generation noticeably improves answer quality for a modest latency cost.
  • Evaluation is the part teams skip and later regret — track answer faithfulness and retrieval precision/recall, not just vibes.

What's next

In a follow-up post I'll cover evaluating RAG systems properly: faithfulness scoring, retrieval metrics, and how to build a regression test suite so a prompt or model change doesn't silently degrade answer quality.

Thoughts on this post?

If anything was unclear, wrong, or worth discussing further, I'd like to hear it.

Say hello