🔨 LearnForge
Tutorial

n8n + Claude: How to Build an AI Agent That Reads and Responds to Emails

A complete walkthrough of building an autonomous email agent in n8n using Anthropic's Claude model. The agent reads Gmail, classifies each email, drafts an appropriate reply, and optionally updates a CRM — no coding required, and deployable in an afternoon.

📅 October 8, 2026 ⏱️ 15 min read ✍️ LearnForge Team 🏷️ n8n · Claude · AI Agents · Tutorial
n8n plus Claude AI agent workflow for email automation 2026

What you'll build

An n8n workflow that watches your Gmail inbox, passes each new email to Claude 3.5 Haiku, and gets back a classified category (refund / shipping / billing / technical / general) plus a drafted reply. The reply is saved as a Gmail Draft for your review. Optionally, a CRM record is created or updated in parallel. Total setup time: 2–3 hours for a first build, less if you follow this guide step by step.

Prerequisites: an n8n account (cloud or self-hosted), a Gmail account, and an Anthropic API key from console.anthropic.com. No coding experience required.

Steps in this tutorial

  1. Why Claude specifically — and which model to use
  2. Setting up credentials: Anthropic API key + Gmail OAuth
  3. Building the workflow: Gmail Trigger → AI Agent
  4. Writing the system prompt
  5. Wiring tools: Gmail Draft + optional CRM
  6. Testing, error handling, and going live
  7. Cost breakdown and scaling
  8. FAQ

Step 1: Why Claude — and which model

1

Choosing between Claude models for email workflows

~5 min to decide

Claude is the better choice over GPT-4o Mini for email tasks because of one specific capability: instruction-following precision. Claude follows multi-condition rules ("if the email mentions a refund AND the order is over 30 days old, do X; if under 30 days, do Y") more reliably than GPT models of similar cost, and produces more consistently structured output. For workflows where an incorrect action (wrong CRM update, off-tone reply) has real consequences, that reliability is worth the slightly higher cost.

Claude 3.5 Haiku
$0.80 / 1M input tokens
Best for this tutorial. Fast, accurate for email classification and reply drafting, strong instruction-following. Handles 99% of business email workflows effectively.
Claude 3.5 Sonnet
$3.00 / 1M input tokens
Use when edge cases matter more than cost — complex legal or financial emails, situations requiring nuanced judgement calls, or when tone accuracy is critical.
Claude 3 Opus
$15.00 / 1M input tokens
Rarely needed for email automation. Reserved for highly complex reasoning tasks. Cost is prohibitive for high-volume workflows.

Use Claude 3.5 Haiku for this tutorial. You can switch to Sonnet later if you find specific email types being handled incorrectly.

Step 2: Setting up credentials

2

Anthropic API key + Gmail OAuth in n8n

~20 min

Anthropic API key: Go to console.anthropic.com → API Keys → Create Key. Copy the key — it starts with sk-ant-. In n8n, go to Credentials → New → Anthropic. Paste the key, name it (e.g., "Anthropic Production"), and save. Test the credential — n8n will ping the API to confirm the key works.

Gmail OAuth: In n8n, go to Credentials → New → Gmail OAuth2. You'll need a Google Cloud Console project with the Gmail API enabled and OAuth consent screen configured. n8n's credential documentation walks through this in detail. The short version: create a project in console.cloud.google.com, enable Gmail API, create OAuth Client ID credentials (type: Web application), add n8n's redirect URI, and paste the Client ID and Secret into n8n. Then click "Sign In With Google" and authorize the email account the agent will watch.

Important: n8n's OAuth redirect URI format is https://[your-n8n-domain]/rest/oauth2-credential/callback for cloud, or http://localhost:5678/rest/oauth2-credential/callback for local. Add this exact URI to the "Authorized redirect URIs" in Google Cloud Console, or the OAuth flow will fail.

Once both credentials are saved and tested, you're ready to build the workflow.

Step 3: Building the workflow canvas

3

Gmail Trigger → AI Agent node structure

~30 min

Create a new workflow in n8n. Add nodes in this order:

Gmail Trigger → AI Agent → Gmail: Create Draft

Gmail Trigger node settings:

  • Resource: Message
  • Event: Message Received
  • Filters → Label/Mailbox: choose a specific label (e.g., "support") to limit scope, or leave empty to watch all incoming mail
  • Filters → Exclude Label: add "SENT", "DRAFT" to avoid processing your own outgoing messages
  • Poll Time: Every Minute (for near-real-time), or Every 5 Minutes (for lower API costs)

