How to Build an AI Agent in n8n
with LangChain 2026

n8n's native AI Agent nodes, powered by the LangChain framework, let you build autonomous AI systems that reason through problems, use tools, and complete multi-step tasks β€” all without writing a single line of Python. Whether you want a customer support agent that queries your CRM, a research agent that searches the web, or a coding assistant that runs code, n8n's visual approach makes it surprisingly accessible. This guide covers every layer: models, memory, tools, and a complete real-world agent example.

What You'll Build

By the end of this guide you'll have a functional Customer Support AI Agent that receives messages via webhook, checks order status via HTTP Request tool, searches a knowledge base, drafts professional replies, and remembers conversation context across multiple turns.

AI Agents vs. Simple Chains: The Key Difference

Before building, it's worth understanding what makes an agent different from a regular LLM node or a simple chain:

ConceptSimple LLM CallChainAI Agent
Decision makingNoneFixed stepsDynamic reasoning
Tool useNonePredefinedChooses tools autonomously
MemoryNoneOptionalBuilt-in with context
IterationSingle callSingle passLoops until goal met
ComplexityLowMediumHigh
Cost per runLowMediumHigher (multiple LLM calls)

An agent uses a ReAct loop (Reason + Act): the LLM thinks about what to do, calls a tool, observes the result, reasons again, and repeats until it reaches a final answer. This makes agents much more capable than simple chains for open-ended tasks.

LangChain Nodes Overview

n8n exposes LangChain through a set of specialized node categories. Understanding the ecosystem before you build saves time:

AI Agent
@n8n/n8n-nodes-langchain.agent
The orchestrator. Connects to a chat model, memory, and tools. Runs the ReAct reasoning loop.
Chat OpenAI
@n8n/n8n-nodes-langchain.lmChatOpenAi
Connects to GPT-4o, GPT-4-turbo, GPT-3.5-turbo. Set temperature, max tokens, and model.
Chat Anthropic
@n8n/n8n-nodes-langchain.lmChatAnthropic
Claude 3.5 Sonnet, Claude 3 Opus. Excellent for long contexts and nuanced reasoning.
Chat Google Gemini
@n8n/n8n-nodes-langchain.lmChatGoogleGemini
Gemini 1.5 Pro and Flash. Best for multimodal tasks and Google Workspace integrations.
Buffer Window Memory
@n8n/n8n-nodes-langchain.memoryBufferWindow
Keeps the last N conversation turns in memory. Cheapest memory option for multi-turn chat.
Postgres Chat Memory
@n8n/n8n-nodes-langchain.memoryPostgresChat
Persists conversation history in PostgreSQL. Survives restarts. Needed for production agents.
HTTP Request Tool
@n8n/n8n-nodes-langchain.toolHttpRequest
Gives the agent the ability to call any REST API. Describe the API and let the agent decide when to use it.
Code Tool
@n8n/n8n-nodes-langchain.toolCode
Run JavaScript or Python code dynamically. The agent writes and executes code to process data.
Wikipedia Tool
@n8n/n8n-nodes-langchain.toolWikipedia
One-click tool to search Wikipedia. No API key required. Great for factual research agents.
Vector Store Tool
@n8n/n8n-nodes-langchain.toolVectorStore
Semantic search over your documents. Connect Pinecone, Supabase, Qdrant, or Postgres PGVector.

Setting Up the AI Agent Node

Create a new workflow and add an AI Agent node. This is your orchestration center β€” everything else connects to it as sub-nodes.

Agent type selection

The AI Agent node offers several agent types. For most use cases, choose Tools Agent β€” it uses the modern function-calling API supported by GPT-4, Claude 3, and Gemini 1.5, which is more reliable and faster than the older ReAct text-parsing approach.

Agent TypeBest ForRequired Model Feature
Tools AgentMost use cases, production agentsFunction calling / tool use
ReAct AgentModels without function callingGood instruction following
Plan and ExecuteComplex multi-step tasksStrong reasoning
SQL AgentNatural language to SQL queriesFunction calling
Conversational AgentChatbots with memoryFunction calling

System message (prompt)

The system message defines your agent's persona, capabilities, and constraints. This is the most impactful configuration you control:

You are a helpful customer support agent for Acme Corp, an e-commerce store. Your capabilities: - Look up order status using the "Check Order Status" tool (requires order ID) - Search the knowledge base for product information and policies - Draft professional, empathetic responses Rules: - Always greet the customer by name if available - Never make up order details β€” always use the tool to check - If you cannot resolve an issue, escalate: "I'll connect you with a specialist" - Keep responses under 150 words unless explaining a complex policy - Today's date: {{ $now.toISODate() }}
Prompt Engineering Tip

Use negative constraints ("Never make up...") alongside positive instructions. LLMs respond well to clear boundaries. Also include dynamic context like the current date via n8n expressions β€” the agent uses this for time-sensitive reasoning.

