Retrieval-Augmented Generation · worked example

Anatomy of a RAG

A question-answering system built over the SmartDelivery infra docs and microservice code — pulled apart stage by stage, with real results from two retrievers and what would change before it went to production.

268
files indexed
637
chunks
1,536
dims / vector
2
retrievers compared
~$0.003
to embed all

How this was run

268 files were chunked and embedded once with OpenAI. Then the same question set went through two different retrievers, each feeding its chunks to the same gpt-4o-mini prompt — so any difference in the answers is down to retrieval, not the model.

stack · text-embedding-3-small (embeddings) · gpt-4o-mini (generation) · Azure AI Search (hybrid)

Retriever A · Local

Plain cosine similarity over the 637 vectors held in a local file. The simplest retriever that works — nothing but nearest-neighbour math.

Retriever B · Azure

Deployed on Azure AI Search (free tier): hybrid retrieval that fuses BM25 keyword scoring with vector similarity. The production-style backend.

What RAG actually is

RAG is two ordinary steps bolted together. Retrieval: documents are cut into chunks, each chunk is turned into a vector (an embedding) that captures its meaning, and your question — also a vector — is matched against them by nearest-neighbour search. No model "understanding" yet; it is similarity arithmetic. Generation: the closest chunks are pasted into the prompt, and the model answers using only that context, so the answer is grounded in your material instead of the model's training data.

The pipeline

Index time — run once
Docs + code.md · .cs · .ts · .yml
→
Chunkstructure-aware
→
Embedtext-embedding-3-small
→
Vector store637 × 1536 floats
Query time — per question
Question
→
Embed
→
Retrieve top-kcosine or hybrid
→
Prompt + LLMgpt-4o-mini
→
Answer

Chunking, hands-on

The retriever can only ever hand the model a whole chunk, so a chunk that splits a heading from its body degrades every answer that needs it. Try the three strategies on a real slice of TECH_SPEC.md and watch the boundaries move.

Two retrievers, one question set

The interesting engineering is retrieval, so we ran the same questions through two backends and fed each one's chunks to the same gpt-4o-mini prompt. Any difference in the answer comes from what was retrieved, not the model.

LOCAL — plain cosine similarity over vectors in a file.  ·  AZURE — Azure AI Search hybrid retrieval: BM25 keyword scoring fused with vector similarity. These are the real, unedited answers from the run.

Two honest caveats. The scores aren't comparable — cosine runs 0–1, Azure's fused RRF score runs ~0.02–0.03 — so judge by which chunks came back, not the number. And "more complete" is not automatically "more correct": broader retrieval can also pull in loosely-related context, so a production system verifies, it doesn't just trust.

This demo vs. production

ConcernThis demoIn production
Vector storeOne file, brute-force cosine over 637 rowsApproximate-NN index (pgvector, Pinecone, Azure AI Search) over millions
RetrievalCosine, or Azure free-tier hybridHybrid plus a cross-encoder semantic reranker (Basic tier+)
ChunkingRecursive character splitter per file typeLayout-aware for PDFs, AST-based for code, parent-document retrieval
Query handlingQuestion embedded verbatimQuery rewriting, multi-query, HyDE, conversation-aware rephrasing
FreshnessManual re-indexIncremental pipeline keyed on content hashes, triggered by commits
EvaluationReading the answersRecall@k / MRR on a golden set; faithfulness scoring in CI
Access controlNone — localPer-document ACLs enforced at retrieval time
ServingStatic page, no keys (this site)Backend proxy, per-IP rate limits, budget caps, abuse monitoring

The design space

Chunking

  • Fixed — slice every N chars. Splits anything.
  • Overlap — shared tail across chunks.
  • Structure-aware — split on headings/declarations (used here).
  • Semantic — break where meaning shifts.
  • Parent-document — embed small, return the larger block.

Retrieval

  • Keyword / BM25 — exact terms. Strong on names, IDs.
  • Dense vectors — semantic similarity. Strong on paraphrase.
  • Hybrid — fuse both (the gap we measured).
  • Reranking — a cross-encoder reorders the shortlist.

Beyond basic

  • Query rewriting — fix vocabulary mismatch.
  • HyDE — embed a hypothetical answer.
  • GraphRAG — retrieve over an entity graph.
  • Agentic RAG — the model decides what to retrieve.

A static snapshot — no live API keys, nothing to abuse. Corpus: the SmartDelivery infra + app repositories. Embeddings: text-embedding-3-small. Generation: gpt-4o-mini. Hybrid retrieval: Azure AI Search (free tier).