Feb 2026 • LangChain + n8n

n8n AI Agents

สร้าง AI Agents ด้วย LangChain แบบ Visual Workflow บน n8n

รองรับ Multi-Agent Systems, RAG, Memory Management, และ Production-Ready Deployment ด้วย Queue Mode

AI Agents
LangChain
Workflows
LLM

1. n8n AI Agents คืออะไร

n8n AI Agents เป็นฟีเจอร์สำหรับสร้าง autonomous AI agents บน n8n workflow automation platform โดยใช้ LangChain เป็น framework หลัก ช่วยให้คุณสามารถสร้าง agents ที่สามารถตัดสินใจ ใช้ tools ต่างๆ และรักษา context ในการสนทนาได้

จุดเด่นของ n8n AI Agents คือการใช้ Visual Workflow Builder ทำให้ไม่ต้องเขียน code มากนัก แต่ยังมีความยืดหยุ่นสูงด้วยการรองรับ custom code nodes

ทำไมต้องใช้ n8n AI Agents?

  • Self-hostable - รันบน server ของคุณเอง ควบคุม data ได้เต็มที่
  • 500+ Integrations - เชื่อมต่อกับ apps และ services ต่างๆ ได้มากมาย
  • LangChain Native - รองรับ LangChain components ครบถ้วน
  • Visual Builder - Drag-and-drop สร้าง workflow ได้ง่าย
  • Production Ready - รองรับ Queue Mode สำหรับ horizontal scaling

Key Feature: n8n AI Agents แยกแยะระหว่าง "Agent Node" (orchestration) และ "Sub-nodes" (LLM, Memory, Tools) ทำให้ swap components ได้ง่าย!

2. n8n AI Agent Architecture

n8n ใช้ hierarchical node system ที่แยก root nodes (cluster nodes) จาก sub-nodes ที่ให้ capabilities เฉพาะทาง:

Chat Trigger User Input AI Agent Node Orchestration Layer LLM (OpenAI/Anthropic) Memory (Buffer/Vector) Tools (HTTP/Code/API) Actions • Send Email • Update Database • Call API • Create Ticket • Generate Report Powered by LangChain

Root Nodes

AI Agent node ทำหน้าที่เป็น orchestration layer รับ input และตัดสินใจ

Sub-nodes

เชื่อมต่อกับ Agent เพื่อให้ capabilities: LLM, Memory, Tools

3. การติดตั้ง n8n ด้วย Docker

1 Docker Compose สำหรับ Production

สร้างไฟล์ .env และ docker-compose.yml:

.env
DOMAIN_NAME=yourdomain.com
SUBDOMAIN=n8n
GENERIC_TIMEZONE=Asia/Bangkok
POSTGRES_PASSWORD=your-secure-password
N8N_ENCRYPTION_KEY=your-32-character-encryption-key
docker-compose.yml
version: '3.8'

services:
  postgres:
    image: postgres:15
    restart: unless-stopped
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=n8n
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U n8n']
      interval: 10s
      timeout: 5s
      retries: 5

  n8n:
    image: docker.n8n.io/n8nio/n8n:latest
    restart: unless-stopped
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
      - N8N_HOST=${SUBDOMAIN}.${DOMAIN_NAME}
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - N8N_RUNNERS_ENABLED=true
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
      - TZ=${GENERIC_TIMEZONE}
    volumes:
      - n8n-data:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy

volumes:
  postgres-data:
  n8n-data:
Terminal
# Start n8n
docker compose up -d

# Check status
docker compose ps

# View logs
docker compose logs -f n8n

4. สร้าง AI Agent แรกของคุณ

4 ขั้นตอนสร้าง AI Agent

Step 1: Add Chat Trigger

เพิ่ม On Chat Message node เพื่อรับ user input

  • • output จะมี chatInput (user message)
  • • และ sessionId (conversation ID)

Step 2: Add AI Agent Node

เพิ่ม AI Agent node และกำหนด System Prompt:

System Prompt Example
คุณเป็นผู้ช่วย AI ที่ช่วยตอบคำถามและดำเนินการต่างๆ
เมื่อผู้ใช้ถามเกี่ยวกับข้อมูล ให้ใช้ tools ที่มีอยู่
ตอบเป็นภาษาไทยเสมอ

Step 3: Add LLM Sub-node

เชื่อมต่อ Language Model:

  • OpenAI Chat Model - GPT-4, GPT-3.5-turbo
  • Anthropic Chat Model - Claude 3 family
  • Google Gemini - Gemini Pro
  • Ollama - Local models

Step 4: Add Memory (Optional)

เพิ่ม Window Buffer Memory เพื่อให้ agent จำการสนทนาได้

Pro Tip: ใช้ ={{ $json.sessionId }} ใน sessionKey ของ Memory node เพื่อแยก conversation ตาม user

