Technical Deep DiveProduction AI

Building Production RAG Systems: Architecture and Best Practices

A notebook demo that answers three questions is not a RAG system. Architecture, retrieval quality, and evaluation that survive real users.

Syed Sartaj

Founder & AI Engineer

·12 min read
About the author

Someone wires a vector database to a chat UI over a weekend. It answers the three questions in the demo deck. Monday morning, a real user asks about last quarter’s policy change, and the system cites a document from 2022 with total confidence.

That gap is where most RAG projects live. Retrieval Augmented Generation is a solid pattern for grounding models in your knowledge. Production means architecture, retrieval quality, and evaluation, not just “we connected an LLM to our docs.”

Understanding RAG architecture

At its core, a RAG system retrieves relevant context from a knowledge base and puts that context into the prompt so the model can answer with your material, not only its training data.

When I work with teams, the architecture conversation starts with documents and access, not with model brand names.

Key components

1. Document processing pipeline

Chunking decides what retrieval can even see. Poor chunking produces irrelevant hits and confident nonsense.

  • Semantic chunking: Segment on real boundaries (sections, topics), not only fixed character counts
  • Overlap: Keep modest overlap so ideas that straddle boundaries are not lost
  • Metadata enrichment: Source, date, author, product, sensitivity. You will need these for filters and citations later

2. Vector database selection

Pick for ops reality, not for a blog ranking:

  • Pinecone: Managed path when you want less infrastructure work early
  • Weaviate: Strong filtering story, open-source option
  • Qdrant: Solid when throughput and control matter
  • PostgreSQL + pgvector: Best when vector search must live next to your existing relational data and transactions

The “right” database is the one your team can operate and that supports the filters your permissions model needs. See permissions-aware retrieval.

3. Embedding strategy

Your embedding model shapes semantic similarity. General-purpose models are fine to start. Domain fine-tuning only earns its keep when your corpus language is genuinely specialised and you can measure the lift on a golden set.

Production considerations

Retrieval quality

The retrieval step is often the real bottleneck. Poor retrieval means even a strong model cannot save you.

Hybrid search

Combine dense vector search with sparse keyword search (BM25). Product names, error codes, and policy IDs often need exact matching that embeddings alone miss:

# Pseudo-code for hybrid search
dense_results = vector_db.search(query_embedding, top_k=20)
sparse_results = bm25_index.search(query, top_k=20)
final_results = rerank(dense_results + sparse_results, top_k=5)

Query transformation

Do not always retrieve with the raw user query:

  • HyDE: Generate a hypothetical answer, embed that, retrieve against it
  • Multi-query: Expand into a few phrasings and merge results
  • Query decomposition: Split compound questions into sub-questions

Use these when your golden set shows retrieval gaps. Do not add them for fashion.

Prompt engineering

The prompt template is a control surface. Be explicit about grounding:

You are an assistant with access to relevant documentation.

Guidelines:
- Only answer based on the provided context
- If the context does not contain relevant information, say so clearly
- Cite specific sections when making claims
- If sources conflict, acknowledge the conflict

Context:
{retrieved_context}

Question: {user_question}

Answer:

Evaluation framework

Production RAG without evaluation is storytelling. Separate layers and measure them. For a fuller treatment, see RAG evaluation and evaluation suites.

Key metrics:

  1. Retrieval: Precision@K, recall of required passages, MRR
  2. Generation: Faithfulness, relevance, groundedness
  3. System: Latency (P50/P95), cost per query, cache hit rate

Scaling considerations

Caching strategy

Multi-level caching reduces cost and latency without pretending models are free:

  1. Exact match cache for identical queries
  2. Semantic cache for near-duplicates when freshness allows
  3. Embedding / context cache for hot documents

Async processing

Not every workload needs interactive latency. Batch enrichment and report generation through queues:

async def process_batch(queries):
    embeddings = await embed_batch(queries)
    results = await vector_db.batch_search(embeddings)
    responses = await llm.batch_generate(results)
    return responses

Common pitfalls

1. Stuffing the context window

Five highly relevant chunks beat twenty mediocre ones. Quality over quantity.

2. Ignoring document freshness

Incremental indexing keeps the knowledge base current without full reindexes every night.

3. No fallback

When retrieval is weak, expand search, broaden filters carefully, or refuse clearly. Silent guessing destroys trust.

A practical production shape

A shape I return to often:

User Query
    ↓
Query rewriting (when needed)
    ↓
Hybrid search (vector + BM25)
    ↓
Reranking
    ↓
Context selection and assembly
    ↓
LLM generation (streaming if the UX needs it)
    ↓
Validation / citation checks
    ↓
Response

Permissions belong in retrieval, not as an afterthought on the final string. Evaluation gates belong in CI before you widen the audience. Cost controls belong next to latency dashboards. See LLM cost control.

Closing

Building production RAG is more than connecting a vector database to a model. Process documents carefully, measure retrieval separately from generation, and refuse to scale on anecdotes.

If you are designing knowledge AI over real company sources, start with Private Knowledge AI, or discuss your use case.

Written by Syed Sartaj

Founder of Neurocell. Builds production AI for growth-stage and mid-market teams: agents, knowledge systems, and product features that ship and stay reliable.

Book a 2-week architecture sprint

Keep going

Continue reading

All notes