n8n AI agent tutorial: build a lead research agent
Build an n8n AI agent step by step: Chat Trigger, AI Agent node, chat model, Simple Memory and tools, using a lead research agent that logs to Google Sheets.
By Md Anjum Kamali · · 6 min read

On this page
An n8n AI agent is the AI Agent node with a chat model, an optional memory and at least one tool connected to it. You trigger it with a Chat Trigger or any other trigger, the model reads the message, decides which tools to call, and returns an answer. Below I build a lead research agent that reads a company's website and logs a short summary to Google Sheets.
The AI Agent node in n8n is a root node that runs a language model in a loop: the model can call the tool sub-nodes you attach, look at what came back, and decide whether to call another tool or reply.
If you're not sure whether you need an agent at all, read AI agents vs workflows first. Plenty of jobs are better as a plain workflow.
What are the parts of an n8n AI agent?
| Part | Node | Required? | What it does |
|---|---|---|---|
| Trigger | Chat Trigger (or Webhook, Schedule, etc.) | Yes | Starts the run and passes in the user's message |
| Agent | AI Agent | Yes | Runs the model, calls tools, returns the answer |
| Model | OpenAI Chat Model, Anthropic Chat Model, Ollama Chat Model and others | Yes | The LLM doing the thinking |
| Memory | Simple Memory, or a database-backed memory | No | Lets the agent remember earlier messages in a chat |
| Tools | HTTP Request, Google Sheets, Call n8n Workflow, Code and many more | At least one | Things the agent can do |
The AI Agent node docs say it plainly: you must connect at least one tool sub-node. Since n8n 1.82.0 every AI Agent node works as a Tools Agent, so there's no agent type to choose anymore.
What will the lead research agent do?
You type a company domain into the chat. The agent:
- Fetches the company's homepage.
- Works out what the company sells and who it sells to.
- Writes a two-line summary and a suggested opening line for a cold email.
- Appends a row to a Google Sheet with the domain, summary and opener.
- Replies in the chat with the same summary.
It's a small version of the research step every outbound team does by hand. If you want the bigger picture of where this fits, how B2B data enrichment works covers the data side.
How to build it, step by step
1. Add a Chat Trigger
Create a new workflow and add the Chat Trigger node. Leave "Make Chat Publicly Available" off while you build. That keeps the chat inside the editor, where you can test it with the Chat button at the bottom of the canvas.
Each message you send runs the workflow once.
2. Add the AI Agent node
Connect an AI Agent node to the trigger. For Prompt, keep "Take from previous node automatically". It expects a chatInput field, which is what the Chat Trigger sends.
Open Options and add a System Message. This is where the agent's job description goes:
You are a B2B lead researcher.
The user gives you a company domain.
1. Use the fetch_website tool to read https://<domain>.
2. From the page, work out what the company sells and who buys it.
3. Write a summary of at most two sentences.
4. Write one opening line for a cold email that mentions something specific from the site.
5. Use the log_lead tool to save the domain, summary and opener.
6. Reply with the summary and opener.
If the site doesn't load or says nothing useful, say so. Never guess.
The last line matters. Without it, models tend to invent a plausible company description when the fetch fails.
3. Connect a chat model
Click the Chat Model slot under the agent and pick one. I'd start with a hosted model from OpenAI or Anthropic, because tool calling is where small models fall over. Add your API key as a credential.
If you'd rather keep everything local, the Ollama Chat Model works too. When n8n runs in Docker and Ollama on the same machine, the n8n docs for Ollama say to use http://host.docker.internal:11434 as the base URL. I cover that setup in running an LLM locally with Ollama.
4. Add Simple Memory
Click the Memory slot and add Simple Memory. It keeps the last few exchanges in the conversation, so you can follow up with "now do the same for their competitor" and the agent knows what you mean.
Two caveats from the docs. Simple Memory doesn't persist between sessions. And if you run n8n in queue mode with several workers, don't use it in production, because calls can land on different workers. Swap in a Postgres or Redis memory node for that.
5. Add the website tool
Click the Tool slot and add the HTTP Request tool. Name it fetch_website and give it a description the model will read:
Fetches the HTML of a company homepage. Input is a full URL like https://example.com.
Set the method to GET. For the URL, let the model fill it in:
{{ $fromAI('url', 'The full homepage URL to fetch, starting with https://', 'string') }}
Turn on Optimize Response and pick Text. A raw homepage is mostly scripts and markup, and you don't want to pay for the model to read all of that.
6. Add the Google Sheets tool
Make a sheet with three columns: domain, summary, opener. Add a Google Sheets tool, name it log_lead, choose the Append Row operation and pick your sheet.
For each column, map a $fromAI() value:
domain: {{ $fromAI('domain', 'The company domain, without https://', 'string') }}
summary: {{ $fromAI('summary', 'Two-sentence summary of what the company sells and to whom', 'string') }}
opener: {{ $fromAI('opener', 'One cold email opening line based on the website', 'string') }}
The $fromAI docs list the arguments in order: key, description, type and an optional default. It only works in tools connected to an AI Agent.
7. Test it
Open the chat and type a domain. Watch the canvas: the agent node lights up, then fetch_website, then log_lead. Click into the agent's output to see each tool call and what came back. That log is the fastest way to debug.
What goes wrong, and how to fix it
| Problem | Usual cause | Fix |
|---|---|---|
| Agent never calls a tool | Vague tool name or description | Name tools by what they do and say what input they expect |
| Agent makes up company details | No instruction for failure | Tell it in the system message to say so when a fetch fails |
| Run stops with a max iterations error | The agent keeps looping | Tighten the prompt, or raise Max Iterations (the default is 10) |
| Sheets row has empty columns | $fromAI descriptions too vague |
Describe each value precisely, including format |
| Huge token bills | Full HTML sent to the model | Turn on Optimize Response on the HTTP Request tool |
When should you not use the AI Agent node?
If every run does the same steps in the same order, you don't need an agent. My signup alert workflow is four fixed nodes with no model in it, and it's more reliable for that.
Agents earn their place when the input is messy and the next step depends on what the model finds. A company website is a good example: some are clear, some are a single tagline, some redirect three times. Letting the model decide what to do with each one is the point.
Taking it further
Once this works in chat, swap the Chat Trigger for a Schedule Trigger that reads new rows from a sheet, and you have a batch researcher. Add a second tool that searches the web, and it can find a company's domain from just a name.
This is close to what we built into Prospectee, where an agent browses company websites using the user's own LLM key. One user ran it across 45,000 rows for under $2 in model fees. You don't need that scale to get value, though. Ten good researched leads a day from an n8n agent is already more than most people do by hand.
Questions people ask
Does the n8n AI Agent node need a tool?
Yes. n8n's docs say you must connect at least one tool sub-node to the AI Agent node. If you only want a model to answer from a prompt with no actions, a Basic LLM Chain is the simpler choice.
Can I use a local model with the n8n AI Agent?
Yes, through the Ollama Chat Model sub-node. If n8n runs in Docker and Ollama runs on the host, set the Ollama credential's base URL to http://host.docker.internal:11434. Small local models are less reliable at calling tools than large hosted ones.
Why does my n8n agent not remember earlier messages?
You need a memory sub-node such as Simple Memory attached to the agent. Simple Memory keeps a window of recent messages per session and doesn't persist between sessions, so use a database-backed memory if you need history to stick.
What is $fromAI in n8n?
It's a function you put in a tool's fields so the model fills in that value at run time. For example $fromAI('company', 'The company name', 'string') lets the agent decide what goes in a Google Sheets column.
