DEV Community

Pirate Prentice
Pirate Prentice

Posted on

n8n AI Agent Node: Build Your First AI Agent Workflow (Free JSON)

The n8n AI Agent node lets you build autonomous AI agents inside your workflows — agents that can use tools, make decisions, and loop until a task is done. It's one of the most powerful (and most searched) features in n8n right now.

This guide walks you through how it works, when to use it, and gives you a free workflow JSON to get started immediately.


What Is the n8n AI Agent Node?

The AI Agent node is different from a simple OpenAI node. Instead of a single call-and-return, it:

  • Takes a task description (not just a prompt)
  • Uses tools (search, code execution, custom HTTP calls, other n8n nodes)
  • Loops autonomously until the task is complete or it hits a stop condition

Think of it as the difference between asking ChatGPT a question and handing ChatGPT a to-do list with access to your apps.


When Should You Use the AI Agent Node?

Use the AI Agent node when:

  • The task requires multiple steps the LLM decides at runtime
  • You want the LLM to choose which tool to call based on what it finds
  • You need iterative reasoning (e.g., research → summarize → email)

Use a plain OpenAI node when:

  • You have a fixed prompt and want a single response
  • You don't need tool use — just text generation or classification

Core Concepts

1. The ReAct Loop

The AI Agent node runs a ReAct loop: Reason → Act → Observe → Repeat.

  1. LLM receives the task
  2. LLM decides which tool to call (or says it's done)
  3. Tool runs and returns a result
  4. LLM incorporates the result and continues or stops

2. Tools

Tools are what the agent can do. In n8n, you connect tool nodes to the AI Agent:

  • HTTP Request tool — call any external API
  • Code tool — run JavaScript to transform or calculate data
  • Wikipedia tool — search Wikipedia
  • Calculator tool — do math reliably
  • Custom workflow tool — trigger another n8n workflow and get its output

3. Memory

The AI Agent node supports optional memory — a way to persist conversation context across multiple agent invocations. Connect a Memory node (e.g., Window Buffer Memory) to give the agent short-term context.

4. Chat Model

You connect your LLM of choice (OpenAI GPT-4o, Anthropic Claude, Groq, etc.) to the agent's Chat Model input.


Step-by-Step: Build a Research-and-Email Agent

This example agent:

  1. Takes a topic from a webhook
  2. Looks it up on Wikipedia
  3. Summarizes the results
  4. Sends the summary by email

Step 1: Trigger

Add a Webhook node. Set it to POST. This is where the task arrives (e.g., topic: quantum computing).

Step 2: AI Agent Node

Add an AI Agent node. In the System Prompt field:

You are a research assistant. When given a topic, search Wikipedia for it, summarize what you find in 3 bullet points, and return the summary. Be concise.
Enter fullscreen mode Exit fullscreen mode

In the User Message field, reference the webhook input: {{ $json.body.topic }}

Step 3: Connect a Chat Model

Add an OpenAI Chat Model sub-node. Connect it to the AI Agent's Chat Model input. Select model gpt-4o-mini (cheap and fast for this task).

Step 4: Connect the Wikipedia Tool

Add a Wikipedia tool node. Connect it to the AI Agent's Tools input. No configuration needed — the agent will call it automatically when it decides to search.

Step 5: Send the Result by Email

Add a Gmail node after the AI Agent. Map the agent's output: {{ $json.output }}

Step 6: Activate and Test

Test with a webhook POST: { "topic": "n8n workflow automation" }

The agent will search Wikipedia, summarize, and email the results — no fixed prompt needed.


Free Workflow JSON

Copy this into n8n via Import from JSON (Menu → Workflows → Import):

{
  "name": "AI Agent: Research & Email (Starter)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ai-agent-research",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-node",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.body.topic }}",
        "options": {
          "systemMessage": "You are a research assistant. When given a topic, search Wikipedia for information about it. Summarize your findings in 3 clear bullet points. Return only the summary."
        }
      },
      "id": "ai-agent-node",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [480, 300]
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "options": {}
      },
      "id": "openai-model",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [480, 500]
    },
    {
      "parameters": {},
      "id": "wikipedia-tool",
      "name": "Wikipedia",
      "type": "@n8n/n8n-nodes-langchain.toolWikipedia",
      "typeVersion": 1,
      "position": [680, 500]
    },
    {
      "parameters": {
        "sendTo": "you@example.com",
        "subject": "Research Summary",
        "emailType": "text",
        "message": "={{ $json.output }}"
      },
      "id": "email-node",
      "name": "Send Email",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [720, 300]
    }
  ],
  "connections": {
    "Webhook": { "main": [[{ "node": "AI Agent", "type": "main", "index": 0 }]] },
    "AI Agent": { "main": [[{ "node": "Send Email", "type": "main", "index": 0 }]] },
    "OpenAI Chat Model": { "ai_languageModel": [[{ "node": "AI Agent", "type": "ai_languageModel", "index": 0 }]] },
    "Wikipedia": { "ai_tool": [[{ "node": "AI Agent", "type": "ai_tool", "index": 0 }]] }
  },
  "active": false,
  "settings": { "executionOrder": "v1" }
}
Enter fullscreen mode Exit fullscreen mode

Tips for Production AI Agent Workflows

Set a max iterations limit. In the AI Agent node settings, set Max Iterations to 5–10 to prevent runaway loops that burn API credits.

Use gpt-4o-mini for tool-heavy agents. It's 15x cheaper than GPT-4o and handles tool calling well for most tasks.

Add error handling. Connect an Error Trigger node to catch agent failures and notify you via Slack or email.

Log agent outputs to a Google Sheet. Add a Google Sheets node after the agent to record every run — great for debugging and measuring quality over time.

Start with 1–2 tools max. Agents with too many tools get confused. Start simple, add tools only when needed.


What's in the Full Workflow Starter Pack?

The n8n Workflow Starter Pack ($29) includes 10 production-ready workflow JSONs covering common automations — lead capture, Stripe receipts, scheduled reports, Slack notifications, and more — each with full setup docs.

👉 Get the Starter Pack on Gumroad


Your Turn

What are you building with the n8n AI Agent node? Drop your use case in the comments — especially curious what tools people are connecting to their agents.

Top comments (1)

Collapse
 
pirateprentice profile image
Pirate Prentice

What are you building with the n8n AI Agent node? I'm especially curious which tools people are connecting — HTTP Request for custom APIs, Code tool for calculations, or something else entirely? Drop your use case below.