Connecting Language Models

The Chat Model sub-node provides the intelligence. Connect it to the AI Agent's "Chat Model" input. Here's how to configure each major provider:

OpenAI GPT-4o (recommended for most cases)

Node: Chat OpenAI Model: gpt-4o Temperature: 0.3 # Lower = more deterministic, better for factual tasks Max Tokens: 1000 Timeout: 60000 # ms β€” increase for complex reasoning tasks # Credential: OpenAI API key # Cost: ~$5 per 1M input tokens, ~$15 per 1M output tokens

Anthropic Claude 3.5 Sonnet (best for nuanced text)

Node: Chat Anthropic Model: claude-3-5-sonnet-20241022 Temperature: 0.2 Max Tokens: 2000 # Claude excels at: following complex instructions, long documents, # nuanced writing, and avoiding hallucinations on ambiguous queries

Google Gemini 1.5 Flash (best price/performance)

Node: Chat Google Gemini Model: gemini-1.5-flash Temperature: 0.4 # Gemini Flash is extremely fast and cheap β€” ideal for high-volume agents # Gemini 1.5 Pro has a 1M token context window for very long documents

Adding Memory to Your Agent

Without memory, every message starts a fresh conversation β€” the agent forgets everything said before. Memory nodes solve this by maintaining conversation history.

Buffer Window Memory (in-memory)

This stores the last N conversation turns in RAM. Simple to set up but not persistent across n8n restarts:

Node: Buffer Window Memory Session ID: ={{ $json.sessionId }} # Unique per user/conversation Context Window Length: 10 # Keep last 10 messages

The Session ID is critical β€” it isolates memory between different users. Pass a unique identifier from your trigger (user ID, conversation ID, email) and make sure the same ID is passed on every turn of the conversation.

PostgreSQL Chat Memory (production)

For production agents that must survive restarts and serve multiple users reliably:

Node: Postgres Chat Memory Credential: your-postgres-connection Session ID: ={{ $json.userId }} Table Name: n8n_chat_histories # Auto-created on first use Context Window Length: 20 # To clear a session's memory (e.g., after ticket resolved): # DELETE FROM n8n_chat_histories WHERE session_id = 'user-123';

Adding Tools: The Power of Agentic Workflows

Tools are what make agents genuinely useful. Each tool sub-node connects to the AI Agent's "Tools" input. The agent decides autonomously which tools to use and when, based on the user's message and its reasoning.

Tool 1: HTTP Request Tool (call your APIs)

Node: HTTP Request Tool Name: Check Order Status Description: Look up the status of a customer order. Use when the customer mentions an order ID or asks about delivery. Input: order_id (string, e.g. "ORD-12345") Method: GET URL: https://api.yourstore.com/orders/{order_id} Authentication: Header Auth (Bearer token) # The description is what the LLM reads to decide when to use this tool # Be specific about inputs β€” the agent extracts them from the conversation

Tool 2: Code Tool (dynamic computation)

The Code Tool lets the agent write and execute JavaScript to process data, do calculations, or transform responses:

Node: Code Tool Name: Calculate Refund Amount Description: Calculate the refund amount for a partial return. Input: original_price (number), items_returned (number), total_items (number) // Code (the agent provides the input values): const { original_price, items_returned, total_items } = query; const refund = (original_price / total_items) * items_returned; return { refund_amount: refund.toFixed(2), currency: 'USD' };

Tool 3: Wikipedia Tool (factual knowledge)

Node: Wikipedia # No configuration needed β€” drop it in and connect to the agent # The agent uses it automatically when it needs factual information # Works by searching Wikipedia and returning the article summary

Tool 4: Custom n8n Workflow Tool

You can call an entire n8n sub-workflow as a tool β€” this is powerful for complex operations:

Node: Call n8n Workflow Tool Name: Search Knowledge Base Description: Search the company knowledge base for product documentation, FAQs, and policies. Use when the customer asks about how something works or what our policies are. Input: search_query (string) Workflow: [Select your sub-workflow that does vector search]

Building a Real Agent: Customer Support Bot

Here is the complete workflow structure for a production customer support agent:

