<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aman gupta</title>
    <description>The latest articles on DEV Community by Aman gupta (@aman_gupta_215e43a2a87141).</description>
    <link>https://dev.to/aman_gupta_215e43a2a87141</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2732730%2F8e13d17c-8341-4021-9753-ba668106964b.jpg</url>
      <title>DEV Community: Aman gupta</title>
      <link>https://dev.to/aman_gupta_215e43a2a87141</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aman_gupta_215e43a2a87141"/>
    <language>en</language>
    <item>
      <title>Practical Patterns for Adding a RAG Pipeline to an Existing Node.js Backend</title>
      <dc:creator>Aman gupta</dc:creator>
      <pubDate>Wed, 25 Nov 2026 13:38:38 +0000</pubDate>
      <link>https://dev.to/aman_gupta_215e43a2a87141/practical-patterns-for-adding-a-rag-pipeline-to-an-existing-nodejs-backend-1mbl</link>
      <guid>https://dev.to/aman_gupta_215e43a2a87141/practical-patterns-for-adding-a-rag-pipeline-to-an-existing-nodejs-backend-1mbl</guid>
      <description>&lt;p&gt;Most teams don't get to build a RAG (Retrieval-Augmented Generation) system from a blank slate. You already have a Node.js API, a Postgres database, auth, logging, and deploy pipelines - and now someone wants "an AI assistant that answers questions about our data." Here's what actually works when you bolt RAG onto an existing service, based on a few production integrations.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Don't let the vector store become a second source of truth
&lt;/h2&gt;

&lt;p&gt;The easiest mistake is treating the vector database (pgvector, Pinecone, Qdrant, whatever) as an independent system you sync "eventually." It drifts. Documents get edited, deleted, or permission-scoped in your primary DB and the embeddings don't know it.&lt;/p&gt;

&lt;p&gt;Instead, treat embedding generation as a side effect of your existing write path, not a separate pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;saveDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;saved&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upsert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;enqueueEmbeddingJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;saved&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// async, retryable&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;saved&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're on Postgres already, pgvector keeps this in one transaction boundary conceptually, even if the embedding job itself runs out-of-band in a queue (BullMQ, pg-boss - anything you already run). This matters more than picking the "best" vector DB.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Chunking is a product decision, not a hyperparameter
&lt;/h2&gt;

&lt;p&gt;Teams spend days tuning chunk size (500 tokens? 1000? overlap 10%?) when the real question is: what's the smallest unit of your data that answers a question on its own? For support tickets, that's a resolved thread. For a knowledge base, it's a heading section. For contracts, it's a clause.&lt;/p&gt;

&lt;p&gt;Generic recursive text splitting works as a fallback, but if your data has natural structure (Markdown headings, DB rows, ticket threads), chunk along that structure first and only fall back to token-based splitting for oversized chunks. This alone fixes more retrieval quality issues than switching embedding models.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Retrieval is a ranking problem, not a similarity-search problem
&lt;/h2&gt;

&lt;p&gt;Cosine similarity on embeddings gets you a candidate set, not an answer. In every production RAG system we've shipped, a reranking step (even a cheap cross-encoder, or just a second LLM call scoring relevance) made a bigger quality difference than swapping embedding models. A simple two-stage pipeline:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Vector search top-K (K=20-50, cheap, approximate)&lt;/li&gt;
&lt;li&gt;Rerank down to top-N (N=3-5, more expensive, precise)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If latency budget allows, this is the single highest-leverage change you can make to answer quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Keep the LLM call stateless and the context assembly explicit
&lt;/h2&gt;

&lt;p&gt;Resist building a framework. A RAG call in an existing Express/Nest service can be a plain function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;answerQuestion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;vectorSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;reranked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;rerank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reranked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\n---\n'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildPrompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No hidden agent loop, no implicit memory. When something looks wrong in production, you can log &lt;code&gt;candidates&lt;/code&gt;, &lt;code&gt;reranked&lt;/code&gt;, and &lt;code&gt;prompt&lt;/code&gt; and see exactly why the model said what it said. This debuggability is worth more than any abstraction a RAG framework gives you, especially in the first 6 months of running it live.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Scope retrieval by permissions at query time, not index time
&lt;/h2&gt;

&lt;p&gt;If your app has row-level security or per-tenant data, don't build a separate vector index per tenant unless scale forces it - you'll be maintaining N indexes forever. Instead, store the tenant/user/ACL fields as metadata on each embedded chunk and filter at query time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;document_chunks&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps your existing authorization model as the source of truth instead of duplicating it into the retrieval layer, which is where RAG security bugs actually come from in practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Evaluate with real questions before you ship, not after
&lt;/h2&gt;

&lt;p&gt;Write down 20-30 real questions your users will actually ask, with what a correct answer looks like, before you start tuning. Re-run this fixed set every time you change chunking, the embedding model, or the prompt. Without this, "it feels better" is the only signal you have, and that's how teams end up tuning against vibes for weeks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;None of this requires a new framework or rewriting your backend. RAG fits into an existing Node.js service as: a side-effect on write, a queue worker for embeddings, a two-stage retrieve-then-rerank function, and metadata-scoped filtering reusing your existing auth model. The failure mode to avoid is treating RAG as its own application instead of a feature of the one you already have.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I'm Aman, a full-stack engineer working on &lt;a href="https://stackvise.com" rel="noopener noreferrer"&gt;StackVise&lt;/a&gt;, where we build custom software and AI/LLM integrations for teams. Happy to compare notes on RAG implementations - drop a comment.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rag</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
