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.

What You'll Build

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.

1

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:

# Pull and start n8n docker volume create n8n_data docker run -it --rm \ --name n8n \ -p 5678:5678 \ -v n8n_data:/home/node/.n8n \ docker.n8n.io/n8nio/n8n

Navigate to http://localhost:5678 to access the editor. For cloud, sign up at n8n.io/cloud.

2

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:

  1. Click the + button to add a node
  2. Search for "Webhook" and select it
  3. Set the HTTP Method to POST
  4. Copy the Webhook URL — this is what you'll send messages to
  5. Click Listen for Test Event to activate the webhook temporarily

Test it by sending a POST request:

curl -X POST https://your-n8n-url/webhook/your-id \ -H "Content-Type: application/json" \ -d '{"message": "Hello agent!", "session_id": "user_123"}'
3

Add the AI Agent Node

This is where the magic happens. After your webhook node:

  1. Click + to add a new node
  2. Search for "AI Agent" — you'll see it under the Advanced AI category
  3. 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.

Important

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.

4

Connect OpenAI

Connect a language model to the AI Agent node:

  1. Click the Chat Model connector at the bottom of the AI Agent node
  2. Add an OpenAI Chat Model node
  3. Create an OpenAI credential (paste your API key)
  4. Set the model to gpt-4o for best results
  5. 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:

You are a helpful assistant for Acme Corp. You help customers with order tracking, product questions, and returns. Always be concise and professional. If you don't know the answer, say so honestly.
5

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:

  1. Click the Memory connector on the AI Agent node
  2. Add Window Buffer Memory
  3. Set Session ID to {{ $json.session_id }} — this pulls the session ID from your webhook payload
  4. 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.

6

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:

{{ $('Webhook').item.json.message }}

After the AI Agent node, add a Respond to Webhook node to send the response back to the caller:

  1. Add a Respond to Webhook node after the AI Agent
  2. Set Response Body to:
{ "reply": "{{ $json.output }}", "session_id": "{{ $('Webhook').item.json.session_id }}" }
7

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:

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.

8

Activate and Deploy

Once your workflow is tested and working:

  1. Click Save in the top right
  2. Toggle the Active switch to turn on the workflow
  3. 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.

Production Tip

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:

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: