CyberFreezeDev Loading
  1. Home
  2. Blog
  3. How-to guides
  4. How to build an n8n AI agent: a worked example

How-to guides

How to build an n8n AI agent: a worked example

An n8n AI agent is the AI Agent node with a chat model, tools and optional memory. Here is a worked example that triages support email into a CRM, with a person approving every write.

Two people at a desk with laptops, one marking notes on paper with a pencil
Two people at a desk with laptops, one marking notes on paper with a pencil

An n8n AI agent is the AI Agent node with a chat model, at least one tool and, for conversations, a memory sub-node. The model reads the input, decides which tools to call, reads their results and stops when it has an answer or hits its iteration limit. Below we build one that triages support email into a CRM, with a person approving anything it writes.

This guide is for teams that already run n8n and want an agent that does a real job. If you are still deciding whether you need an AI agent at all, our comparison of AI agents and workflow automation helps.

A man looking through sheets of paper in an office, with a colleague at a desk behind him
Triage is a good first agent: the input is text, the categories are known, and every write can wait for a person.

What are the parts of an n8n AI agent?

An agent in n8n is a root node with sub-nodes attached:

  • AI Agent node. Since n8n 1.82.0 every AI Agent node works as a Tools Agent. Version 1 of the node, which still has the agent type setting, will be removed in n8n 3.0, so update old workflows.
  • Chat model. The language model that decides what to do next.
  • Tools. At least one is required. Many app nodes, the HTTP Request node and other n8n workflows can be tools. Inside a tool, $fromAI("email", "the sender's address") lets the model fill that field.
  • Memory. Optional. Needed only when the agent must remember earlier messages in a conversation.
  • Output parser. Turn on Require Specific Output Format and attach a Structured Output Parser when the next node expects fixed fields.

A worked example: support email into a CRM

The goal: every email to a support label is sorted, matched to a contact, logged in the CRM and given a draft reply. Nothing is sent or written without a person.

  1. Gmail Trigger. Event Message Received, filtered by Label Names or IDs so it only sees the support label. It polls, and takes 10 emails per poll by default (up to 50).
  2. Text Classifier. A cheap first pass with categories such as enquiry, support, invoice and spam, each with a one line description. Set When No Clear Match to Output on Extra, 'Other' Branch and send that branch to a person. Spam stops here.
  3. AI Agent. Set the prompt to Define below and pass the sender, subject and the email body with quoted history removed. The System Message states the job, the allowed tools and what to do when an action is refused.
  4. Tools. Find contact: an HTTP Request tool that searches your CRM's API by email. Create ticket or lead: an HTTP Request tool that writes to the CRM. Draft reply: the Gmail node's Draft, Create operation, so the reply waits in the inbox and is never sent by the agent.
  5. Structured Output Parser. A schema with category, contact id, urgency, a two line summary and whether a person must call back. A schema made from a JSON example marks every field as required, which is what you want here.
  6. Log. Write the parsed result and the email id to a sheet or a CRM note, so every decision can be checked later.

Where does the human approve?

Open the Tools panel on the AI Agent node, add a Human review step, choose a channel and connect the CRM write tool behind it. When the agent tries to use that tool, the workflow pauses. Approve, and the tool runs with the input the model chose. Deny, and the action is cancelled and the agent is told, so your system message must say what to do next.

Reviewers can be reached in n8n Chat, Slack, Microsoft Teams, Telegram, Gmail, Outlook or WhatsApp Business Cloud, among others. Put $tool.name and $tool.parameters in the message so the reviewer sees exactly what will be written. For timeouts, audit trails and which actions need approval at all, see our guide to human approval for AI agents.

Interlocking steel gears inside an industrial machine, in black and white
Approvals work best when the reviewer sees exactly what the agent wants to write and why.

Does the agent need memory?

For email triage, no: each email stands alone, and memory only adds tokens.

For WhatsApp enquiries, yes. Start the workflow with the WhatsApp Trigger (event Messages) and use the sender's number as the Session Key, so each customer has their own history. Two cautions from the n8n docs:

  • Simple Memory does not work in an active production workflow when n8n runs in queue mode, because calls may reach different workers. Use Postgres Chat Memory instead; it creates its table if it does not exist.
  • WhatsApp allows one webhook per app, so a test run and the live workflow overwrite each other's webhook. Test on a separate app or number.

Which model: an API or your own server?

The Tools Agent docs list chat models from OpenAI, Anthropic, Groq, Mistral Cloud and Microsoft Foundry. The Ollama Chat Model node connects to models running on your own server, which keeps customer emails inside your network.

Whichever you choose, the agent depends on the model calling tools correctly. Run 30 to 50 past emails through it before going live and check every tool call. Smaller local models are more likely to skip a tool or invent a field, so test them hardest.

Error handling and cost control

Errors.

  • Turn on Retry On Fail for the CRM tools, since APIs time out.
  • Set On Error to Continue (using error output) on the final write and route failures to a sheet a person checks.
  • Create an error workflow that starts with the Error Trigger node and select it under Error workflow in the workflow's settings, so a failed run sends an alert. Use Stop And Error when the parsed output is missing a required field.

Cost.

  • Max Iterations defaults to 10. A triage agent needs three or four tool calls, so lower it.
  • Let the classifier drop spam before the agent runs, and use a smaller model for classifying.
  • Trim signatures and quoted threads before the prompt.
  • On HTTP Request tools, Optimize Response sends the model only the fields it needs, or truncates text at 1,000 characters by default.
  • Turn on Return Intermediate Steps while testing to see every call, then switch it off. Check each provider's current pricing page for token rates.

Getting an agent like this built

We run n8n on a server you control, with error workflows, retries and model output checked against a schema before the next node trusts it (n8n integrations). For agents, anything that moves money, reaches a customer or cannot be undone waits for a person (AI agent development). One anonymous engagement used n8n, the WhatsApp Business API, a CRM and Claude to qualify incoming enquiries. Book a consultation.