Prompt
I'm building a RAG product and need advice on the infrastructure stack
Latest observation
Here’s a practical way to think about a RAG infrastructure stack: optimize for reliability, retrieval quality, latency, and observability first, then add complexity only where it pays off.
1) Reference architecture
A solid production RAG stack usually has these layers:
-
Ingestion
- Pull from docs, web pages, PDFs, Notion/Confluence, Slack, DBs, etc.
- Parse, normalize, clean, dedupe, and enrich metadata.
-
Indexing
- Chunking
- Embeddings generation
- Optional keyword / BM25 index
- Store vectors + metadata + document text
-
Retrieval
- Hybrid search (vector + lexical)
- Metadata filters
- Re-ranking
- Query rewriting / routing for complex queries
-
Generation
- Prompt assembly
- Context window management
- Citation handling
- Guardrails / safety checks
-
Serving
- API layer
- Caching
- Auth / tenant isolation
- Rate limits / quotas
-
Observability + Evaluation
- Traces for retrieval and generation
- Offline evals
- Feedback loop
- Cost/latency monitoring
2) Recommended stack by layer
Ingestion / ETL
Good options:
- Python for orchestration and parsers
- Airbyte or custom connectors for sources
- Prefect or Dagster for pipelines
- Celery / RQ / Sidekiq-like queues if you need async jobs
- Kafka / SQS / PubSub if ingest volume is high
If you’re early-stage, keep it simple:
- Python workers
- Queue (SQS/RabbitMQ/Redis queue)
- Scheduled jobs + webhooks
Parsing and chunking
- Unstructured is popular for PDFs/Office docs
- Apache Tika for broad file support
- Custom parsing for high-value sources
- Store both:
- raw text
- structured metadata
- chunk provenance
Chunking tips:
- Don’t only use fixed-size chunks
- Prefer structure-aware chunking:
- headings
- paragraphs
- tables
- semantic boundaries
- Preserve parent-child links so you can fetch larger context when needed
Embeddings
Choose based on:
- quality
- cost
- latency
- multilingual support
- domain specificity
Common approach:
- Start with a strong general model
- Switch to domain-tuned embeddings only if needed
You can use:
- Hosted embedding APIs
- Self-hosted open models if scale/cost requires it
Vector database / search
This is a key decision.
If you want simplicity:
- Postgres + pgvector
- Great for MVPs and moderate scale
- Easy operationally if you already use Postgres
- Supports metadata filtering well
- Not always best for very large scale or highest recall/latency needs
If you need managed vector search:
- Pinecone
- Weaviate Cloud
- Qdrant Cloud
- Milvus/Zilliz
- OpenSearch / Elasticsearch if you want hybrid search and are already in that ecosystem
If you need hybrid search and enterprise search:
- Elasticsearch/OpenSearch
- Strong keyword + filters + vector
- Good for enterprises with existing ES footprint
Practical recommendation:
- MVP / early product: Postgres + pgvector
- Growing product with search-heavy workload: Qdrant / Pinecone / Weaviate
- Enterprise search with lexical importance: OpenSearch / Elasticsearch
Re-ranking
Often hugely improves answer quality.
Options:
- Cross-encoder rerankers
- LLM-based reranking
- Lightweight rank fusion from vector + BM25
A strong pattern:
- retrieve top 50–200
- rerank to top 5–20
- feed those into the LLM
LLM serving
You can:
- Use a hosted model API for fastest iteration
- Self-host open models for cost/control
Consider:
- latency
- context length
- reasoning quality
- tool use
- structured output reliability
For production:
- Use a gateway/service layer to abstract model providers
- Support fallback models
- Log prompt/version/model metadata
API and app backend
Typical choices:
- FastAPI for Python shops
- Node.js / TypeScript if your team prefers it
- Go if you need high throughput and low latency
Add:
- auth
- tenant isolation
- request tracing
- streaming responses
- background job support
Caching
Useful caches:
- embedding cache for repeated queries/docs
- retrieval result cache
- prompt/response cache for deterministic or near-repeatable queries
- metadata cache
Common tools:
- Redis
- CDN edge caching for static knowledge assets
Storage
You’ll likely need:
- Object store for raw files and parsed artifacts
- S3 / GCS / Azure Blob
- Relational DB for metadata, users, permissions, jobs, audit logs
- Postgres is usually the default choice
3) Observability and evaluation
This is where many RAG products fail.
What to log
- query
- retrieved chunks + scores
- reranker output
- final prompt
- model output
- citations used
- latency per stage
- token usage
- user feedback
Tools
- OpenTelemetry for traces
- LangSmith, Helicone, Arize Phoenix, WhyLabs, or similar for LLM observability
- Prometheus + Grafana for infra metrics
- Sentry for app errors
Evaluation
Track:
- retrieval precision/recall
- groundedness
- citation accuracy
- answer correctness
- refusal quality
- latency and cost
You need both:
- offline eval sets
- production feedback loops
4) Security and tenancy
If you’re B2B, this matters early.
Minimum requirements
- document-level ACL enforcement
- tenant isolation
- encryption at rest and in transit
- secrets management
- audit logs
Nice to have
- row-level security in Postgres
- per-tenant indexes or namespace partitioning in vector DB
- PII detection/redaction
- data retention policies
- customer-controlled key management for enterprise
5) Suggested stacks by stage
MVP stack
Best if you’re validating the product:
- API/backend: FastAPI
- Job orchestration: Celery or simple worker queue
- DB: Postgres
- Vector: pgvector
- Object store: S3
- LLM/embeddings: hosted APIs
- Observability: OpenTelemetry + basic logs + Sentry
- Deploy: Docker + managed cloud service
Why:
- easiest to ship
- low ops burden
- enough for many early products
Growth stack
When load and quality needs increase:
- API/backend: FastAPI/Go
- Pipelines: Dagster/Prefect
- DB: Postgres
- Vector/search: Qdrant/Pinecone/Weaviate or OpenSearch
- Reranker: dedicated reranking service
- Cache: Redis
- Observability: Phoenix/Helicone + Prometheus/Grafana
- Infra: Kubernetes or managed containers
Enterprise stack
If you need compliance, scale, hybrid search, and advanced ACLs:
- Search: OpenSearch/Elasticsearch + vector
- Metadata/auth: Postgres with RLS
- Pipelines: Dagster + queue
- LLM gateway: model abstraction, fallback, policy enforcement
- Observability: full tracing + eval platform
- Security: KMS, audit logs, DLP, tenant isolation
- Deployment: Kubernetes, private networking, VPC endpoints
6) Architectural patterns that matter a lot
Hybrid retrieval
Use both:
- semantic similarity
- lexical/BM25
This helps with:
- proper nouns
- exact terms
- code snippets
- rare identifiers
Metadata-aware retrieval
Filter by:
- tenant
- source
- doc type
- recency
- access permissions
Multi-stage retrieval
Best practice:
- broad candidate retrieval
- reranking
- context compression if needed
Context budgeting
Don’t blindly stuff top-k into the prompt. Use:
- deduplication
- diversity constraints
- parent-child expansion
- summarization/compression only when needed
Freshness strategy
If docs change often:
- incremental indexing
- delete/update support
- TTL or periodic reindex for unstable sources
- event-driven reindexing where possible
7) Common mistakes
- relying only on embeddings and no lexical search
- no reranking
- no ACL enforcement at retrieval time
- poor chunking
- no eval dataset
- no tracing for retrieval decisions
- mixing tenant data in shared indexes without strong controls
- making the first architecture too complex
8) My practical recommendation
If you’re early stage and want a robust default:
- FastAPI
- Postgres + pgvector
- S3
- Redis
- Hosted embeddings + hosted LLM
- Hybrid retrieval if possible
- Reranker
- OpenTelemetry + basic LLM logging
- Docker + managed cloud deployment
That gets you moving fast with a good path to scale.
9) How I’d choose if I were you
- If you’re pre-PMF: prioritize speed of development over perfect infra.
- If you’re B2B with enterprise docs: prioritize ACLs, hybrid search, and observability.
- If you’re high-scale consumer search: prioritize latency, caching, and search infra.
- If you’re cost-sensitive: evaluate self-hosted embeddings/LLMs later, not immediately.
If you want, I can also give you:
- a concrete AWS/GCP architecture,
- a startup vs enterprise stack comparison, or
- a recommended stack based on your expected scale and team size.
Brands