5. เชื่อมต่อ LLM Providers

OpenAI Configuration

ก่อนอื่นเพิ่ม API Credential ใน Settings → Credentials:

OpenAI Chat Model Config
{
  "model": "gpt-4",
  "options": {
    "temperature": 0.7,
    "maxTokens": 2000,
    "frequencyPenalty": 0.0,
    "presencePenalty": 0.0
  }
}

temperature: 0.7

ความสุ่มของคำตอบ (0-2)

maxTokens: 2000

ความยาวสูงสุดของคำตอบ

Anthropic Claude Configuration

Anthropic Chat Model Config
{
  "model": "claude-3-sonnet-20240229",
  "options": {
    "temperature": 0.7,
    "maxTokens": 4096
  }
}

Model Swapping: คุณสามารถสลับ LLM providers ได้โดย disconnect node เดิมและ connect node ใหม่ โดยไม่ต้องแก้ workflow!

6. Memory Management

Memory nodes ช่วยให้ AI Agent จำการสนทนาได้ ทำให้สามารถตอบโต้แบบ multi-turn conversation ได้อย่างมี context

Window Buffer Memory

Window Buffer Memory Config
{
  "sessionKey": "={{ $json.sessionId }}",
  "contextWindowLength": 10
}

contextWindowLength: 10 = เก็บ 10 ข้อความล่าสุดใน conversation history

Vector Store Memory (RAG)

สำหรับ long-term memory และ RAG applications:

  • Pinecone - Managed vector database
  • Qdrant - Self-hosted vector database
  • Simple Vector Store - In-memory (for testing)

7. Tools & External APIs

Tools ขยายความสามารถของ Agent ให้สามารถเรียก APIs, query databases, และ execute custom logic ได้

Built-in Tools

Calculator

Code Tool

HTTP Request

Web Search

HTTP Request Tool Example

HTTP Request Tool Config
{
  "name": "get_user_data",
  "description": "ดึงข้อมูลผู้ใช้จาก API ใช้เมื่อต้องการข้อมูล user",
  "method": "GET",
  "url": "https://api.example.com/users/{{ $json.userId }}",
  "options": {
    "timeout": 30000,
    "retry": {
      "maxTries": 3,
      "waitBetweenTries": 1000
    }
  }
}

Important: name และ description สำคัญมาก Agent ใช้ข้อมูลนี้ตัดสินใจว่าจะเรียก tool ไหน!

8. Use Cases ยอดนิยม

Multi-Agent Systems

ประสานงานหลาย specialized agents (research, writing, QA) สำหรับ complex workflows

RAG Chatbot

สร้าง chatbot ที่ตอบคำถามจาก company documents ด้วย vector database

Autonomous Crawler

AI agent ที่ค้นหาและ scrape ข้อมูลจาก web อัตโนมัติ

Customer Support

Automated support agent ที่ตอบคำถามและสร้าง tickets ได้

9. Production Deployment

Queue Mode Architecture

สำหรับ horizontal scaling ให้ใช้ Queue Mode ที่แยก main instance จาก workers:

docker-compose.yml - Queue Mode
services:
  redis:
    image: redis:7-alpine
    restart: unless-stopped
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 5

  n8n-main:
    image: docker.n8n.io/n8nio/n8n:latest
    environment:
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      # ... other config
    depends_on:
      - redis
      - postgres

  n8n-worker:
    image: docker.n8n.io/n8nio/n8n:latest
    command: worker
    environment:
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      # ... other config
    deploy:
      replicas: 3
    depends_on:
      - redis
      - postgres

Main Instance

รับ webhooks และ UI requests

Redis Queue

จัดการ job queue

Workers (3)

Execute workflows แบบ parallel

Performance: Queue mode สามารถประมวลผลได้ ~72 requests/second ด้วย 3 workers เทียบกับ ~100 concurrent executions ใน single instance mode

Best Practices

Security

ใช้ credential management ของ n8n ไม่ใช่ env vars สำหรับ API keys

Network Isolation

Bind PostgreSQL และ Redis ให้ internal networks เท่านั้น

Graceful Shutdown

ใช้ health checks และ proper signal handling

Cost Control

ใช้ conditions กรอง unnecessary data ก่อนส่งให้ LLM

สรุป

n8n AI Agents เป็น platform ที่ยอดเยี่ยมสำหรับสร้าง autonomous AI agents ด้วย visual workflow builder โดยมีจุดเด่น:

  • Self-hostable พร้อม data sovereignty
  • LangChain native รองรับ LLM providers หลายตัว
  • 500+ integrations สำหรับ tools และ APIs
  • Production-ready ด้วย Queue Mode
  • Open source พร้อม community support
Open Source LangChain Self-hosted

แชร์บทความนี้