IamBishal

AI Engineering · Tutorial Series

Build a secure RAG API

Ship a RAG API where every answer respects who is asking with evals that catch regressions.

By Bishal Saha · Updated

  • 3 Modules

    5 lessons live

  • Beginner

    Level

  • 3+ hours

    Learning Content

Start Learning

About This Tutorial Series

Most RAG tutorials stop once the model gives a good answer. This series starts there and asks a harder question: does every answer respect who is asking?

You build a support-assistant API for a multi-tenant SaaS product. Two synthetic customers, Acme and Globex, upload their own runbooks and help articles. Their staff ask questions and get answers with citations. The catch is that both tenants' documents live in the same PostgreSQL table, so one wrong query can hand Acme's runbook to a Globex user.

What you build, lesson by lesson

  1. Foundations. Map the ingest, retrieve and generate path, and name where it leaks. Store chunks and embeddings in PostgreSQL 17 with pgvector and an HNSW index. Then add a /answer endpoint that refuses below a similarity threshold and checks every citation. At the end of this module the API works, and it deliberately leaks across tenants.
  2. Security. Close the leak. A verified JWT becomes the caller's tenant and roles. Transaction-local settings carry them into PostgreSQL, where row-level security and document ACLs filter the vector query itself, not the results afterwards. Retrieved text is escaped and treated as untrusted data.
  3. Quality. Turn the rules into a build gate: a versioned golden dataset, recall@k and groundedness scores, hard cross-tenant probes and a CI workflow that fails when a change leaks data or lowers quality.

How it works in practice

Every lesson has a runnable folder in the companion repo, with tests you run against a throwaway PostgreSQL container. A deterministic fake embedder and a fake LLM stand in for paid APIs, so the whole series runs offline at no cost, and the provider interfaces show where a real model plugs in. Every command and output in the lessons comes from that code.

Who it is for

Backend engineers who know Node.js and REST, and who need to put retrieval in front of data that belongs to different customers. You don't need any RAG or vector database background.

  • Isolation in the database

    Row-level security and ACLs filter the vector query itself.

  • Evals that gate CI

    Golden dataset, recall@k and cross-tenant probes.

  • Runnable code per lesson

    Tested Node.js 24 folders in the companion repo.

  • No paid APIs

    A fake embedder and fake LLM keep it offline and free.

What You'll Learn

  • Explain the ingest, retrieve and generate path of a RAG API and the points where a multi-tenant assistant can leak data or mislead users.
  • Chunk tenant documents and store embeddings in PostgreSQL 17 with pgvector and an HNSW index, then return the top-k similar chunks.
  • Build an /answer endpoint with a bounded prompt, citations, and a refusal when retrieval finds nothing relevant.
  • Carry the caller identity from a verified JWT into PostgreSQL and enforce tenant isolation with row-level security inside the vector query.
  • Treat retrieved text as untrusted input and test each isolation layer separately, including pooled connections.
  • Build a golden dataset and eval runner for recall@k, groundedness and cross-tenant probes, and gate CI on it.

Prerequisites

  • Node.js and REST

    You can build and call a small HTTP API in Node.js. The code uses Node.js 24 with plain JavaScript modules.

  • Basic SQL

    You can read SELECT, INSERT and CREATE TABLE. Row-level security and pgvector are explained as they come up.

  • Docker

    Each lesson runs PostgreSQL 17 with pgvector in a throwaway container, so you need Docker installed.

  • No RAG background needed

    Embeddings, vector search and evals are introduced from scratch. No paid API key is required.

Course Curriculum

Reviews (0)

No reviews yet

Finish a lesson and be the first to share what you thought.

View All Tutorials

Bishal SahaLet's build together

Have a system to design, or an AI feature to get right?

Tell me about the problem. You'll get a clear, honest view of the best way forward.

  • Reply within 24 hoursOn business days, with my honest view and a next step.
  • Accepting select engagementsArchitecture, AI features and full builds.
  • Remote, worldwideWorking with startups, SMEs and enterprises.