DEV Community

Cover image for Getting Started with Lead Scoring in n8n Using GPT-4o-mini
Bernard K
Bernard K

Posted on

Getting Started with Lead Scoring in n8n Using GPT-4o-mini

Building a lead scoring pipeline wasn't initially on my agenda, but necessity demanded it. Working with IoT and AI in Kenya often means grappling with unreliable internet and outdated hardware. Wherever you're working, leads are vital. To prioritize sales leads cost-effectively, I decided to test n8n. It wasn't merely about saving money; I wanted to see if a low-code tool could handle such an important task.

Why n8n and GPT-4o-mini?

I had used n8n for other automation tasks and was intrigued by its versatility. Being open-source, it has no licensing issues or hidden fees, which is crucial on a budget. The bigger question was whether it could integrate well with GPT-4o-mini for accurate lead scoring. GPT-4o-mini attracted me with its lightweight nature compared to full GPT versions, which suited my operating conditions. A strong, stable connection isn't always available, so something needing minimal cloud interaction was essential.

Setting up the workflow

I set up my n8n instance, integrating it with daily tools like our CRM and email. The workflow needed to automate data gathering, scoring, and pushing results back into our CRM. n8n handled the flow, while GPT-4o-mini managed the scoring.

Here is the core part of the workflow:

const axios = require('axios');

// This node triggers when a new lead enters the system
function onNewLead(leadData) {
    // Format the data for GPT-4o-mini
    const formattedData = JSON.stringify(leadData);

    // Call GPT-4o-mini for scoring
    axios.post('http://gpt-4o-mini.local/score', formattedData)
        .then(response => {
            const score = response.data.score;
            // Push the score back to the CRM
            updateLeadScoreInCRM(leadData.id, score);
        })
        .catch(error => {
            console.error('Error scoring lead:', error);
        });
}

function updateLeadScoreInCRM(leadId, score) {
    // Integration with CRM API
    axios.post(`http://crm.local/api/leads/${leadId}`, { score })
        .then(() => console.log(`Lead ${leadId} scored with ${score}`))
        .catch(error => console.error('Error updating CRM:', error));
}
Enter fullscreen mode Exit fullscreen mode

This code manages communication with both GPT-4o-mini and our CRM. n8n triggers when a new lead arrives, ensuring scores get stored back into the CRM efficiently.

Challenges and learnings

Setting this up was anything but straightforward. I encountered latency issues due to intermittent connectivity, especially in initial tests. The response time from the GPT-4o-mini server exceeded 5 seconds about 40% of the time. Adding retry logic reduced response failures by nearly 60%. Here's the code:

function scoreLeadWithRetry(leadData, retries = 3) {
    let attempt = 0;

    function attemptScore() {
        attempt++;
        axios.post('http://gpt-4o-mini.local/score', leadData)
            .then(response => {
                const score = response.data.score;
                updateLeadScoreInCRM(leadData.id, score);
            })
            .catch(error => {
                if (attempt < retries) {
                    console.warn(`Retry ${attempt}: Scoring lead failed, retrying...`);
                    attemptScore();
                } else {
                    console.error('Scoring lead failed after several attempts:', error);
                }
            });
    }

    attemptScore();
}
Enter fullscreen mode Exit fullscreen mode

This retry strategy helped make the process more reliable. Working within hardware restrictions also forced me to simplify data structures as much as possible.

The results

Once sorted, I tested the system live with 100 leads. Processing time dropped from 4 minutes to just about 1.5 minutes post-optimization, which was a win. The scores helped the sales team prioritize their efforts more effectively, improving conversion rates by around 10% according to their initial feedback.

Final thoughts

This was my first time merging n8n with a model like GPT-4o-mini for lead scoring, and it was a rewarding experience. For environments with flaky connectivity, on-the-ground constraints, or budget issues, this setup is worth considering. However, if you're after real-time processing with zero delays, a more advanced system might be needed. Still, for my needs, it balanced functionality and economy well.

Next, I'm looking into running sentiment analysis on communications with these leads to further refine scoring. It's about maximizing the advantages of the tools available while facing the realities of my operating environment.

Top comments (0)