Build an AI Agent with n8n and OpenAI: A Working Walkthrough
A real n8n + OpenAI agent walkthrough: form trigger, search API, HTML extraction, GPT-4.1 summarisation, CSV output, plus the production gotchas tutorials skip.
Updated May 2026. Rewritten for current n8n nodes, modern OpenAI models (gpt-4.1, gpt-4o-mini), and the search API approach we actually use in production.
Most n8n + OpenAI tutorials show you a clean happy path that breaks the moment you run it twice. This one will not. We have built this exact agent for half a dozen clients, and the version below is the pattern that survives a real workload.
At Osher Digital, we are a Brisbane-based AI and automation consultancy. We deploy n8n and OpenAI agents for businesses that need real research, document processing, and reporting workflows. This walkthrough builds a small but realistic web research agent: it accepts a search term, fetches relevant pages, summarises each one with GPT-4.1, and produces a CSV report.
If you are new to AI agents in general, start with our guide to what an AI agent actually is. If you want a code-first version of the same idea, see how to build an AI agent in Python. This guide is for people who want to build agents inside n8n itself.
What This Walkthrough Builds
A web research agent that takes a search query, runs it through a real search API, fetches the top results, summarises each page with OpenAI, and returns a CSV the user can download. The whole flow runs in n8n with no custom code outside small expressions.
The flow looks like this:
Form Trigger → Search API (HTTP Request) → Extract URLs →
Split Out → Fetch Page → Strip HTML →
OpenAI Chat Model → Aggregate → Convert to CSV →
Respond to Webhook
One detail upfront. The original version of this tutorial scraped Google search results directly. That was always fragile and Google has tightened detection further since 2024. We now use a real search API (Tavily or Brave Search) for any client work. It is more reliable, the rate limits are documented, and a query costs less than a cent.
The Modern n8n AI Agent Stack
n8n has changed a lot since 2024. The native AI Agent node, AI nodes for OpenAI / Anthropic / Google, and the built-in vector store nodes mean you do not have to wire up raw HTTP calls for most LLM work. The walkthrough below uses the OpenAI Chat Model node directly. For more complex agents (tool calling, memory, multi-step reasoning), we use the AI Agent node, but for a sequential summarisation pipeline like this one the simpler approach is faster and easier to debug.
What we use across our client work in 2026:
Prerequisites
Before you start, make sure you have:
Add credentials for OpenAI and Tavily (HTTP Header Auth) under Credentials before you start dropping nodes. It saves a tab switch later.
Step 1: The Form Trigger
Start with an n8n Form Trigger. This gives you a public URL with a real form, no front-end work. Configure:
researchquery, requiredOne thing to know: the Form Trigger’s “Wait for Workflow” mode keeps the user’s browser tab waiting for up to 120 seconds by default. For a workflow that fetches and summarises five pages, this is fine. For anything longer, redirect to a status page instead.
Step 2: The Search API Call
Add an HTTP Request node after the Form Trigger. Configure for Tavily:
https://api.tavily.com/searchAuthorization: Bearer YOUR_KEY){
"query": "{{ $json.query }}",
"search_depth": "basic",
"max_results": 5,
"include_raw_content": false
}
Tavily returns a clean JSON response with results as an array of { title, url, content, score } objects. We do not ask for raw content here because we will fetch it ourselves in step 5. This keeps the Tavily call fast and lets us strip noise (cookie banners, navigation) before paying for OpenAI tokens.
Step 3: Split Out Results
Add a Split Out node. Configure:
resultsYou now have five items, each a single search result with title, url, and content. Every node downstream of this runs once per item. n8n handles the iteration automatically.
Step 4: Fetch Each Page
Add another HTTP Request node:
{{ $json.url }}Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36Realistic completion rate: 4 of 5 pages will return clean HTML. The fifth is a coin flip depending on the destination.
Step 5: Strip HTML Down to Readable Text
Sending raw HTML to GPT-4.1 burns tokens for nothing. Use a Code node to extract just the readable text:
const html = $input.item.json.data || '';
// Remove script, style, and noscript blocks
let text = html
.replace(/<script[\s\S]*?<\/script>/gi, '')
.replace(/<style[\s\S]*?<\/style>/gi, '')
.replace(/<noscript[\s\S]*?<\/noscript>/gi, '')
.replace(/<[^>]+>/g, ' ')
.replace(/\s+/g, ' ')
.trim();
// Cap at 8000 characters. GPT-4.1 can handle far more
// but you rarely need it for summarisation.
if (text.length > 8000) {
text = text.substring(0, 8000);
}
return {
title: $('Split Out').item.json.title,
url: $('Split Out').item.json.url,
text: text
};
For production work we use the readability npm package via a custom n8n function or call out to a service like Mercury Parser. For a working tutorial, the regex approach above is fine. It handles 95% of pages and the failure mode is “the LLM gets messy text” not “the workflow crashes”.
Step 6: Summarise with OpenAI
Add an OpenAI node (or the OpenAI Chat Model node if you prefer the AI-flavoured version):
gpt-4.1 for quality, gpt-4o-mini if you want it cheap and the source pages are simpleTitle: {{ $json.title }}\n\nText:\n{{ $json.text }}Cost on gpt-4.1 for five pages of 8K characters each: roughly $0.04 USD per run. On gpt-4o-mini: under half a cent. We default to gpt-4o-mini for high-volume client work and only step up to gpt-4.1 when the summary quality demands it. The summary quality difference is real but smaller than people expect for plain summarisation.
Step 7: Aggregate and Convert to CSV
After OpenAI returns five summaries (one per item), pull them back together and write a CSV.
Add a Set node first to clean up the structure:
title = {{ $('Strip HTML').item.json.title }}url = {{ $('Strip HTML').item.json.url }}summary = {{ $json.message.content }} (the OpenAI output field)Then add a Convert to File node:
dataresearch-{{ $now.format('yyyyMMdd-HHmm') }}.csvStep 8: Return the File to the User
The final node is a Respond to Webhook (renamed “Respond to Form” in some versions). Configure:
data (the binary field from Convert to File)Content-Disposition = attachment; filename="research.csv"Save the workflow, activate it, open the form URL, type a query, hit submit. Twenty seconds later the browser downloads a CSV with five summarised search results. The whole flow on a quiet n8n instance takes 15-25 seconds wall-clock for a five-result run.
Things That Will Break
The four production gotchas we have hit on this exact pattern:
Pages that hang. Some sites accept the connection and never respond. Always set a timeout on the HTTP Request node. 15 seconds is generous; we use 8 in production.
OpenAI rate limits. If you scale this to do 100 queries in a minute, you will hit per-minute token limits before request limits. Add a Wait node (or use the Loop Over Items node’s batching feature) to space out OpenAI calls. We typically run 5 concurrent at most.
Garbage input pages. Some pages are pure JavaScript with almost no HTML. The text you extract is “Loading…” and OpenAI dutifully summarises that. Add a length check after Strip HTML; skip pages with under 200 characters and log them.
The encryption key on credential update. If you rotate your n8n encryption key without backing up first, every credential including the OpenAI key needs re-entering. We have done this once. It is twenty minutes of manual work and a hard lesson.
When This Pattern Is Not the Right Tool
The HTTP-Request-and-summarise pattern is great for narrow research jobs. It is the wrong tool for:
Conversational agents. If users want to chat back and forth, use the n8n AI Agent node with chat memory and tool calling. The chain we built above is a one-shot pipeline, not a conversation.
Document Q&A over a known corpus. Build a vector store with the n8n Vector Store nodes (Pinecone, Qdrant, or PGVector) and use retrieval. Web scraping every time is wasteful when the source documents do not change.
Scheduled monitoring. If you want daily or weekly research, swap the Form Trigger for a Cron node and route results to email or Slack. We run a few of these for clients tracking competitor announcements.
Anything that needs Python’s data libraries. n8n’s Code node is JavaScript by default. You can switch to Python in newer versions but the libraries available are limited. For real ETL or ML work, run a Python service alongside n8n and call it via HTTP.
Costs: Realistic Numbers
For a single research run with five results:
For 1000 runs a month on gpt-4.1 you are looking at around $45 USD ($70 AUD) all-in including the search API. On gpt-4o-mini it drops to about $8 USD ($12 AUD). Self-hosted n8n on a $20 AUD per month VPS adds the only fixed cost.
Want this built out for your business with multi-step research, scheduled runs, and CRM integration? Book a call with our team. We have shipped this pattern in three different industries.
Frequently Asked Questions
How do I build an AI agent with n8n and OpenAI?
Wire a trigger node to an OpenAI Chat Model node, optionally add tool nodes the model can call (HTTP Request, Postgres, Slack), and pipe the response to wherever the output needs to go. For simple sequential flows like the one in this guide, you do not need the AI Agent node, just chain the OpenAI node into the rest of your workflow. For multi-step reasoning or tool use, use the AI Agent node with the OpenAI Chat Model attached as the language model.
Which OpenAI model should I use in n8n?
For high-volume cheap work like classification or short summaries, gpt-4o-mini. For complex reasoning, longer outputs, or anything user-facing, gpt-4.1. The o-series reasoning models are useful for agents that plan multi-step actions but they are slower and more expensive. Avoid gpt-3.5-turbo in 2026; quality is materially behind and the cost difference vs gpt-4o-mini is small.
How do I extract HTML content in n8n?
The HTML node (“Extract HTML Content”) accepts a JSON property containing HTML and a CSS selector, and returns the matched content. For LLM input we prefer stripping all HTML to plain text using a Code node with a simple regex (shown above), because LLMs do not need the markup and you save tokens. The HTML node is best for structured extraction where you want a specific element.
Can I build agentic AI in n8n?
Yes. The AI Agent node supports tool calling, memory, and multi-step planning out of the box. Attach an OpenAI Chat Model (or Anthropic, or a self-hosted model via Ollama), wire up tools the agent can call (HTTP Request, Postgres, Slack, Gmail, custom code), and the agent will plan and execute multi-step tasks. For simpler sequential flows like research-and-summarise, a plain chain of nodes is faster and easier to debug.
How much does it cost to run this agent?
For 1000 research queries per month, expect $12-$70 AUD per month in API costs depending on the OpenAI model. Self-hosted n8n adds a fixed $20-$30 AUD per month for a small VPS. n8n Cloud’s lowest paid tier is $24 USD per month and is fine for prototyping or low volume. For most small businesses running back-office automation, total monthly cost lands around $50-$100 AUD all-in.
How do I loop through an array in n8n?
Use the Split Out node. Point it at the field containing the array and every downstream node will run once per element. For more control over batching (rate limits, parallelism), use the Loop Over Items node which lets you set a batch size and process items in chunks. Split Out is the right choice for this walkthrough; Loop Over Items is what we reach for when calling rate-limited APIs at scale.
How do I upload a file to n8n from a form?
Add a File element to the Form Trigger and the uploaded file becomes a binary in the workflow. From there, route it to whichever node needs it (HTTP Request to upload to S3, OpenAI for vision input, Convert to File to transform). For multi-file uploads, the Form Trigger returns binaries indexed by field name; iterate over them with a Code node if you need to.
Should I use n8n Cloud or self-host for AI agents?
Self-host the moment you start sending sensitive data through OpenAI nodes or you need more than a few credential pairs across workflows. n8n Cloud is excellent for prototyping but the per-execution pricing makes high-volume agentic work uneconomical fast. Our threshold: more than 100 runs per day, or any client data that should not leave a controlled environment. Self-hosting on Docker is the path of least resistance.
If you need help building production AI agents in n8n that handle real workloads, integrate with your CRM and database, and survive a busy day, get in touch with our team. We are based in Brisbane and ship n8n + OpenAI agents for businesses across Australia.
Jump to a section
Ready to streamline your operations?
Get in touch for a free consultation to see how we can streamline your operations and increase your productivity.