Building autonomous AI agents that can make decisions, use tools, and maintain context across conversations presents significant challenges for full-stack developers. You need to orchestrate multiple AI models, manage state persistence, handle API integrations, and ensure your agents can scale under production load. Traditional approaches require extensive backend infrastructure and complex state management logic that pulls focus from shipping features.
This guide walks through the technical implementation of AI agents in n8n, from initial setup through production deployment. You'll configure LLM integrations, implement memory management for stateful conversations, connect external tools and APIs, and deploy scalable agent systems using queue-based architecture.
In brief:
- Build autonomous AI agents using n8n's LangChain-powered node system with support for OpenAI, Anthropic, and HuggingFace models
- Implement memory management and tool-calling capabilities for context-aware agents that can execute actions across your stack
- Deploy production-ready workflows using Docker Compose with PostgreSQL and Redis queue mode for horizontal scaling
- Integrate AI agents with your headless CMS through webhooks and REST API connections
Prerequisites
Before building AI agents with n8n, you need:
- Node.js 20 or later (for npm installation) or Docker installed
- PostgreSQL 11.0 or higher for production deployments
- API keys from your chosen LLM provider (OpenAI, Anthropic, or HuggingFace)
- Basic understanding of REST APIs and workflow automation concepts
- Familiarity with JavaScript for custom code nodes
Build with n8n's AI Agent Architecture
n8n implements AI agent functionality through a hierarchical node system built on the LangChain JavaScript framework. The architecture separates root nodes (cluster nodes) that define main agent logic from sub-nodes that provide specific capabilities like language model integration, memory management, and tool execution.
The platform supports four primary architectural patterns for AI workflows. Chained requests execute sequential AI model calls with intermediate processing, reducing API costs by 30-50% compared to monolithic agent calls through selective model invocation. Single agent with state maintains context throughout workflow execution using memory nodes, making it ideal for conversational interfaces.
Multi-agent with gatekeeper implements centralized control with specialized agent delegation in a modular and scalable architecture. Multi-agent teams enable parallel specialized agents that collaborate on complex tasks with distributed decision-making.
At the core, every AI agent workflow in n8n requires four key components that work together as a hierarchical system. Trigger nodes (such as Webhook, Scheduler, or HTTP request nodes) initiate execution.
The AI Agent node serves as the orchestration layer, using LangChain-powered reasoning to make decisions and determine which tools to use based on user input and available capabilities.
Sub-nodes connect to the agent and provide critical capabilities: a language model integration node (for LLM access), memory nodes for maintaining context across interactions, and tool nodes that provide external APIs and functions the agent can invoke. Output nodes process the agent's responses and route them to downstream systems or user interfaces.
The workflow engine executes nodes sequentially or in parallel based on your configuration, with data flowing as JSON objects from node outputs to node inputs. This design lets you compose complex agent behaviors by connecting pre-built nodes rather than implementing orchestration logic from scratch.
1. Install and Configure n8n
The fastest path to a production-ready n8n instance uses Docker Compose with PostgreSQL. Create a project directory and a .env file with your configuration:
DOMAIN_NAME=yourdomain.com
SUBDOMAIN=n8n
GENERIC_TIMEZONE=America/Los_Angeles
POSTGRES_PASSWORD=your-secure-password
N8N_ENCRYPTION_KEY=your-32-character-keyCreate a docker-compose.yml file with PostgreSQL and n8n services:
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:2.1.4
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_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
- N8N_RUNNERS_ENABLED=true
- GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
- TZ=${GENERIC_TIMEZONE}
- NODE_ENV=production
volumes:
- n8n-data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
volumes:
postgres-data:
n8n-data:Start your services:
docker-compose up -dAccess the web interface at http://localhost:5678 and complete the initial setup wizard. The installation uses version 2.1.4, released December 23, 2025, which includes security hardening and enhanced credential encryption.
The N8N_RUNNERS_ENABLED parameter activates task runners for improved workflow execution, while N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS enforces secure file permissions required for production deployments.
2. Create Your First AI Agent Workflow
Building your first agent starts with the Chat Trigger node, which captures user input and initializes conversation sessions. Add a Chat Trigger to your workflow canvas. This node outputs a JSON object containing chatInput (the user's message) and sessionId (a unique identifier for conversation context).
Add an AI Agent node to handle orchestration and decision-making. Configure the system prompt to define your agent's behavior:
{
"parameters": {
"promptType": "define",
"text": "You are a helpful assistant that can answer questions and take actions using available tools. When users ask about content management, reference relevant features and capabilities.",
"hasOutputParser": false
}
}Connect the Chat Trigger to the AI Agent through the main connection. The agent receives input from the trigger and processes it using the connected language model and tools. This separation between trigger, orchestration, and language model provides flexibility to swap models or add capabilities without rebuilding your entire workflow.
You can test the basic structure before adding language model integration by creating a simple response workflow. The Chat Trigger passes user input to the AI Agent, which requires an LLM sub-node to generate actual responses.
This modular architecture mirrors how you might structure a modern web application with separated concerns between routing, business logic, and data access layers.
3. Integrate LLM Providers
Language model integration with n8n's AI Agent nodes occurs through specialized Chat Model sub-nodes that connect via the ai_languageModel connection type. The platform supports a comprehensive range of LLM providers including OpenAI (GPT-4, GPT-3.5-turbo), Anthropic Claude (Claude 3 family), HuggingFace inference models, Google Gemini, Cohere, and local models via Ollama, with each provider accessible through dedicated integration nodes.
For OpenAI integration, add an OpenAI Chat Model node to your workflow. Configure your API credentials in n8n's credential system by navigating to Settings and adding an OpenAI API credential with your key from platform.openai.com. Return to your workflow and configure the model parameters:
// OpenAI Chat Model Configuration
{
"name": "OpenAI Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"parameters": {
"model": "gpt-4",
"options": {
"temperature": 0.7,
"maxTokens": 1000,
"frequencyPenalty": 0.0,
"presencePenalty": 0.0
}
},
"credentials": {
"openAiApi": {
"name": "OpenAI Account"
}
}
}Connect the OpenAI Chat Model to the AI Agent through the language model connection. The temperature parameter (0.0 to 2.0) controls response randomness, with lower values producing more deterministic outputs and higher values increasing creativity.
The maxTokens parameter limits response length to control API costs and processing time. Additional configuration parameters include topP for nucleus sampling, frequencyPenalty and presencePenalty for controlling token repetition and topic diversity respectively.
For Anthropic Claude integration, add an Anthropic Chat Model node with similar configuration. Claude models use a temperature range of 0.0 to 1.0 and require a maxTokens parameter. The model selection accepts identifiers like "claude-3-opus-20240229" or "claude-3-sonnet-20240229" depending on your performance requirements.
The AI Agent automatically handles the communication protocol with your chosen language model, sending conversation history and tool information according to each provider's API specification. You can swap between providers by disconnecting one Chat Model node and connecting another without modifying the agent configuration. This abstraction proves valuable when you need to compare model performance or optimize costs across different tasks.
4. Implement Memory and State Management
AI agents in n8n can be implemented with or without memory management. Agents without memory sub-nodes (such as Window Buffer Memory or Buffer Memory nodes) operate without conversation history, making them unsuitable for conversational interfaces or workflows requiring historical awareness.
Memory nodes solve this by connecting to AI Agent nodes as sub-nodes and storing conversation history through configurations like context windowing and session management, making them available to the agent during processing.
Add a Window Buffer Memory node to your workflow and configure it with session management:
// Window Buffer Memory Configuration
{
"name": "Window Buffer Memory",
"type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
"parameters": {
"sessionKey": "={{ $json.sessionId }}",
"contextWindowLength": 10
}
}Connect the memory node to the AI Agent through the ai_memory connection type. The sessionKey parameter uses the session identifier from the Chat Trigger to associate memory with specific users or conversation threads. The contextWindowLength parameter is configurable to maintain a set number of messages in conversation history (for example, 10 messages), balancing context retention with token usage optimization.
The agent automatically includes relevant conversation history in its prompts to the language model, enabling it to reference previous exchanges and maintain coherent multi-turn conversations.
This pattern works for chatbot implementations that need to remember user preferences, ongoing tasks, or conversation context across sessions, as supported through n8n's memory management system including Window Buffer Memory and Chat Memory Manager nodes for maintaining conversation state.
For production deployments requiring long-term memory persistence, integrate vector database nodes like Qdrant or Pinecone. These enable retrieval-augmented generation patterns where agents can search through large knowledge bases and retrieve relevant information based on semantic similarity. Configure a Simple Vector Store node in retrieve mode and connect it to your AI Agent as a memory source for document-based context retrieval.
5. Connect Tools and External APIs
Tools extend agent capabilities beyond text generation by enabling actions like API calls, database queries, and custom logic execution. Tool nodes connect to the AI Agent through the ai_tool connection type, and the agent autonomously decides when to invoke them based on tool descriptions and user requests.
Built-in tool nodes include the Calculator Tool, Code Tool, HTTP Request Tool, Workflow Tool for calling other n8n workflows, SerpAPI Tool for web search, and Wikipedia Tool for knowledge retrieval, while app integration tools provide access to services like Google Drive, Gmail, Notion, and Slack.
Add an HTTP Request Tool to your workflow for external API integration:
// HTTP Request Tool Configuration for CMS Content Retrieval
{
"name": "Get Content Tool",
"type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
"parameters": {
"name": "get_content",
"description": "Retrieve content from the CMS API. Input should be a content type slug like 'articles' or 'pages'. Returns populated content entries with all related fields.",
"method": "GET",
"url": "={{ $env.CMS_API_URL }}/api/{{ $json.contentType }}",
"authentication": "genericCredentialType",
"sendQuery": true,
"queryParameters": {
"parameters": [
{
"name": "populate",
"value": "*"
},
{
"name": "pagination[pageSize]",
"value": "{{ $json.limit || 10 }}"
}
]
},
"options": {
"timeout": 30000,
"retry": {
"maxTries": 3,
"waitBetweenTries": 1000
}
}
}
}Technical Configuration Notes:
This HTTP Request Tool integrates with CMS APIs (such as Strapi) as a callable function within AI Agent workflows. The tool configuration enables:
- Dynamic endpoint construction using the
contentTypeparameter passed by the AI agent - Wildcard population (
populate: "*") to retrieve all related fields and nested content - Pagination support with configurable limits for large result sets
- Retry logic with exponential backoff (3 attempts, 1-second wait) for handling transient API failures
- 30-second timeout to prevent indefinite waits on slow API responses
Integration Pattern:
This tool connects to an AI Agent node through the ai_tool connection type, allowing autonomous agents to dynamically retrieve content based on user queries. The agent's natural language understanding determines when to invoke this tool, using the description to understand its purpose and required inputs.
Production Recommendations:
For self-hosted instances, external modules can be used by setting NODE_FUNCTION_ALLOW_EXTERNAL=true. For n8n Cloud deployments, ensure credentials are stored using the platform's credential management system rather than hardcoded in workflow definitions. The timeout and retry parameters are critical for production reliability when calling external CMS APIs that may experience temporary latency or failures.
The name and description parameters are critical because the AI Agent uses them to determine when to invoke the tool. Write descriptions that clearly specify what the tool does, what input format it expects, and the expected output format. The agent analyzes user requests, compares them against available tool descriptions, and selects appropriate tools to accomplish the task.
Add a Calculator Tool for mathematical operations:
// Calculator Tool Configuration
{
"name": "Calculator Tool",
"description": "Performs basic mathematical calculations. Input should be a math expression like '2 + 2' or '15 * 3'."
}This tool can be integrated into AI Agent workflows as a sub-node tool to enable autonomous mathematical calculations during agent decision-making processes.
For custom business logic in AI agent workflows, use Code Tool nodes or Code nodes that execute JavaScript or Python:
JavaScript (Recommended):
- Full ES6+ syntax support with async/await
- Complete access to n8n's built-in methods (
$input.all(),$json,$binary) - Best performance for custom AI integrations
- Direct API calls to external AI services
- Example: Direct OpenAI integration, data transformation for agent responses
Python (For Specialized Libraries):
- Native Python execution (stable since v1.111.0) recommended over Pyodide
- Underscore prefix syntax (
_input.all(),_json) - Limited access to n8n's built-in methods compared to JavaScript
- Self-hosted instances can import external packages via Docker image extension
- Best for machine learning operations and data science workflows
Both execution modes run in a dedicated sandbox environment with security restrictions preventing unauthorized file system or network access. Use "Run Once for All Items" mode for batch processing or "Run Once for Each Item" mode for individual agent task processing. Execution history, debugging tools, and error logging via the Debug Helper node enable comprehensive development and troubleshooting of custom AI workflow logic.
// Code Tool Implementation
const contentTypes = $input.all();
return contentTypes.map(item => ({
json: {
slug: item.json.attributes.slug,
title: item.json.attributes.title,
publishedAt: item.json.attributes.publishedAt,
formatted: `${item.json.attributes.title} (${item.json.attributes.slug})`
}
}));In n8n, AI agents can autonomously chain multiple tool calls together to accomplish complex tasks through the Tools Agent node architecture. When a user asks "What articles were published this month?", the n8n AI Agent node—powered by LangChain—might first invoke an HTTP Request tool to query a database, then use a Code tool to filter results by date, and finally format the output through a downstream processing node.
This autonomous tool selection and chaining, orchestrated through n8n's sub-node tool connections and agent reasoning capabilities, distinguishes AI agents built in n8n from simple API wrappers or chatbots.
Your agents can integrate with content management workflows by connecting tools that query, create, or update content through your CMS API. Configure authentication using n8n's credential system and reference stored tokens or API keys using environment variables.
6. Deploy and Scale Your AI Agent
Production AI agent deployments require queue mode architecture for horizontal scaling. Queue mode separates the main instance that handles webhooks and UI from worker instances that execute workflows, enabling you to scale execution capacity by adding workers.
Add Redis to your Docker Compose configuration:
services:
redis:
image: redis:7-alpine
restart: unless-stopped
volumes:
- redis-data:/data
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 10s
timeout: 5s
retries: 5
volumes:
redis-data:Configure the main instance and worker services:
n8n-main:
image: docker.n8n.io/n8nio/n8n:2.1.4
restart: unless-stopped
ports:
- "127.0.0.1:5678:5678"
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=${DB_PASSWORD}
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
- N8N_HOST=${N8N_HOST}
- N8N_PROTOCOL=https
- WEBHOOK_URL=https://${N8N_HOST}/
- EXECUTIONS_MODE=queue
- QUEUE_BULL_REDIS_HOST=redis
- QUEUE_BULL_REDIS_PORT=6379
- QUEUE_HEALTH_CHECK_ACTIVE=true
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
- N8N_RUNNERS_ENABLED=true
depends_on:
- redis
- postgres
volumes:
- n8n_data:/home/node/.n8n
n8n-worker:
image: docker.n8n.io/n8nio/n8n:2.1.4
command: worker
environment:
- EXECUTIONS_MODE=queue
- QUEUE_BULL_REDIS_HOST=redis
- QUEUE_BULL_REDIS_PORT=6379
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=${DB_POSTGRESDB_PASSWORD}
- N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
- GENERIC_TIMEZONE=America/Los_Angeles
- TZ=America/Los_Angeles
deploy:
replicas: 3
depends_on:
- redis
- postgres
volumes:
redis-data:
n8n_data:This configuration creates one main instance and three workers. The main instance receives webhook triggers and API requests, adds workflow executions to the Redis queue, and workers pull tasks for execution.
Scale horizontally by adjusting the replicas parameter based on load. Queue mode architecture achieves significantly higher throughput than single-instance deployments—a single instance in standard mode handles up to 100 concurrent workflow executions, while queue mode with three workers on a C5.large instance processes approximately 72 requests per second with less than 3-second latency and zero failures under load.
Monitor your deployment using n8n's built-in health check endpoints. Configure Docker health checks to ensure service availability.
For production deployments requiring high availability, deploy n8n on Kubernetes using managed services like AWS EKS, Google Kubernetes Engine, or Azure Kubernetes Service. The same Docker images work across platforms, and you can use Helm charts or custom manifests to define your deployment topology.
Implement proper SSL/TLS termination using a reverse proxy like Nginx or Caddy. Bind n8n to localhost (127.0.0.1:5678) for security, and configure your reverse proxy to listen on ports 80 and 443. With Caddy, the reverse proxy automatically provisions and renews SSL certificates through Let's Encrypt—simply configure your domain in the Caddyfile and Caddy handles certificate provisioning without manual intervention.
For Nginx, integrate with Let's Encrypt using certbot or similar tools. First configure your domain DNS to point to the reverse proxy (not directly to n8n), wait for DNS propagation to complete, then enable SSL/TLS configuration on the reverse proxy. The reverse proxy will handle all certificate management and HTTPS traffic, while n8n communicates securely with it over the local network.
Secure your deployment by restricting network access to internal services. Bind PostgreSQL and Redis to internal networks only, exposing only the n8n web interface through your reverse proxy. Use n8n's credential management system for storing API keys and sensitive configuration rather than environment variables, and enable role-based access control through n8n's user management system introduced in version 2.0.
Connect your AI agents to Strapi CMS through webhooks configured in Strapi's settings to trigger automated workflows on content events like entry creation, updates, or publication. The n8n Strapi node supports Create and Update operations, allowing you to build workflows that process content with AI models and update your CMS accordingly.
For example, you could configure an n8n workflow triggered by Strapi's entry.create webhook event to pass the content through an OpenAI or Anthropic language model for processing, then use the Strapi node's Update operation to store AI-generated enhancements back to the original entry.
Configure API Token authentication in both systems to prevent unauthorized workflow triggers, using Strapi's granular API token permissions to limit n8n integration access to only necessary content types.
Your deployed AI agents can integrate with API-driven architectures through n8n's comprehensive REST API and HTTP Request nodes, enabling direct communication with backend services and external APIs. This capability allows agents to participate in broader application workflows by responding to events from frontend applications, mobile apps, or other backend services.
When combined with n8n's 500+ pre-built integrations and queue-based execution model, agents can reliably orchestrate complex multi-service workflows while maintaining the scalability and reliability benefits of the queue-based architecture, which supports up to 220 workflow executions per second on a single instance.
Next Steps
Building AI agents with n8n gives you production-ready workflow automation with LangChain integration, enabling autonomous decision-making systems that scale horizontally through queue-based architecture. Your agents can orchestrate multiple language models, maintain conversation context through memory systems, and execute actions across your stack through tool integrations.
To extend your implementation, explore n8n's workflow templates for pre-built agent patterns, or integrate your agents with Strapi's plugin marketplace for additional capabilities. Start by deploying the Docker Compose configuration from this guide, then examine the self-hosted AI starter kit to extend your agent capabilities with local LLM hosting and vector databases.
For production optimization, review n8n's official scaling documentation and consider implementing monitoring solutions like Prometheus for workflow observability. Test your agent's performance under production load using tools like Apache Benchmark or k6 to validate your deployment architecture. Join the Strapi community to share your AI agent implementations and discover advanced integration patterns from other developers building autonomous systems.
Get Started in Minutes
npx create-strapi-app@latest in your terminal and follow our Quick Start Guide to build your first Strapi project.