WORKFLOW STRUCTURE: 1. Webhook Trigger β”œβ”€β”€ Path: /support-chat β”œβ”€β”€ Method: POST └── Body: { "message": "...", "userId": "...", "sessionId": "..." } 2. Set Node (normalize input) └── chatInput: {{ $json.body.message }} sessionId: {{ $json.body.userId }}-{{ $json.body.sessionId }} 3. AI Agent Node β”œβ”€β”€ Agent Type: Tools Agent β”œβ”€β”€ Chat Input: {{ $json.chatInput }} β”œβ”€β”€ System Message: [see above] β”‚ β”œβ”€β”€ β†’ [Chat Model] Chat OpenAI (gpt-4o, temp: 0.3) β”œβ”€β”€ β†’ [Memory] Postgres Chat Memory (session: {{ $json.sessionId }}) β”œβ”€β”€ β†’ [Tool] HTTP Request - Check Order Status β”œβ”€β”€ β†’ [Tool] HTTP Request - Get Customer Profile β”œβ”€β”€ β†’ [Tool] Call Workflow - Search Knowledge Base └── β†’ [Tool] HTTP Request - Create Support Ticket 4. Respond to Webhook └── Body: { "reply": "{{ $json.output }}", "sessionId": "..." }

Handling the agent output

The AI Agent node outputs the agent's final response in $json.output. Add a Respond to Webhook node to return it to the caller:

Node: Respond to Webhook Response Code: 200 Response Body: { "reply": {{ $json.output }}, "sessionId": {{ $('Set Node').item.json.sessionId }}, "timestamp": {{ $now.toISO() }} }

Prompt Engineering Tips for n8n Agents

1. Describe tools precisely

The LLM reads tool descriptions to decide when and how to use them. Vague descriptions lead to poor tool selection. Be explicit about: what the tool does, when to use it, and exactly what inputs it expects.

2. Use few-shot examples in system prompts

Example interactions (add to system message): User: "Where is my order ORD-99234?" Thought: I should check the order status using the order ID provided. Tool: Check Order Status (order_id: "ORD-99234") Result: { status: "shipped", tracking: "1Z999AA10123456784" } Response: "Your order ORD-99234 has shipped! Track it here: 1Z999AA10123456784" User: "How do I return an item?" Thought: This is a policy question. I should search the knowledge base. Tool: Search Knowledge Base (query: "return policy")

3. Control reasoning with temperature

  • Temperature 0.0–0.2: Factual, consistent, best for data retrieval and structured outputs
  • Temperature 0.3–0.5: Balanced β€” good for most support agents
  • Temperature 0.7–1.0: Creative, varied β€” good for content generation but avoid for agents that need accuracy

4. Limit the number of iterations

Set "Max Iterations" on the AI Agent node to prevent infinite loops. For simple support queries, 5 iterations is plenty. For research agents that might need many tool calls, use 10–15.

Cost Alert

Each tool call involves at least one additional LLM API call. An agent with 3 tools might make 4–6 API calls per user message. Monitor your API usage carefully in production β€” set spending limits on your OpenAI/Anthropic dashboard and use cheaper models (GPT-4o mini, Claude Haiku) for high-volume agents.

Debugging AI Agents in n8n

Agents are harder to debug than simple workflows because the reasoning happens inside the LLM. Use these techniques:

Enable verbose logging

# Add to your .env to see the full ReAct chain in n8n logs N8N_LOG_LEVEL=debug LANGCHAIN_VERBOSE=true

Use execution history

In the n8n editor, click on any past execution to see exactly which tools the agent called, what inputs it passed, and what it received back. This reveals misconfigurations instantly β€” for example, if the agent is calling the wrong tool or passing malformed parameters.

Test with pinned data

Pin a test message at your webhook trigger and run the agent manually. Change the system prompt, model, or tool descriptions iteratively until the agent behaves correctly. Only then activate the live webhook.

Frequently Asked Questions

Can I use local Ollama models instead of OpenAI?
Yes. n8n supports Ollama via the "Ollama Chat Model" node. However, most local models don't support function calling (required for Tools Agent). Use ReAct Agent type with Ollama, and test with models like llama3.1, mistral-nemo, or qwen2.5 which have reasonable instruction-following capability. Quality will be lower than GPT-4 for complex reasoning tasks.
How do I share state between tool calls within one agent run?
The agent naturally passes tool results back to the LLM context β€” so each subsequent tool call can reference previous results. For more complex state, use the Code Tool to build a state object and pass it explicitly in subsequent tool descriptions.
What's the difference between Buffer Memory and Postgres Memory?
Buffer Window Memory is in-RAM and disappears when n8n restarts. Postgres Chat Memory persists to a database. Use Buffer for prototyping or single-session agents, and Postgres for production multi-user chatbots that need to maintain conversation context across days.
Can agents call other agents?
Yes β€” this is called a multi-agent architecture. Use the "Call n8n Workflow" tool to have a supervisor agent delegate to specialist agents (e.g., a research sub-agent, a writing sub-agent). Each sub-workflow contains its own AI Agent node. This is powerful but expensive β€” each delegation adds multiple LLM calls.
How do I prevent the agent from hallucinating facts?
Ground the agent with tools β€” don't rely on the LLM's parametric knowledge for facts. Use a vector store or search tool for product info, an API for order data, and a structured database for customer records. Explicitly state in the system prompt: "Never invent information β€” always use the provided tools."