Building a RAG App
After this lesson, you will be able to:
- Architect a production RAG app end-to-end — ingestion, embedding, hybrid retrieval, reranking, generation, citations, evaluation, monitoring
- Map each stage of the app onto a real service (vector database, reranker, generation API, evaluation tool) and run the real-stack version in Colab
- Apply production patterns: streaming responses, query caching, fallback handling, cost tracking, A/B testing different retrievers
- Identify the most common production failure modes and the production hardening that prevents them
Before you start
Open the /play/rag-pipeline playground to walk a query end-to-end: preprocessing → hybrid retrieval → rerank → prompt assembly → streamed generation with citations. Flip the "cache hit" toggle to see how prompt caching lowers the cost of repeated context.
#The Production RAG Architecture
USER QUERY
│
▼
┌────────────────────────────────────┐
│ 1. Query Preprocessing │
│ - Normalization │
│ - PII redaction │
│ - Query rewriting (optional) │
└────────────────────────────────────┘
│
▼
┌────────────────────────────────────┐
│ 2. Hybrid Retrieval │
│ - BM25 search (sparse) │
│ - Dense vector search │
│ - RRF fusion │
│ - Top-50 candidates │
└────────────────────────────────────┘
│
▼
┌────────────────────────────────────┐
│ 3. Reranker │
│ - Cohere Rerank or BGE │
│ - Top-50 → top-10 │
└────────────────────────────────────┘
│
▼
┌────────────────────────────────────┐
│ 4. LLM Generation │
│ - System prompt with rules │
│ - Top-10 chunks as context │
│ - Streaming response │
│ - Inline citations │
└────────────────────────────────────┘
│
▼
┌────────────────────────────────────┐
│ 5. Post-processing │
│ - Citation validation │
│ - Refusal detection │
│ - Cache write │
└────────────────────────────────────┘
│
▼
USER STREAMING RESPONSE WITH FOOTNOTES
│
└──► Logged to eval pipeline (RAGAS)
#Step-by-Step Implementation
The cell below is a complete RAG app in about 100 lines of plain Python with no libraries, so it runs on this page. It has the same stages as the diagram. It retrieves two ways (BM25 keyword scores and a tiny stand-in for embeddings), fuses the two lists with reciprocal rank fusion, builds a prompt with numbered passages, gets an answer from a stand-in for the language model, and then checks every citation against its passage. The stand-in model only quotes the top passages, so the answer is only as good as the retrieval. That is the point: change a document or the query and watch the ranking change.
Tests: Run the cell and check that the retrieved ids include 2, that every [n] in the answer is a retrieved id, and that check_citations prints none. Then change one document so it no longer supports its claim and check that check_citations reports a problem.
The real stack swaps each stand-in for a service: a sentence-transformers model for the embeddings, a vector database for the index, a reranker, and a streaming model API behind a FastAPI endpoint. Those libraries cannot run on this page, so the version below is read-only. Copy it to Google Colab (run the pip line first) or to your own machine.
# Copy to Colab: needs fastapi, chromadb, sentence-transformers and rank-bm25, so it cannot run on this page.
# Cited-answer search, real stack with a mock reranker and a mock model (about 150 lines).
# !pip install fastapi uvicorn chromadb sentence-transformers rank-bm25
import os
import time
import asyncio
from typing import AsyncGenerator
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
# --- Mock components (replace with real APIs) ---
import chromadb
from sentence_transformers import SentenceTransformer
from rank_bm25 import BM25Okapi
# Step 1: Build the corpus + index (run once at startup)
embedder = SentenceTransformer('all-MiniLM-L6-v2')
client = chromadb.Client()
collection = client.get_or_create_collection("docs")
corpus = [
{"id": "1", "title": "RAG Overview", "text": "RAG combines retrieval with LLM generation for grounded answers."},
{"id": "2", "title": "Hybrid Retrieval", "text": "Hybrid retrieval combines BM25 with dense embeddings via RRF."},
{"id": "3", "title": "Reranking", "text": "A cross-encoder reranker rescores the short list of candidates for the top few results."},
{"id": "4", "title": "Long Context", "text": "Long-context models can read whole documents; RAG sends only the passages a question needs."},
# ... in production: thousands or millions of docs
]
doc_texts = [d["text"] for d in corpus]
doc_embs = embedder.encode(doc_texts, normalize_embeddings=True)
# Add to vector DB
for i, doc in enumerate(corpus):
collection.add(
ids=[doc["id"]],
embeddings=[doc_embs[i].tolist()],
documents=[doc["text"]],
metadatas=[{"title": doc["title"]}],
)
# BM25 index for sparse retrieval
bm25_corpus = [doc["text"].lower().split() for doc in corpus]
bm25 = BM25Okapi(bm25_corpus)
# --- Step 2: Hybrid retrieval with RRF ---
def hybrid_retrieve(query: str, k: int = 50) -> list:
q_emb = embedder.encode(query, normalize_embeddings=True).tolist()
dense_results = collection.query(query_embeddings=[q_emb], n_results=k)
dense_ids = dense_results["ids"][0]
bm25_scores = bm25.get_scores(query.lower().split())
bm25_top = sorted(range(len(corpus)), key=lambda i: -bm25_scores[i])[:k]
bm25_ids = [corpus[i]["id"] for i in bm25_top]
# RRF fusion (k=60)
scores = {}
for r in [dense_ids, bm25_ids]:
for rank, doc_id in enumerate(r):
scores[doc_id] = scores.get(doc_id, 0) + 1.0 / (60 + rank + 1)
fused = sorted(scores, key=lambda x: -scores[x])
return [d for d in corpus if d["id"] in fused[:k]]
# --- Step 3: Mock reranker (use real Cohere Rerank in prod) ---
def mock_rerank(query: str, candidates: list, top_n: int = 10) -> list:
# Replace with: cohere.Client().rerank(query=query, documents=[c["text"] for c in candidates], top_n=top_n)
# Here we just take the first top_n for demo
return candidates[:top_n]
# --- Step 4: LLM with citation prompt ---
SYSTEM_PROMPT = """You are a helpful research assistant.
Answer the user's question using ONLY the provided context.
For every claim in your answer, cite the relevant source(s) using [1], [2], etc.
If the answer is not in the context, say "I don't have enough information to answer."
Do not invent information."""
def build_user_prompt(query: str, chunks: list) -> str:
context = "\n\n".join([f"[{i+1}] {c['title']}: {c['text']}" for i, c in enumerate(chunks)])
return f"Context:\n{context}\n\nQuestion: {query}\n\nAnswer with citations:"
# Mock streaming LLM (replace with OpenAI/Anthropic streaming API)
async def stream_llm_response(query: str, chunks: list) -> AsyncGenerator[str, None]:
# Real implementation:
# async for chunk in openai.AsyncOpenAI().chat.completions.create(
# model="YOUR_MODEL_NAME", # use your provider's current model name
# messages=[{"role": "system", "content": SYSTEM_PROMPT},
# {"role": "user", "content": build_user_prompt(query, chunks)}],
# stream=True,
# ):
# yield chunk.choices[0].delta.content or ""
answer = f"Based on the retrieved context, {chunks[0]['text']} [1]\n\nAdditionally, {chunks[1]['text']} [2]" if len(chunks) >= 2 else "Insufficient context."
for word in answer.split():
yield word + " "
await asyncio.sleep(0.02)
# --- Step 5: FastAPI endpoint ---
app = FastAPI()
class Query(BaseModel):
q: str
@app.post("/search")
async def search(body: Query) -> StreamingResponse:
candidates = hybrid_retrieve(body.q, k=50)
reranked = mock_rerank(body.q, candidates, top_n=10)
return StreamingResponse(stream_llm_response(body.q, reranked), media_type="text/event-stream")
# --- Run with: uvicorn app:app --reload, then ---
# curl -X POST http://localhost:8000/search -d '{"q": "How does hybrid retrieval work?"}'
print("Architecture built. In production, replace mocks with:")
print(" - A hosted vector database")
print(" - Cohere Rerank or BGE-Reranker-v2")
print(" - A streaming language model API (LLM)")
print(" - Redis or Upstash for query cache")
print(" - Langfuse for observability + RAGAS evaluation")
print(" - Sentry for error tracking")
print("\nCHALLENGE: Add semantic query caching: compare the embedding of incoming queries")
print("and return cached results when similarity > 0.95. Saves LLM calls on repeated questions.")A quick check before the production hardening table — make sure you can spot the right place to spend optimization effort.
Your end-to-end cited-answer app has 3 second p95 latency. Breakdown: embed 30ms, vector search 25ms, rerank 120ms, LLM TTFT 800ms, LLM full response 2000ms. Where is the highest-leverage latency improvement?
#Production Hardening Checklist
| Concern | Solution |
|---|---|
| Streaming latency | Use SSE (Server-Sent Events); first-token < 1s |
| Cost overruns | Per-user rate limits, semantic caching, model tiering (mini for cheap queries, premium for hard ones) |
| Hallucination | Citation requirement in system prompt; post-process to verify cited claims actually appear in retrieved chunks |
| Refusals | Detect "I don't have information" responses; route to fallback (web search, escalate to human) |
| Stale corpus | Incremental re-embedding pipeline; track doc updated_at; re-embed on change |
| PII leakage | Redact PII before indexing; check responses for accidental PII regurgitation |
| Prompt injection | Sanitize retrieved chunks; treat them as untrusted; never let chunks override system prompt |
| Cost monitoring | Track tokens/query and $/query in real-time; alert on anomalies |
| A/B testing | Run two retrievers in parallel on a small share of traffic; compare RAGAS metrics |
| Eval CI | RAGAS golden-set runs on every PR; fail the build when a metric drops below your threshold |
#The Cost Math: Unit Economics of a RAG App
Before you scale, model your unit economics. The playground below computes per-query cost across assumed component prices (embed, retrieval, rerank, LLM input, LLM output; edit them to your provider's current prices), shows the cache-hit-rate lever, and finds the volume where self-hosted infra beats hosted APIs.
#Common Production Failure Modes
#Hands-On Walkthrough: Building a Perplexity-Clone
The CodePlayground above gives the bones. To productionize:
-
Replace mocks with real services:
- Vector DB: Pinecone / Weaviate / Qdrant
- Reranker: Cohere Rerank API or BGE-Reranker-v2 self-hosted
- LLM: a generation model with streaming and, if offered, a native citations feature
- Observability: Langfuse / Arize Phoenix / LangSmith
-
Add safety layers:
- Input sanitization
- Output PII redaction
- Refusal detection
- Citation validation (do the cited chunk ids actually contain the claim?)
-
Add streaming: replace mock
stream_llm_responsewith real OpenAI / Anthropic streaming SSE. -
Add caching:
- Exact-match cache (hash query)
- Semantic cache (embedding similarity)
- Per-user invalidation on subscription changes
-
Add evaluation:
- RAGAS pipeline runs on every deploy with a golden set
- Online metrics: thumbs-up/down, regeneration rate, time-on-page
- Track which retrievers / rerankers / LLMs win in A/B tests
-
Add monitoring:
- Cost per query (LLM + reranker + retrieval)
- Latency breakdown (retrieval / rerank / LLM gen)
- Error rates and refusal rates
- User satisfaction signals
#Key Takeaways
- Production RAG is integration, not invention. The components are well-understood; the hard part is wiring them together with caching, streaming, monitoring, and eval
- Stream first-token in < 1s. Users expect immediate feedback; spinners > 5s feel broken
- Citations are required for trust. Every claim must trace to a retrieved chunk; bake this into your system prompt
- A/B test retrievers and rerankers. Production data will reveal which configurations win on YOUR queries; don't trust generic benchmarks
- Eval pipeline is non-negotiable. RAGAS on a golden set in CI; alert on regressions
#Quick Check
Which production hardening provides the largest cost savings for a high-traffic RAG app?