✦

SAAD ELGHABA

Portfolio

Loading... 0%

AI Automation • Prospect

AI Lead Qualification: Automated Sales Triage with n8n and RAG

AI Lead Qualification: Automated Sales Triage with n8n and RAG

An automated n8n workflow that scores incoming leads (0–100) using an LLM grounded in a company knowledge base (RAG). It generates deterministic lead IDs via Google Apps Script, evaluates the lead's intent, and writes the score, category, and reasoning back to Google Sheets.

Client

Prospect

Category

AI Automation

Technologies

N8N, JavaScript

Timeline

Nov 15, 2026 - Nov 15, 2026

The Challenge

Manually reading, evaluating, and prioritizing incoming contact form submissions is time-consuming. It is easy to miss high-value leads or waste time replying to spam and unqualified inquiries.

Strategic Approach

Built an end-to-end n8n automation pipeline. It uses custom Google Apps Script to generate deterministic lead IDs, Google Gemini for document embeddings, and an OpenRouter LLM for RAG-based scoring. The AI evaluates the lead's intent against a company knowledge base and updates the Google Sheet with a score, category, and reasoning.

Measurable Outcomes

Achieved 100% automated lead triage. High-intent leads are instantly flagged with a score above 80, while spam and off-topic messages are automatically scored below 10, allowing me to focus solely on revenue-generating opportunities.

Overview

The Vision: Intelligent Lead Triage

When running a business, every incoming lead from a contact form needs to be evaluated. Doing this manually is slow, inconsistent, and prone to human bias. I wanted a system where a lead could submit a form, and within 60 seconds, it is automatically scored, categorized, and prioritized based on how well it matches my actual business model.

AI Lead Qualification is a fully automated n8n pipeline that bridges Google Sheets, custom Google Apps Script, and a RAG-powered AI Agent. It acts as an autonomous sales assistant, ensuring high-intent leads are separated from spam and unqualified inquiries.


The Architecture

The system operates on two separate but connected flows within a single n8n workflow:

1. Knowledge Base Ingestion (Run Once / On Update)

To ground the AI in reality, I upload PDF/CSV documents detailing my business model, services, and ideal client profile. The workflow loads these documents, generates embeddings using Google Gemini, and stores them in an In-Memory Vector Store.

2. Lead Scoring & Retrieval (Runs Every Minute)

When a new lead arrives in the Google Sheet, the workflow triggers, fetches the lead's details, and passes their comment to an AI Agent (powered by OpenRouter/Nvidia Nemotron). The AI Agent has access to a Query Data Tool that queries the vector store. It scores the lead from 0–100, assigns a category (Hot, Warm, Cold, Spam), and writes the result back to the sheet.


Technical Deep Dive

Deterministic Lead IDs (Apps Script)

To ensure data integrity, every lead needs a unique, stable identifier. I wrote a custom Google Apps Script called IDENTIFIER(). It uses SHA-256 hashing on the timestamp, email, and phone number to generate a deterministic 22-character base64url ID. This ensures that even if the data is synced across multiple systems, the ID remains identical.

// Google Apps Script snippet\nfunction IDENTIFIER(timestamp, email, phone, salt) {\n const payload = (salt ? String(salt) + '|' : '') + t + '|' + e + '|' + p;\n const bytes = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, payload);\n return Utilities.base64EncodeWebSafe(bytes).slice(0, 22);\n}


Strict JSON Output

LLMs are notoriously unpredictable. To ensure the workflow never breaks, the AI Agent is prompted to return a strict JSON contract. A subsequent Code node is responsible for parsing this string (handling markdown fence stripping) and flattening the response into fields that the Google Sheets update node can consume.


The Data Contract

The AI is explicitly instructed to return the following JSON structure, which the workflow uses to update the sheet:

{\n \"identifiant\": \"\",\n \"score\": 85,\n \"category\": \"hot\",\n \"reasoning\": \"The lead mentioned a specific budget and timeline for a full-stack SaaS project.\",\n \"matched_signals\": [\"budget mentioned\", \"SaaS development\", \"urgent timeline\"]\n}


Impact & Results

  • Zero Manual Triage: Every lead is processed and scored automatically within 60 seconds of submission.
  • Context-Aware Scoring: Because the LLM uses RAG, it doesn't just look for keywords; it understands if the lead actually fits the specific services I offer.
  • Actionable Insights: The 'reasoning' and 'matched_signals' columns explain exactly why a lead received a specific score, providing immediate context for follow-ups.

Related Services

Business AutomationAPI Development & Integration

Let's Create Something Extraordinary

Have a project in mind? Let's discuss how we can bring your vision to life.

Start a Project
AI Lead Qualification: Automated Sales Triage with n8n and RAG