DEV Community

Cover image for Dev Log: Building a Secure RAG Agent for 150k Records
Krunal Bhimani
Krunal Bhimani

Posted on

Dev Log: Building a Secure RAG Agent for 150k Records

The 150,000 Record Nightmare

We’ve all been there. You inherit a legacy dataset, and suddenly you are expected to make it perform like a modern app.

Recently, a team ran into exactly this wall. A digital procurement platform needed to make over 150,000 records searchable via a chat interface. The old system was choking. Queries were taking forever, lagging out the UI, and ruining the user experience.

To make matters worse, this wasn't just public info. It contained sensitive contracts and bids. Sending that payload to a public third-party automation cloud was strictly against the rules.

The mission was simple but brutal. Build a secure, smart chatbot that could parse thousands of files and answer complex questions in under 3 seconds.

And the deadline was two weeks.

Here is the breakdown of how the engineering team pulled it off using n8n, OpenAI, and RAG.

The Stack

To move fast without breaking security protocols, the team needed a hybrid approach. It had to be part "low-code" for speed and part "hard-code" for control:

  • Orchestration: n8n (Self-hosted)
  • Logic & LLM: OpenAI (GPT-4o) & Pinecone (Vector Database)
  • Backend: Java & PostgreSQL
  • Frontend: WhatsApp Business API & Twilio

Why Self-Hosted n8n?

Going with n8n wasn't just about the drag-and-drop features. It was about keeping the data home. Since the procurement data was highly confidential, the team couldn't risk it leaving the secure environment during the orchestration steps.

By self-hosting n8n directly on the client’s private server, developers got the speed of visual workflow building without the security risks of a SaaS platform. No external logs and no data leaks.

Fixing the "Hanging" Query

If you try to connect an LLM directly to a database of 150k records, you are going to have a bad time. You get timeouts, hallucinations, and angry users.

To get around this, the team built batch processing logic right into the n8n workflows. Instead of trying to fetch or update the entire dataset in one heavy lift, the workflow breaks the data down into optimized chunks. This simple architectural tweak stopped the system from "hanging" while thinking. This is a common headache when chatbots try to chew on too much data at once.

The RAG Factor

To stop the AI from making things up, a Retrieval-Augmented Generation (RAG) system was the only viable path:

  • Ingestion: Procurement records get vectorized and stored in Pinecone.
  • The Ask: When a user asks a question, like "Show me open tenders for construction in Muscat", the system doesn't guess. It queries the vector database first.
  • The Answer: n8n grabs those specific chunks of data and feeds them to OpenAI as context. The LLM then writes a natural language answer based only on those facts.

The Result

By leaning on orchestration tools rather than writing thousands of lines of boilerplate code for API connections, the development timeline collapsed. What typically takes 3 or 4 months of custom dev work was finished in just 2 weeks.

The final system is now live and handling complex queries in roughly 3 seconds. It is a solid proof of concept that enterprise-grade AI agents don't always need to be built from scratch.

Deep Dive

If you want to see the specific workflow nodes, the architecture diagrams, and exactly how the security protocols were handled, you can read the full engineering case study here.

Top comments (0)