Chapter 15

Building AI Agent Workflows

Chapter 15: Building AI Agent Workflows

Starting from n8n v1.x, the AI Agent node became a first-class citizen. Unlike simple LLM nodes that answer one question at a time, an Agent can call tools, reason across multiple steps, and dynamically decide what to do next — all within a single workflow execution. This chapter covers the three agent types, tool configuration, memory modules, and two complete projects: a customer service agent and a research agent.

15.1 The n8n AI Agent Node

The AI Agent node lives under the "Advanced AI" category in the node panel. It wraps an LLM inside a reasoning loop: at each step the model decides whether to call a tool, executes it, and feeds the result back as context — repeating until it arrives at a final answer.

Compared to a Basic LLM Chain node, the Agent adds three key building blocks:

Version note: AI Agent nodes (and all Langchain integration nodes) require n8n v1.19.0 or later. Self-hosted users should confirm the N8N_EXPERIMENTAL_FEATURES=AI env var is set (from v1.30+ it is enabled by default).

15.2 Three Agent Types

Type Reasoning Style Best For Token Cost
Conversational Agent Single-step tool calls with full chat history Customer service, chatbots Low
ReAct Agent Thought → Action → Observation loop Research, multi-step data tasks Medium
Plan-and-Execute Plan all steps first, then execute sequentially Complex long-chain tasks High

Conversational Agent is the lightest type — decides each turn whether to call a tool and keeps all history in context. Best for short conversations (under 20 turns).

ReAct Agent is the most widely used in production. Each reasoning cycle: Thought → Action → Observation, repeated until reaching a Final Answer. n8n logs every step for easy debugging.

Plan-and-Execute first generates a complete plan via a Planner LLM, then executes each step. Planner and executor can use different models (e.g. GPT-4o for planning, GPT-4o-mini for execution).

15.3 Configuring Tools

Tools give an agent its "hands." Connect any sub-node to the AI Agent's Tool port to register it as a callable tool.

Calculator Tool

Handles math, preventing LLM hallucinations on numerical questions. Zero configuration — just connect and go.

Code Tool

Lets the agent write and execute JavaScript or Python snippets for data transformation, regex extraction, and complex string handling. Set the allowed language and a maximum execution timeout (default: 10 seconds).

Security note: The Code Tool runs in a sandbox, but always validate input data and restrict the allowed languages in production. Set strict timeouts to prevent runaway executions.

HTTP Request Tool

Wraps any REST API as an agent tool. The $fromAI() expression tells the agent which fields it should dynamically fill in, with a description to guide the model's choice.

// HTTP Tool config example
{
  "name": "search_products",
  "description": "Search product catalog by keyword. Use when user asks about availability, price, or specs. Input: search query string.",
  "method": "GET",
  "url": "https://api.yourstore.com/products/search",
  "parameters": {
    "q": "={{ $fromAI('query', 'The search keyword') }}",
    "limit": "5"
  }
}

Custom Tools via Tool Workflow

Wrap any n8n sub-workflow as an agent tool using the Tool Workflow node. This lets you package a full pipeline (query DB → filter → format) into a single callable tool.

15.4 Memory Modules

Window Buffer Memory

Keeps the last N turns (user message + assistant reply) in context. A starting value of 10 turns works for most customer service scenarios.

Simple Memory

Retains context only within a single workflow execution. Perfect for one-shot research tasks where cross-session persistence is not needed.

Persistent memory: For cross-session memory, store conversation history in PostgreSQL or Redis and inject it into the Agent's System Message at the start of each execution. This is the standard approach for production conversational systems.

// Window Buffer Memory config
{
  "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
  "parameters": {
    "contextWindowLength": 10,
    "sessionKey": "={{ $json.sessionId }}"
  }
}

// Use the user's ID or chat session ID as the sessionKey
// to keep conversations separated per user

15.5 Project 1: Customer Service Agent

Scenario: Users send product inquiries via Feishu. The agent searches the product knowledge base and live inventory API, responds professionally, and logs every conversation to a MySQL database.

Workflow Steps

  1. Feishu Webhook trigger — extract openId and message text
  2. AI Agent (Conversational) — with three tools attached
  3. Tool 1: search_products — HTTP GET to product search API
  4. Tool 2: check_inventory — HTTP GET to inventory API
  5. Tool 3: escalate_to_human — flags conversation for human handoff
  6. MySQL Insert — log the conversation record
  7. Feishu Send Message — deliver the agent reply to the user
// System Message
You are a professional customer service assistant for YiteAI, answering product questions.

Rules:
1. Always use search_products to look up product info — never answer from memory
2. For inventory questions, always call check_inventory for live data
3. For refunds or complaints, call escalate_to_human
4. Reply concisely and professionally, under 200 words
5. If search returns empty results, honestly say "No information available for that product"

Current user ID: {{ $json.openId }}
Current time: {{ $now.toISO() }}

15.6 Project 2: Research Agent

Scenario: Every weekday at 8 AM, the agent searches for the latest news on configured topics, reads the top 5 articles, extracts key insights, and sends a structured briefing to a Feishu group.

// ReAct Agent core config
{
  "agentType": "reActAgent",
  "model": "gpt-4o",
  "systemMessage": "You are a professional industry researcher. For the given topic:\n1. Search for the latest news (last 24 hours)\n2. Read the top 3-5 most relevant articles\n3. Extract 3-5 key insights\n4. Output a structured Markdown briefing\nDo not fabricate information. Every claim must come from actual search results.",
  "maxIterations": 15,
  "returnIntermediateSteps": true
}

Workflow: Schedule (0 8 * * 1-5) → Set (topic list) → Loop Over Items → ReAct Agent (with web_search, fetch_webpage, summarize_text tools) → Code node (merge all topic briefings) → Feishu Bot (send to group).

15.7 Multi-Agent Collaboration

For complex tasks, a single agent may be insufficient. n8n supports multi-agent architectures via the Execute Workflow Tool — an Orchestrator Agent decomposes the task and delegates to specialist agents, then aggregates results.

Typical architecture:

Cost tip: Use GPT-4o for the Orchestrator (planning requires high intelligence) and GPT-4o-mini or Claude 3 Haiku for specialist agents (execution is cheaper). This "brain + hands" architecture typically cuts total API costs by 60%+.

Rate this chapter
4.8  / 5  (16 ratings)

💬 Comments