How to build an AI agent that qualifies and scores leads with n8n
By Max Warnault · 22 April 2025 · 8 min read
In short
You can build an AI agent in n8n that qualifies leads from GoHighLevel. A GoHighLevel webhook sends the lead's details and message to n8n. An AI agent running on an OpenAI model works through a set list of qualifying questions, remembers the conversation by contact ID, saves the lead's first name back into GoHighLevel, and returns JSON with the next question, the next step and a lead score with its reasoning. The score tells you where to send the lead next.
Qualifying leads by hand takes time. Someone has to ask the same questions to every new contact, read the answers and decide who is worth a call.
In this article I show you how I build an AI agent that does that work. It qualifies your leads and gives each one a score. Once a lead has a score, you can send it to another AI agent: for example a scheduling agent that books calls, or a sales agent that sells low ticket products for you. I build it from a clean instance of n8n, connected to GoHighLevel, step by step.
What you need before you start
- An n8n instance (I start from a clean one)
- A GoHighLevel account, where the leads and conversations live
- An OpenAI account, for the API key that runs the chat model
Step 1: Set up the webhook in n8n
Everything starts with a webhook node.
- Add a Webhook node and rename it. I call mine "AI setter".
- Keep the HTTP method on POST.
- In the response setting, choose "Using 'Respond to Webhook' Node". That means the webhook receives the data, does some work, and then answers with whatever you choose to send back, here the AI's reply.
- Leave authentication for later. You don't need anything else on this node yet.
Step 2: Add the AI agent node
Next, add an AI Agent node (under Advanced AI). Set the prompt source to "define below". The text the agent reads will be the lead's message.
To map that message, you need real data coming in, so you have to run the webhook once.
Step 3: Send test data from GoHighLevel
- In n8n, copy the webhook's test URL.
- In GoHighLevel, paste it into a custom webhook action. Set the method to POST.
- Pass the contact's variables from GoHighLevel into the webhook. I send the ID, first name, email, phone, message, location and source.
- In n8n, click to test the workflow so it listens for an event, then send the test request from GoHighLevel.
The execution shows up in n8n with all those fields. The message field is what the AI agent will use. In the AI Agent node, drag the message into the prompt field.
Then add the Respond to Webhook node after the agent. You can start with "first incoming item", and later switch to "all incoming items". Set it to respond with text and pass in what the AI agent gives you.
Step 4: Connect the OpenAI chat model
The agent needs a chat model. I use an OpenAI chat model, which means ChatGPT.
- Go to platform.openai.com, log in and open settings.
- Create a new API key in the default project and copy the secret key.
- Paste it into a new OpenAI credential in n8n and save it.
- Pick the model. I start with GPT-4o mini.
- Set the response format to always be JSON.
n8n warns you about one thing here: if you use the JSON format, the word "JSON" has to appear somewhere in your prompt. Keep that in mind when you write the system message.
Step 5: Give the agent memory
Add a Simple Memory node. Set the session key to "define below" and use the ID of the contact that comes in from the webhook. I set the context window length to 100.
The agent now stores the conversation per contact, so when a lead replies, it knows what it asked before.
Step 6: Write the system prompt for your AI setter
In the agent's options, add a system message. Mine tells the agent it is a setter. It asks a series of questions, one by one, and at the end it scores the lead and gives a reason for that score. One of the questions is "Would you be open to some ideas?"
In my version the questions are hardcoded in the prompt to make it easier. You can change them to fit your business. In general, you write the prompt so the agent asks the questions and the conversation flows the way you want.
Step 7: Add an output parser so the reply is clean JSON
Turn on "require specific output format" and add a structured output parser. Use "generate from JSON example" and paste in an example of the object you want.
Mine has an "output" object, and inside it:
- the question to send the lead
- the next step
- the reasoning for the next step
- the lead score (with its reasoning)
I wrap everything in "output" because the AI sometimes adds that key on its own. Planning for it keeps the result consistent.
At this point I select all the nodes and tidy the layout, and I give them short names (like "OpenAI" and "Output") so they don't overlap on the canvas.
Step 8: Connect n8n to the GoHighLevel API
Now the agent needs a tool that can write to your CRM.
- Add a HighLevel API v2 node and create a new credential. I use the white label option.
- Go to the GoHighLevel marketplace site and sign in.
- Create a new client key. Name it something like "n8n" and add it. You get a client ID and a client secret.
- Copy the scopes into the credential, separated by a single space.
Watch the spaces. The scopes I copied had double spaces, so I pasted them into Visual Studio Code and replaced every double space with a single one. I also had a connection issue because of a trailing space at the end. Once that was cleaned up, I logged into the CRM and the connection worked.
Step 9: Let the agent update the lead's first name
Add the GoHighLevel tool to the agent and set it to update a contact:
- The contact ID comes from the webhook.
- The field to update is the first name.
- The value is set to be defined automatically by the model.
Rename the tool "update first name".
Then update the prompt with a rule for the start of the conversation:
- If the first name is empty, the first message is "Hey there, what's your first name?" When the lead answers, the agent calls "update first name" to save it.
- If the first name is not empty, the agent starts the qualification flow.
Since the prompt needs the word JSON, I made sure it's in there. I also removed a line that had become redundant.
Step 10: Test the AI agent
I test with the same contact. The workflow runs through the agent, responds to the webhook, and the reply comes back: "Hey Maxim, so you're looking to scale your coaching business, right?"
Then I hardcode the incoming message as "yes" and test again. Because of the memory, the agent knows this is a reply to its previous message. You can see it in the chat history. It answers with something like "Cool, I appreciate you sharing. What client acquisition method have you been using so far? Ads, outreach..." and keeps going through the questions until the end, where it scores the lead.
Next I test the empty first name case. Memory already holds a past conversation, so I remove the memory node to be sure the agent behaves as if this were the first message. I send a test with no first name, and it asks "Hey there, what's your first name?" I reply with my name, test again, and the agent calls the "update first name" tool. The contact's first name is saved in GoHighLevel, and the agent moves back to the normal flow.
Finally, I switch the webhook off test mode, save, and the AI agent is live, qualifying people with a series of questions you can update any way you want.
Going further: feed the agent the full conversation from GoHighLevel
One more thing I did is a little more advanced. Even with memory, the agent won't remember everything. So I added steps before the agent that pull the full conversation from GoHighLevel and feed it in before the agent is called. That way the prompt has the full history, and the agent can be reminded of things that were said in the past.
This is the kind of system we build at Million Dollar Year: software that runs while you sleep, so a solo business can qualify every lead without hiring someone to do it.
Key takeaways
- Start the n8n workflow with a webhook set to POST that answers through a Respond to Webhook node.
- Send the contact's ID, first name, email, phone, message, location and source from a GoHighLevel custom webhook.
- Set the OpenAI model's response format to JSON and include the word JSON in your system prompt.
- Key the agent's simple memory to the contact ID so it remembers each lead's conversation.
- Use a structured output parser so every reply returns the next question, the next step and a lead score with its reasoning.
- Give the agent a GoHighLevel tool that updates the contact's first name when it's missing.
Questions and answers
How do I connect n8n to GoHighLevel for an AI agent?
Create a HighLevel API v2 credential in n8n, then go to the GoHighLevel marketplace, sign in and create a client key to get a client ID and client secret. Paste the scopes separated by single spaces, because double spaces or a trailing space can break the connection. Then log into your CRM to finish the connection.
How does the AI agent score leads?
The system prompt tells the agent to ask a series of qualifying questions and, at the end, score the lead and give a reason for that score. A structured output parser makes it return JSON with the next question, the next step and its reasoning, and the lead score.
Which OpenAI model should I use for an n8n lead qualification agent?
I start with GPT-4o mini through the OpenAI chat model node, with the response format set to JSON. n8n requires the word JSON in the prompt when you use that format.
Do I need memory for an AI setter in n8n?
Yes. A simple memory node keyed to the contact ID lets the agent know that a reply like "yes" answers its previous question. Memory won't hold everything, so for more context you can pull the full conversation from GoHighLevel and feed it to the agent before it runs.
What do I do with a lead once it is scored?
You can send it to another AI agent. For example, a scheduling agent can book calls, or a sales agent can sell low ticket products for you.