AI Agent node settings:

  • Agent: Conversational Agent (best for email workflows)
  • Chat Model: Anthropic Chat Model → select your Anthropic credential → Model: claude-3-5-haiku-20241022
  • Prompt (Human Message): Map from Gmail Trigger — use the expression {{ $json.text }} for the email body, combined with sender and subject fields
  • System Message: write your system prompt here (covered in Step 4)

For the Human Message input, format the email context clearly so Claude has all the information it needs:

// Expression in the AI Agent's "Human Message" field: FROM: {{ $('Gmail Trigger').item.json.from.value[0].address }} SUBJECT: {{ $('Gmail Trigger').item.json.subject }} DATE: {{ $('Gmail Trigger').item.json.date }} EMAIL BODY: {{ $('Gmail Trigger').item.json.text }}

Step 4: Writing the system prompt

4

The system prompt — where most of the work happens

~45 min to write and test

The system prompt defines the agent's entire behaviour: who it is, what it's allowed to do, how to handle each email type, and what format to return. This is the most important part of the build. A strong prompt produces consistent, accurate output. A vague one produces unpredictable results.

Here is a real-world template you can adapt:

System Prompt Template
You are the customer support agent for [COMPANY NAME], a [BRIEF DESCRIPTION — e.g., "Shopify store selling handmade candles"]. Your role is to read incoming customer emails, classify them, and draft appropriate replies. CLASSIFICATION CATEGORIES: - refund_request: customer wants money back - shipping_question: asking about delivery status or timing - product_question: asking about a specific product - billing_issue: payment problem, wrong charge - complaint: expressing dissatisfaction - general_inquiry: anything that doesn't fit above RULES BY CATEGORY: - refund_request: Check if order is within 30-day policy. If yes, confirm the refund will be processed in 3–5 business days. If outside policy, offer store credit as an alternative. - shipping_question: Acknowledge the question and note that tracking info will be sent separately (you do not have access to live tracking — flag for human review). - product_question: Answer based on the product knowledge below. If unsure, say you'll follow up within 24 hours. - billing_issue: Apologize and confirm the team will investigate within 1 business day. Do not make commitments about refund amounts. - complaint: Acknowledge their frustration directly (don't be defensive), apologize, and offer a concrete next step. - general_inquiry: Respond helpfully and briefly. TONE: Warm but professional. First-name basis with customers. Maximum 150 words per reply. Never start a reply with "I" — start with the customer's name. OUTPUT FORMAT (return exactly this JSON structure): { "category": "[one of the categories above]", "draft_reply": "[the full email reply text]", "needs_human_review": true/false, "reason_for_review": "[explain if needs_human_review is true, else empty string]" } PRODUCT KNOWLEDGE: [paste your product FAQ here, or key product details]

Two things to customize before going live: replace all bracketed placeholders, and fill in the PRODUCT KNOWLEDGE section with the actual information Claude needs to answer product questions accurately. The more specific the rules, the less often the agent will produce a reply that needs editing.

Prompt iteration tip: Run the workflow manually on 10–20 old emails before activating it. Compare the agent's output to what you actually sent at the time. Where they diverge, add a rule to the system prompt to cover that case. Most prompts reach production-quality after 3–4 iterations.

Step 5: Wiring tools and output

5

Gmail Draft output + optional CRM update

~30 min

After the AI Agent node, add a Code node (a simple one — just 3 lines) to parse the JSON output from Claude:

// Parse Claude's JSON output const output = JSON.parse($input.item.json.output); return { json: output };

Then add a Switch node to route based on {{ $json.needs_human_review }}:

  • If false → Gmail: Create Draft (saves the reply for your quick review)
  • If true → Slack: Send Message (notifies you with the email summary and reason for review)

Gmail: Create Draft node settings:

  • To: {{ $('Gmail Trigger').item.json.from.value[0].address }}
  • Subject: Re: {{ $('Gmail Trigger').item.json.subject }}
  • Message: {{ $json.draft_reply }}
  • Reply To: map the original message ID using {{ $('Gmail Trigger').item.json.id }} — this threads the reply correctly in Gmail

Optional — CRM node (HubSpot or Notion): Add in parallel after the Switch node. Create a contact record with email address, category, and a note containing the draft reply. This builds a log of all agent actions even if you edit the draft before sending.

Step 6: Testing and going live

6

Manual testing → live activation

~30 min

Before activating the workflow, test it manually on real emails. In n8n, use the "Execute Step" function to run each node individually. For the Gmail Trigger, use "Fetch Test Event" to pull a real recent email. Then run the AI Agent node with that data and inspect what Claude returns.

Check three things in the output:

  1. Is the category correct for this email type?
  2. Is the draft_reply something you'd actually send?
  3. Does the JSON parse cleanly — no extra text outside the JSON structure?

If Claude returns text outside the JSON (common on first runs), add this line to your system prompt: "Return only the JSON object. No explanation, no preamble, no markdown code fences. Start your response with { and end with }." This eliminates malformed output in almost all cases.

Once 8–10 test emails produce correct output, activate the workflow. Monitor the Gmail Drafts folder for the first 48 hours — check every draft before sending. After 1–2 weeks of monitoring, you'll have a clear picture of which email types the agent handles perfectly and which need occasional editing. Adjust the system prompt for the latter.

Learn to Build This End-to-End

LearnForge's AI Apps course walks through the full build — credentials, AI Agent node, tool wiring, system prompts, error handling, and production deployment. Module 0 is free.

Start Free Module →

Step 7: Cost breakdown and scaling

Claude 3.5 Haiku pricing: $0.80 per million input tokens, $4 per million output tokens. A typical email interaction (incoming email ~300 words, system prompt ~500 words, output ~200 words) uses roughly 1,200 input tokens and 300 output tokens.

Cost per email: (1,200 × $0.80 + 300 × $4) / 1,000,000 ≈ $0.0022 per email. At 100 emails/day: $0.22/day, $6.60/month. At 500 emails/day: $33/month in API costs alone — at that volume, consider whether Claude 3.5 Haiku is still the right model or whether a more aggressive filtering layer should pre-screen emails before sending them to Claude.

Total monthly cost for a typical small business (50–150 emails/day): Claude API $1–4 + n8n cloud $20 = $21–24/month for a continuously running email agent. Self-hosted n8n ($6/month VPS): $7–10/month total.

FAQ

Can you use Claude with n8n? +
Yes. n8n has native Anthropic credential support. Add your API key in n8n's Credentials, then select Claude models in the AI Agent node, Chat Model node, or LLM Chain node. No custom code needed for standard setups.
Which Claude model is best for n8n workflows? +
Claude 3.5 Haiku for most business workflows — email classification, reply drafting, data extraction. It's fast, cost-effective ($0.80/1M input tokens), and follows complex instructions reliably. Use Claude 3.5 Sonnet ($3/1M) when edge-case accuracy matters more than cost.
How do I connect Claude to Gmail in n8n? +
Three parts: (1) Add Anthropic API key credential in n8n. (2) Add Gmail OAuth2 credential via Google Cloud Console (create a project, enable Gmail API, create OAuth Client ID, add n8n's redirect URI). (3) Wire Gmail Trigger → AI Agent node (Claude) → Gmail Create Draft. The AI Agent handles the loop between Claude and Gmail tools automatically.
How do I write a good system prompt for an n8n + Claude email agent? +
Include: your business context (2 sentences), customer types and common questions, specific rules for each email category, escalation criteria, tone instructions, output format (return JSON). The more specific the rules, the more consistent the output. Vague prompts produce inconsistent results — iterate on old emails before going live.
Is it safe to let Claude reply to emails automatically? +
Start with Gmail Draft mode — agent drafts, you click send. After 2–4 weeks of prompt tuning, 70–80% of drafts typically need no editing. Then selectively enable auto-send for specific low-risk categories (shipping status, FAQs) while keeping complex cases in draft mode.
How much does the n8n + Claude email agent cost to run? +
At 50–150 emails/day with Claude 3.5 Haiku: $1–4/month in API costs. Plus n8n cloud at $20/month or self-hosted at $5–6/month. Total: $21–26/month for a fully running email agent — less than $0.01 per email handled.
Can the n8n + Claude agent update a CRM automatically? +
Yes. Add a HubSpot, Notion, Airtable, or Google Sheets node after the Switch node. The agent creates or updates a CRM record in the same workflow run — contact created, category logged, draft reply saved as a note. No separate workflow needed.
What happens when the Claude agent makes a mistake? +
Draft mode catches mistakes before they reach customers. For irreversible actions (CRM updates), add a Slack notification with a summary so you can review. Add a low-confidence rule to your system prompt: "if unsure, set needs_human_review to true." Most agents reach 90%+ accuracy within 2 weeks of prompt iteration.

Related Articles

No-Code & AI

What is an AI Agent? Plain English Guide for Non-Coders

The fundamentals — what agents are, how they work, and what they can do for a business.

No-Code & AI

n8n for Small Business: 8 Automations to Set Up This Week

8 practical workflows that save 10–20 hours per week — all buildable without coding.

No-Code & AI

n8n for Beginners: 7 Things to Know Before Starting

Nodes, triggers, expressions, credentials — the n8n fundamentals in plain language.

Build Your n8n + Claude Agent Today

LearnForge's AI Apps course covers every step in this tutorial and more — credential setup, AI Agent node mastery, prompt engineering, error handling, and production deployment. Start free.

Start Free Module →