Build AI Agents with n8n
Beyond workflows: Enter the agentic era. Agentic systems combine workflows
Agentic systems are environments composed of agents and workflows. Understanding the difference is crucial:
- Workflows: These are automations or systems where the output is predefined. The path the automation takes and the final result are determined during the building process. Think of a post-purchase email automation – the trigger (purchase) leads to a defined action (sending an order confirmation email).
- Agents: These are automations or systems where a large language model (LLM) dynamically decides which tools to use and what output is necessary based on the input. An example is a customer support agent that can access tools like creating support tickets, fetching order history, or issuing refunds. The agent chooses the appropriate tool based on the user’s request, leading to a dynamic output path.
Visually:
* A workflow follows a linear path: Input -> Automation Steps -> Consistent Output Location.
* An agent involves decision-making: Input -> LLM -> Chooses Tool(s) -> Dynamic Output.
Agents can even be nested, allowing one agent to call another agent that has its own specific set of tools.
Getting Started with n8n
n8n is a platform for building workflows and agentic systems. The interface includes:
- Workflows: Your automation blueprints.
- Credentials: Secure storage for API keys and account access information needed for your automations.
- Executions: Records of when your workflows have run, either manually during testing or automatically.
- Projects: Folders to organize your workflows.
To create a new workflow, use the “Create a workflow” button, either from the main dashboard or within a specific project.
Understanding n8n Node Types
Nodes are the building blocks of your n8n workflows. They can be broadly categorized into five types:
- Triggers: These nodes start your automation. They define when, where, and how the process begins (e.g., on a schedule, when a web chat message is received, when an event occurs in an app like Google Drive).
- Action Nodes: These allow interaction with external apps and services (Google Sheets, Airtable, Notion, Gmail, etc.). They perform actions like creating records, sending messages, or retrieving data.
- Utility Nodes: Native n8n nodes for data manipulation, such as
IF
statements, filters, data format conversions, or temporary data storage. - Code Nodes: Allow running custom code snippets (e.g., JavaScript), making HTTP requests, or setting up webhooks for advanced customization.
- Advanced AI Agent Node: This node transforms a standard workflow into an agent, enabling autonomous decision-making, memory, tool usage, and interaction with LLMs.
Building a Basic AI Agent in n8n
- Create a Trigger: Start by adding a trigger node. For interactive testing, the “On Trigger: Chat or form” node is useful. You can configure it to be publicly available via a test URL. This adds a chat interface to the n8n canvas.
- Add the AI Agent Node: Search for and add the “Advanced AI Agent” node. This acts as the “brain” of your agent. By default, it can take input from the preceding trigger node (like the chat input).
- Add a Chat Model: The AI Agent needs an LLM to function. Connect a “Chat Model” node (e.g., “OpenAI Chat Model”).
- Select the desired model (e.g., GPT-4o).
- Connect your credentials. For OpenAI, you’ll need an API key. You can generate one from your OpenAI account settings (https://platform.openai.com/api-keys). Create a new secret key, copy it, and add it as a new credential in the n8n chat model node.
- Basic Testing: At this stage, you can interact with the agent via the chat trigger. It will function like a standard chatbot, responding based on the LLM’s knowledge but without memory of past interactions or access to specific tools.
Adding Memory
To enable conversational context, add memory to the agent:
- Click the memory “+” icon on the AI Agent node.
- Select “Window Buffer Memory”. This stores recent interactions temporarily within n8n.
- Configure the
Context Window Length
(how many past interactions to remember). - Ensure the
Session Key
is mapped correctly (often automatically done if using the chat trigger) so the agent remembers context for the specific chat session.
With memory added, the agent can understand follow-up questions and maintain context within the conversation.
Adding Tools
Tools give the agent capabilities beyond simple chat.
- Add a Tool: Click the “+” button under “Tool” on the AI Agent node.
- Select Tool Type: Choose a tool. Examples include calling other n8n workflows, code execution, vector stores, or specific application integrations (Airtable, Gmail, Google Calendar, Wikipedia, etc.).
- Configure the Tool:
- (Example: Airtable Search)
- Select the Airtable node as the tool.
- Credentials: Connect your Airtable account. You might need to create a Personal Access Token in Airtable’s Developer Hub (https://airtable.com/create/tokens), granting necessary scopes (e.g.,
data.records:read
,data.records:write
,schema.bases:read
) and access to the specific base(s) you want the agent to use. Paste the token into the n8n credential setup. - Tool Description: Crucially, provide a clear, specific description for the AI Agent (e.g., “Searches the Home Inventory database in Airtable for item details”). This tells the agent when and why to use this tool.
- Operation: Select the action (e.g., “Search”).
- Resource: Specify “Record”.
- Base/Table: Select the specific Airtable base and table the tool should interact with.
- Return Data: Configure which fields to return.
- (Example: Airtable Search)
Testing Agent with Tools
Once a tool is configured, the agent can dynamically decide to use it. If you ask a question related to the tool’s description (e.g., “Is anything out of stock in my house?” when an Airtable tool connected to a home inventory exists), the agent should execute the tool to find the answer.
Adding More Tools (Example: Airtable Update)
You can add multiple tools, even for the same service but different functions.
- Add another Airtable tool.
- Tool Description: Describe the specific action (e.g., “Updates the quantity of items in the Home Inventory Airtable database”).
- Operation: Set to “Update”.
- Base/Table: Select the relevant base and table.
- Map Fields using
fromAI
: For fields that need dynamic input based on the conversation (like the record ID to update and the new quantity), use n8n expressions.- Record ID: Set the
ID
field to be anExpression
. Use the{{ fromAI(...) }}
helper. Inside the parentheses, define parameters likekey
,description
,type
, separated by commas and enclosed in quotes.- Example:
{{ fromAI('recordId', 'The Airtable record ID of the item needing updated', 'string') }}
- Example:
- Field to Update (e.g., Quantity): Add the field you want to update (e.g., “Current Quantity”). Set it as an
Expression
.- Example:
{{ fromAI('newQuantity', 'The new quantity of the item being updated', 'number') }}
- Note: Specify the
type
as'number'
if the field expects a numerical value.
- Example:
- Record ID: Set the
The fromAI
expression instructs the agent to extract the necessary information (like the specific item’s record ID and the new quantity mentioned in the chat) and populate the fields dynamically when executing the tool.
Testing Tool Chaining
When multiple tools exist, the agent might use them sequentially. For instance, when asked to “update the inventory for toothpaste,” the agent might first use the “Search” tool to find the correct record ID for toothpaste and then use the “Update” tool with that ID and the new quantity. While this can work automatically, adding specific instructions within the AI Agent node’s main prompt can improve consistency and control the process flow.
Sub-Agents (Calling Workflows as Tools)
A powerful concept is creating specialized agents (workflows like the inventory manager just built) and then having a primary agent call these specialized workflows as tools.
- Modify the trigger of the specialized workflow (e.g., the inventory manager) to be “When called by another workflow”.
- In your main agent workflow, add a tool of type “n8n workflow”.
- Configure this tool to call the specialized workflow.
This allows you to build modular, focused agents for specific tasks (inventory, scheduling, research) and orchestrate them using a higher-level agent, creating a more complex and capable agentic ecosystem.
To learn more advanced techniques for building AI agents and automations with n8n, explore resources like the AI Foundations courses (https://www.skool.com/ai-foundations/about) or visit https://aifoundations.io.