How to Build Your First AI Agent with n8n (Step by Step)
A practical walkthrough of building a working AI agent in n8n — from trigger to tool use — that you can adapt for email triage, lead qualification, and customer support.
Hassan MahmoodIf you've been anywhere near the AI space lately, you've heard the word "agent" about a thousand times. Strip away the hype and an AI agent is simple: an LLM that can decide what to do next, and has tools to do it.
In this guide, I'll walk you through building a real one with n8n — the workflow automation tool I use in production for client projects. No code required, but everything here transfers to code-based frameworks like LangGraph later.
What we're building
An email triage agent that:
- Watches your inbox for new messages
- Reads and classifies each email (sales lead, support request, spam, internal)
- Drafts a reply for leads and support requests
- Files everything into the right place and pings you on Slack for anything urgent
This is the exact pattern behind systems I've deployed that save teams 10+ hours a week.
Step 1: Set up the trigger
Every n8n workflow starts with a trigger. For email, use the Gmail Trigger (or IMAP trigger if you're not on Google Workspace):
- Set it to poll every minute
- Filter to unread emails only
- Skip newsletters with a label filter — don't burn tokens on noise
Self-hosting n8n on a small VPS means this runs 24/7 for a few dollars a month. The cloud version works too if you want zero maintenance.
Step 2: Add the AI agent node
This is where n8n shines. The AI Agent node wraps three things in one place:
- The model — GPT-4o, Claude, or a local model via Ollama if you want privacy
- Memory — so the agent remembers context within a conversation thread
- Tools — the actions it's allowed to take
The system prompt is where the magic happens. Be specific:
You are an email triage assistant for a B2B services company.
Classify each email into exactly one category:
- LEAD: someone asking about services or pricing
- SUPPORT: an existing client with a problem
- INTERNAL: a colleague or business partner
- NOISE: newsletters, cold outreach, spam
For LEAD and SUPPORT emails, draft a reply in a
professional but warm tone. Never promise specific
pricing — always offer a call instead.
Escalate to the team on Slack when the email mentions
"urgent", "down", "broken", or legal language.
Vague prompts produce vague agents. The tighter your categories and rules, the better it performs.
Step 3: Give it tools
An agent without tools is just a chatbot. In n8n, connect these as tool nodes:
- Gmail: Add Label — to file emails by category
- Gmail: Create Draft — so a human approves replies before they send (do this for the first month, always)
- Slack: Send Message — for escalations
The agent decides when to call each tool based on the classification. That's the whole idea: you define the playground, it makes the plays.
Step 4: Test with real email
Run it manually against your last 20 emails and check every classification. You'll find edge cases fast — usually around forwards, CC chains, and people who write two-line emails with zero context. Tighten the prompt, not the workflow.
What most people get wrong
After building dozens of these, the failure modes are consistent:
- Letting the agent send emails directly on day one. Start with drafts. Build trust in the classifications first.
- One mega-prompt for everything. Split classification and drafting into separate steps when volume grows.
- No logging. Save every classification decision to a sheet or database. When something goes wrong — and it will — you need to see what the agent was "thinking."
Where to go from here
This same pattern extends naturally: qualify leads against your CRM, book meetings straight into your calendar, or route support tickets by urgency. If you want to go deeper, I cover builds like this on my YouTube channel, and if you'd rather have it built for you, take a look at my workflow automation service.
The tools are ready. The only question is which workflow you hand over first.
Want this working in your business?
I design and build AI systems like the ones in this article — from strategy to deployment. Start with a free 30-minute call.