Back to blog
AI

Building a RAG system: from zero to production in 2 weeks

Apr 8, 2025 12 min readBy Onesnzeros team

How we built a production-grade Retrieval-Augmented Generation system for a client, step by step.

Our client, a legal services firm in Pune, had 15 years of contract templates, case notes and compliance documents locked inside PDFs and Word files. Associates spent 2–3 hours a day just searching for the right precedent. We built them a RAG (Retrieval-Augmented Generation) system in two weeks. Here's exactly how we did it.

What is RAG, and why does it matter?

Large language models like GPT-4 and Claude are trained on general knowledge up to a cutoff date. They know nothing about your internal documents, your products or the way your company works. RAG fixes this by pulling the relevant chunks of your own documents at query time and handing them to the LLM as context. You end up with an AI that can answer questions about your business specifically.

Our architecture

  • An ingestion pipeline turns PDF, DOCX and text files into chunked text
  • OpenAI's text-embedding-3-small model generates the vector embeddings
  • Supabase pgvector stores the vectors and runs similarity search
  • The retrieval layer pulls the top 5 most relevant chunks for each query
  • Claude Sonnet generates the answer from the retrieved context
  • The frontend is Next.js, with streaming responses so the answer appears as it's generated
  • Supabase Auth handles login, with row-level security on document access

Week 1: building the foundation

  1. 1Set up the ingestion pipeline for PDF, DOCX and scanned documents (OCR via Tesseract)
  2. 2Settle on a chunking strategy: 512 tokens with a 64-token overlap to keep context across boundaries
  3. 3Build the embedding pipeline and batch-process every document through OpenAI's embedding API
  4. 4Set up Supabase with the pgvector extension and write the vector similarity search functions
  5. 5Build the retrieval API: given a query, return the top-k most relevant document chunks
  6. 6Put together a basic chat interface and wire it up end to end with streaming

Week 2: production hardening

  1. 1Add hybrid search, combining vector similarity with keyword (BM25) search for better recall
  2. 2Add a re-ranking step that sorts retrieved chunks by actual relevance before they go to the LLM
  3. 3Show source citations, so every answer says which document it came from
  4. 4Enforce access control, so staff only search documents they're authorised to see
  5. 5Cache frequently asked questions to cut API costs
  6. 6Set up monitoring for query latency, retrieval quality and LLM token usage
  7. 7Load test with 50 concurrent users and tune the database indexes

The problems we ran into

  • Poor-quality scanned PDFs. We fixed this by cleaning up the images before OCR
  • Chunk size. Too small and you lose context; too large and you exceed context limits and hurt relevance
  • Hallucination when the retrieved context wasn't enough. Confidence thresholds and a plain 'I don't know' answer took care of it
  • Latency. Vector search plus LLM inference adds up, and streaming the response hid most of the wait
  • Embedding costs. Batch processing at ingestion is cheap, but re-embedding updated documents needs careful handling

"The firm's associates now find the right document in under 30 seconds on average, down from 2–3 hours. Across the team, that's roughly 500 hours of associate time saved every month."

Should you build or buy?

Off-the-shelf RAG products (Notion AI, SharePoint Copilot) work well for generic use cases. If you have specific access control rules or proprietary document formats, or the system has to plug into what you already run, a custom build gives you the control you need. Two weeks is a realistic timeline with the right team. The components are well understood, and the hard problems already have known solutions.

Ready to apply this to your business?

Book a free 30-minute call. There's no commitment, and you'll come away with a clear plan for how we can help.

Book a free consultation