Building an AI agent used to require a software engineering background, knowledge of APIs, and weeks of development time. Not anymore. n8n — an open-source, self-hostable workflow automation tool — lets you build production-grade AI agents through a visual canvas, with no code required.
In this guide, I'll walk you through building a complete AI agent from scratch: one that receives messages via a webhook, reasons with OpenAI, remembers past conversations, and responds intelligently — deployed and running in production.
A conversational AI agent that accepts messages via webhook, maintains memory across sessions, queries OpenAI GPT-4, and sends structured responses. Deployable to any server or n8n Cloud.
What Is n8n?
n8n (pronounced "n-eight-n") is a workflow automation platform that connects apps and services through a visual node-based editor. Unlike Zapier or Make, n8n is open-source, can be self-hosted, and — crucially — has built-in support for AI agents through its LangChain integration.
The key difference between a standard n8n automation and an n8n AI agent: automations follow fixed paths; agents reason about what to do next. They can use tools, access memory, and adapt their responses based on context.
Set Up n8n
You have two options: n8n Cloud (easiest, free trial available) or self-hosted (more control, free forever).
For self-hosting with Docker, run:
Navigate to http://localhost:5678 to access the editor. For cloud, sign up at n8n.io/cloud.
Create a New Workflow
In the n8n editor, click New Workflow. You'll see a blank canvas. Every agent starts with a trigger — the event that kicks things off.
For our agent, we'll use a Webhook trigger:
- Click the + button to add a node
- Search for "Webhook" and select it
- Set the HTTP Method to POST
- Copy the Webhook URL — this is what you'll send messages to
- Click Listen for Test Event to activate the webhook temporarily
Test it by sending a POST request:
Add the AI Agent Node
This is where the magic happens. After your webhook node:
- Click + to add a new node
- Search for "AI Agent" — you'll see it under the Advanced AI category
- Select AI Agent
The AI Agent node is n8n's LangChain wrapper. It manages the reasoning loop: it receives input, decides which tools to use (if any), and produces a response.
The AI Agent node requires a Chat Model sub-node and optionally a Memory sub-node. These connect to the agent via the small circular connectors on the node's bottom edge.
Connect OpenAI
Connect a language model to the AI Agent node:
- Click the Chat Model connector at the bottom of the AI Agent node
- Add an OpenAI Chat Model node
- Create an OpenAI credential (paste your API key)
- Set the model to gpt-4o for best results
- Set Temperature to 0.3 for consistent, focused responses
In the AI Agent node itself, set the System Message to define your agent's personality and capabilities. For example:
Add Memory
Without memory, every message is a fresh conversation — the agent has no idea what was said before. Add the Window Buffer Memory node to fix this:
- Click the Memory connector on the AI Agent node
- Add Window Buffer Memory
- Set Session ID to
{{ $json.session_id }}— this pulls the session ID from your webhook payload - Set Context Window Length to 10 (keeps the last 10 message pairs)
Now each user has their own conversation thread. Send two messages with the same session_id and the agent will remember the first when responding to the second.
Map the Input and Format the Output
The AI Agent node needs to know where to find the user's message. In the agent's Text field, set:
After the AI Agent node, add a Respond to Webhook node to send the response back to the caller:
- Add a Respond to Webhook node after the AI Agent
- Set Response Body to:
Add Tools (Optional but Powerful)
What makes agents truly powerful is their ability to use tools — external capabilities the agent can call on demand. Common tools to add:
- HTTP Request tool — lets the agent query any REST API (e.g., your CRM, order database)
- Code tool — lets the agent run JavaScript for calculations or data processing
- Google Sheets tool — read/write spreadsheet data in response to queries
- Wikipedia tool — gives the agent access to factual lookups
To add a tool, click the Tools connector on the AI Agent node and attach any supported tool node. The agent will decide autonomously when to invoke each tool based on the user's request.
Activate and Deploy
Once your workflow is tested and working:
- Click Save in the top right
- Toggle the Active switch to turn on the workflow
- Your webhook URL is now live and production-ready
The webhook URL stays the same once the workflow is active. You can integrate it into any frontend, Slack app, WhatsApp bot, or customer support system.
Add error handling by connecting an Error Trigger node that sends you a Slack or email alert when the agent fails. This is essential for production agents — you want to know immediately if something breaks.
What to Build Next
Now that you have a working agent, here's what to layer on:
- Lead qualification agent — Connect your CRM via HTTP Request tool; the agent scores and tags leads automatically
- Document Q&A agent — Add a vector store (Pinecone or Supabase) and the agent can answer questions about your docs
- Multi-agent system — Chain multiple agents together, each specializing in a different task
The n8n AI Agent node is one of the fastest ways to go from idea to a working AI agent. If you want help designing or building your agent, reach out to the Xelionlabs team — we build these every day.
Explore Further
Now that you understand how to build an AI agent with n8n, go deeper:
- n8n + OpenAI Integration Guide — detailed setup walkthrough and agent examples
- Lead Qualification AI Agent — see a complete agent built exactly like this one
- Customer Support AI Agent — RAG-powered support automation
- What is an AI Agent? — the full definition and how agents work
- What is RAG? — add a knowledge base to your n8n agent
- n8n vs Zapier — why n8n wins for AI agent workflows
- Xelionlabs AI Agent Development — have us build it for you