AI Agents

In the last lesson, you built a feedback form that captures user input but sends it nowhere. Now you'll solve that problem by introducing n8n – a tool that connects your app to hundreds of services without writing backend code. You'll use AI agents to analyze and enrich the feedback before sending it back to your app.

What you'll gain

• n8n account with a working feedback automation workflow.
• Understanding of n8n AI Tools Agents and how they process data intelligently.
• Sentiment analysis powered by OpenRouter's LLMs.
• Structured feedback data that your app can use.

1. Introduction to n8n and third-party integrations

When building a product, you'll quickly realize you can't code everything from scratch. Need to send emails? That's a whole email server. Need to store files? That's a storage service. Need to process payments? That's a payment system.

Why third-party tools matter:

Instead of building and maintaining all these systems, you integrate with services that already do them well. This lets you:

  • Move faster – integrate in hours, not weeks
  • Focus on your unique value – build what makes your product special
  • Test before committing – try things without heavy investment
  • Scale easily – these services handle growth for you

n8n is a Swiss Army knife for integrations:

n8n is a workflow automation tool. You connect different apps and services together without writing backend code. Think of it as visual programming for connecting things.

One integration, hundreds of apps:

Once you connect to n8n, you get access to integrations with:

  • Communication: Slack (team notifications), Gmail (email), Discord (community updates)
  • Documentation: Notion (knowledge base), Google Docs (shared docs)
  • Data: Airtable (spreadsheet databases), Google Sheets (analysis)
  • Project Management: Linear (issues), GitHub (code automation)
  • Databases: Neon (your app's database), PostgreSQL, MySQL
  • AI: OpenAI (GPT models), Anthropic (Claude), OpenRouter (multi-model access)

The power of this approach:

Instead of building custom integrations for each service, you build one: your app talks to n8n, and n8n talks to everything else. When you want to add a new service, you don't change your app – you just add it to your n8n workflow.

Outcome: You understand why n8n simplifies third-party integrations.

I understand why n8n simplifies third-party integrations

2. Set up n8n account and first workflow

Let's get hands-on. You'll create an n8n account and build your first workflow: chat trigger to email notification.

Step 1: Create an n8n account

  1. Go to n8n.io
  2. Click "Sign up" and create your account
  3. Choose the Cloud option (no installation needed)
  4. Complete the onboarding flow

Step 2: Create your first workflow

Once you're logged in, you'll create a simple workflow that tests with chat input:

  1. Add a Chat Trigger node:

    • Click "Add node" and search for "Chat"
    • Select the Chat Trigger node
    • This lets you manually trigger the workflow by typing in a chat message
    • No complex webhook URLs needed yet – we'll add those in lesson 5.4
  2. Add a Gmail node:

    • Click the + icon on the Chat Trigger node to add an action
    • Search for "Gmail" and select it
    • Authorize it with your Gmail account
    • Configure the node:
      • Action: "Send Email"
      • To: feedback@yourdomain.com (use your email or domain email)
      • Subject: "New Feedback Received"
      • Body: Include the feedback message from the chat

    Note: If you don't have a Gmail account or prefer another service, n8n supports many alternatives like Slack, Discord, or Microsoft Teams. For this lesson, we'll use Gmail, but the workflow works the same with any messaging service.

Step 3: Connect the nodes

When you use the + icon to add Gmail, it automatically connects to the Chat Trigger. No manual connections needed.

Step 4: Test the workflow locally in n8n

  1. Click the Test button in n8n
  2. In the chat message input, paste one of these example feedback messages:

Negative Example:

I've been using the dashboard for a week now and I keep running into the same problem. When I click "Export Report," the button doesn't clearly show what's happening. There's no loading spinner or confirmation message, so I can't tell if my click registered or if it's processing. I've clicked it 3 times thinking it didn't work, and now I have 3 export jobs running. This is really frustrating and wastes my time.

Positive Example:

Just wanted to say the new onboarding flow is fantastic! The step-by-step guidance is clear and I was able to set up my account in less than 5 minutes. The explanations for each setting are really helpful, and the progress indicator at the top shows me exactly where I am in the process. Great work on this!
  1. After sending the message, check your email – you should receive the feedback notification
  2. In n8n, view the execution log to see the data flow

What just happened:

  • You sent feedback via chat
  • n8n received it
  • Gmail sent you an email with the feedback

This is the foundation. In the next sections, we'll add an AI agent to analyze the feedback before sending the email.

Outcome: You have a basic n8n workflow that sends feedback via email.

I built my first n8n workflow

3. Add an AI Tools Agent to your workflow

Now that your basic workflow works (Chat → Gmail), let's make it intelligent by adding an AI Tools Agent between them.

What is an n8n AI Tools Agent?

An n8n AI Tools Agent is a node that uses large language models (LLMs) to process and analyze text. It can:

  • Understand natural language input (like user feedback)
  • Analyze and categorize the content
  • Return structured results that your systems can use

Read more in the AI Tools Agent documentation.

Example:

Without an AI agent: Feedback "The app crashes when I export data" → just forwarded to email

With an AI agent: Same feedback → analyzed → categorized as "bug", sentiment "negative", priority "high" → sent to email with analysis

Step 1: Add the AI Agent node to your workflow

  1. In your n8n workflow, click between the Chat Trigger and Gmail nodes
  2. Search for "AI Agent" and select it
  3. Choose "Tools Agent" as the type
  4. Your workflow now looks like: Chat Trigger → AI Agent → Gmail

Step 2: Connect the chat input to the AI Agent

The chat message needs to flow into the AI Agent's prompt:

  1. Click on the AI Agent node
  2. In the "Prompt" or "Text" field, reference the chat input
  3. Use an expression like: {{ $json.chatInput }} (the exact field name depends on your Chat Trigger output)

Step 3: Configure the system message

The system message tells the AI Agent how to behave:

  1. In the AI Agent node, click "Add Option"
  2. Select "System Message"
  3. Enter this basic system message:
    You are a feedback analysis assistant. Analyze user feedback and provide a brief summary of the sentiment and type of feedback.
    

What happens now?

Your workflow is: Chat → AI Agent (analyzes) → Gmail

But if you test it right now, it won't work yet. Why? The AI Agent needs a language model to do the thinking. That's what we'll set up in the next section.

Outcome: You've added an AI Agent to your workflow (but it needs a brain to work).

I added an AI Agent node to my workflow

4. Set up OpenRouter and connect the AI model

Your AI Agent needs a "brain" – a large language model (LLM) – to do the analysis. We'll use OpenRouter to access AI models.

Why OpenRouter?

OpenRouter is a unified API that gives you access to many AI models through one account:

  • Free models available – test AI features without spending money
  • Access to premium models – add $10 credit to use a more capable paid model (browse the AI Gateway / OpenRouter for current options)
  • One API for everything – switch between models without changing code
  • Browse all models – see free models on OpenRouter

Step 1: Create an OpenRouter account and get your API key

  1. Go to OpenRouter.ai
  2. Click "Sign up" and create your account (free)
  3. After logging in, search for "API Keys" in the OpenRouter interface
  4. Click "Create Key" and give it a name (e.g., "n8n workflow")
  5. Copy your API key and save it securely

You don't need to add credits yet – there are plenty of free models to test with!

Step 2: Add OpenRouter credentials to n8n

  1. Go back to your n8n workflow
  2. In the bottom left, click "Credentials"
  3. Click "Create new credential"
  4. Search for "OpenRouter" and select it
  5. Paste your API key in the "API Key" field
  6. Click "Save"

Step 3: Connect OpenRouter to your AI Agent

  1. Go back to your workflow and click on the AI Agent node
  2. Find the "Model" section
  3. Click "Add Model" and select "OpenRouter Chat Model" (or similar)
  4. Select your OpenRouter credentials from the dropdown
  5. Choose a model:
    • Click "Browse models" or enter a model name
    • For free testing, pick a free or low-cost model – any model with a :free suffix works
    • Browse all free models here

Step 4: Test your workflow

Now your AI Agent has a brain! Let's test it:

  1. Click "Test workflow" in n8n
  2. Send one of the feedback examples from Section 2
  3. Check your email – you should now receive an email with AI-analyzed feedback

If you want better analysis later, you can:

  • Add $10 credit to your OpenRouter account
  • Switch to a more capable paid model (browse the AI Gateway / OpenRouter for current options)
  • No code changes needed – just select a different model

Outcome: Your AI Agent is now powered by OpenRouter and can analyze feedback.

I connected OpenRouter to my AI Agent

5. Understanding structured outputs

Now that your AI Agent works, let's understand a key concept: structured data.

The problem with raw text:

Right now, your AI Agent returns natural language responses like:

"This feedback seems negative. The user is frustrated with the export button because it doesn't show loading state. This is a usability issue and should be high priority."

This is helpful for humans reading emails, but hard for code to process. What if you want to:

  • Store feedback in a database with specific columns (sentiment, category, priority)
  • Automatically create tasks for high-priority bugs
  • Show analytics (how many negative feedbacks this week?)

Structured data makes everything easier:

Instead of a paragraph, structured data looks like this:

{
  "sentiment": "negative",
  "category": "bug",
  "priority": "high",
  "summary": "Export button missing loading indicator",
  "actionable": true
}

With structured data:

  • ✅ Your code knows exactly what fields to expect
  • ✅ You can easily store it in a database
  • ✅ You can analyze trends (how many high-priority bugs this week?)
  • ✅ You can trigger automated actions (create a task, send alert)

Why this matters for your feedback system:

In lesson 5.4, when you send feedback back to your app, you'll need structured data. The AI Agent can convert unstructured feedback text into clean JSON that your app can store in Neon.

Read more about structured output parsing in n8n.

Outcome: You understand why structured data is essential for automation.

I understand why structured data is essential

6. Configure the AI Agent for sentiment analysis with structured output parsing and self-fixing

Now let's upgrade your AI Agent's system message to return structured sentiment analysis. But there's a problem we need to solve first: LLMs don't always follow instructions perfectly. Sometimes they return malformed JSON, add extra commentary, or miss fields. That's where n8n's output parsing and self-fixing mechanisms come in.

6.1 Understanding the Output Parsing Challenge

The problem:

You tell an LLM "respond with JSON", but you might get:

  • Valid JSON buried in extra text: "Here's the analysis: {json} Let me know if you need more!"
  • Malformed JSON: Missing quotes, trailing commas, incomplete objects
  • Missing or incorrect field types: "priority": "super-high" instead of the expected "high"

The solution: Output Parsing + Self-Fixing

n8n provides two powerful tools:

  1. Structured Output Parser – Defines the exact schema you expect and attempts to parse the LLM output
  2. Auto-fixing Output Parser – Wraps the first parser and uses a second LLM to automatically correct any parsing errors

6.2 Update the system message for strict JSON output

First, let's craft a system message that explicitly guides the LLM toward valid JSON:

  1. Go to your workflow and click on the AI Agent node
  2. Find your existing System Message (you added this in Section 3)
  3. Replace it with this enhanced system message:
You are a feedback analysis assistant. Analyze user feedback and return ONLY valid JSON with no additional text or commentary.

CRITICAL: Your response must be valid JSON that can be parsed. No markdown code blocks, no explanations, no extra text before or after the JSON.

Always respond with valid JSON in this exact format:
{
  "sentiment": "positive" | "negative" | "neutral",
  "category": "bug" | "feature_request" | "question" | "other",
  "priority": "low" | "medium" | "high",
  "summary": "one sentence summary of the feedback",
  "actionable": true | false
}

Sentiment rules:
- Positive: user is happy, satisfied, praising
- Negative: user is frustrated, reporting problems, complaining
- Neutral: general feedback, suggestions, questions

Priority rules:
- High: blocks user workflow, frequently mentioned issues, critical functionality
- Medium: useful improvement, nice-to-have feature, minor bugs
- Low: minor issue, edge case, cosmetic problem

Category rules:
- bug: something is broken or not working as expected
- feature_request: user wants new functionality or improvements
- question: user is asking for help or clarification
- other: feedback that doesn't fit other categories

Remember: Respond ONLY with the JSON object. Nothing else.

This system message is explicit and repetitive–that's intentional. LLMs respond better to redundancy and clear constraints.

6.3 Add a Structured Output Parser node

Now let's add n8n's output parser to validate and extract JSON from the AI Agent's response:

  1. In your n8n workflow, click the + icon after the AI Agent node
  2. Search for "Structured Output Parser" (it's part of the LangChain nodes)
  3. Click to add it to your workflow
  4. Your workflow now looks like: Chat Trigger → AI Agent → Structured Output Parser → Gmail

Configure the Structured Output Parser:

  1. Click on the Structured Output Parser node
  2. Under "Schema", define your expected JSON structure. Most parsers let you paste a JSON example:
{
  "sentiment": "positive",
  "category": "bug",
  "priority": "high",
  "summary": "one sentence summary",
  "actionable": true
}
  1. Click "Parse Output" or similar option to enable automatic parsing
  2. Connect the AI Agent's text output as the input to this parser

The parser will now extract valid JSON from whatever the AI Agent returns, even if there's extra text around it.

6.4 Add an Auto-fixing Output Parser for error recovery

Here's the powerful part: if the Structured Output Parser fails, an Auto-fixing parser can automatically correct it using a second LLM.

  1. After the Structured Output Parser node, click + icon to add another node
  2. Search for "Auto-fixing Output Parser" (also in LangChain nodes)
  3. Add it to your workflow
  4. Your workflow now looks like: Chat Trigger → AI Agent → Structured Output Parser → Auto-fixing Output Parser → Gmail

Configure the Auto-fixing Output Parser:

  1. Click on the Auto-fixing Output Parser node

  2. Set the Base Parser to your Structured Output Parser (reference it from the previous step)

  3. Configure the Retry LLM:

    • Click "Add LLM" and select your OpenRouter credentials (same ones you used for the AI Agent)
    • Choose a model – optionally pick a different (more capable) model than your AI Agent
    • For example: if your AI Agent uses a free or low-cost model, your retry LLM could use a more capable paid model (for $10 credit users)
  4. Set "Max Retries" to 1 or 2 (higher numbers mean more API calls and cost)

How it works:

  1. AI Agent returns raw text with JSON
  2. Structured Parser tries to extract JSON
  3. If parsing succeeds → data flows to Gmail ✅
  4. If parsing fails → Auto-fixing Parser:
    • Takes the original text and the parsing error
    • Sends it to the retry LLM with a prompt like: "Fix this JSON to match this schema"
    • Returns the corrected JSON → data flows to Gmail ✅

6.5 Update the Gmail node to handle structured data

Now that you have guaranteed structured data, update your Gmail node to display it properly:

  1. Click on the Gmail node
  2. In the Gmail node settings, find "Options" and click "Add Option"
  3. Select "Email Type" and set it to "HTML" (for proper formatting)
  4. Update the email Subject:
Feedback Analysis: {{$json.sentiment}} {{$json.priority}}
  1. Update the email Body with HTML formatting:
<h2>📝 Feedback Received</h2>
<p><strong>Original:</strong></p>
<blockquote style="background: #f5f5f5; padding: 10px; border-left: 4px solid #999;">
  {{ $json.chatInput }}
</blockquote>

<hr>

<h2>🤖 AI Analysis</h2>
<table style="width: 100%; border-collapse: collapse;">
  <tr style="background: #f9f9f9;">
    <td style="padding: 8px; border: 1px solid #ddd;"><strong>Sentiment:</strong></td>
    <td style="padding: 8px; border: 1px solid #ddd;">{{ $json.sentiment }}</td>
  </tr>
  <tr>
    <td style="padding: 8px; border: 1px solid #ddd;"><strong>Category:</strong></td>
    <td style="padding: 8px; border: 1px solid #ddd;">{{ $json.category }}</td>
  </tr>
  <tr style="background: #f9f9f9;">
    <td style="padding: 8px; border: 1px solid #ddd;"><strong>Priority:</strong></td>
    <td style="padding: 8px; border: 1px solid #ddd;">{{ $json.priority }}</td>
  </tr>
  <tr>
    <td style="padding: 8px; border: 1px solid #ddd;"><strong>Summary:</strong></td>
    <td style="padding: 8px; border: 1px solid #ddd;">{{ $json.summary }}</td>
  </tr>
  <tr style="background: #f9f9f9;">
    <td style="padding: 8px; border: 1px solid #ddd;"><strong>Actionable:</strong></td>
    <td style="padding: 8px; border: 1px solid #ddd;">{{ $json.actionable ? '✅ Yes' : '❌ No' }}</td>
  </tr>
</table>

<hr>

<p style="font-size: 12px; color: #666;">
  This analysis was generated automatically. Please review for accuracy before taking action.
</p>

6.6 Test the parsing and self-fixing workflow

Now test the entire flow with error recovery:

  1. Click "Test workflow" in n8n
  2. Send the negative feedback example from Section 2
  3. Check the execution logs for each node:
    • AI Agent output: Should see raw text with JSON
    • Structured Parser output: Should show extracted JSON (or an error if it failed)
    • Auto-fixing Parser output: Should show corrected JSON if the parser had to fix anything
  4. Check your email – you should see a nicely formatted analysis table

What to look for:

  • ✅ Email displays all fields correctly (sentiment, category, priority, summary, actionable)
  • ✅ Values match what you'd expect from the feedback
  • ✅ HTML table renders properly with borders and spacing
  • ✅ Actionable field shows ✅ or ❌

6.7 Understanding when the auto-fixer activates

The auto-fixer helps when:

  • AI Agent returns JSON with trailing commas: {"sentiment": "positive",}
  • AI Agent wraps JSON in extra text: "Here's the analysis: {...} Hope this helps!"
  • AI Agent uses slightly wrong field names: {"feeling": "positive"} instead of "sentiment"
  • AI Agent returns wrong types: "priority": 5 instead of "priority": "high"

The auto-fixer doesn't help when:

  • The original text doesn't contain JSON at all (the AI Agent completely ignored instructions)
  • The schema is fundamentally wrong (you asked for JSON but the AI Agent returns CSV or XML)

If the auto-fixer fails, your workflow will error. That's when you need to:

  • Upgrade to a more capable paid model (with paid credits)
  • Refine your system message to be even more explicit
  • Add more examples of correct JSON in your system prompt

6.8 Advanced: Using different models for the auto-fixer

Optional / advanced

This sub-section is optional. If you're not coding, you can safely skip it – your workflow already works. Come back to it later when you want to tune cost for production.

For production workflows, you might want smarter self-correction:

Setup:

  • AI Agent model: a free or low-cost model (fast)
  • Auto-fixer retry model: a more capable paid model (slower, more reliable)

Why this works:

  • The cheap model handles most feedback quickly and cheaply
  • When its JSON is malformed, the stronger model fixes it reliably
  • You only pay for the stronger model when the first attempt fails

How to set it up:

  1. In the Auto-fixing Output Parser, click "Add LLM" (if not already done)
  2. Create a new credential for OpenRouter pointing to a different model
  3. Select a more capable paid model instead of the cheap one
  4. n8n will now use the cheaper model first, and escalate to the stronger model only when needed

This is a cost-effective pattern for production AI systems.

Outcome: Your AI Agent now returns guaranteed, self-correcting structured data with automatic error recovery.

I configured structured output parsing and auto-fixing

7. Test with different types of feedback

Now let's verify your AI Agent correctly categorizes different types of feedback.

Test with multiple examples:

  1. Click "Test workflow" in n8n
  2. Send each of these feedback messages one at a time:

Test 1 - Positive feedback:

Just wanted to say the new onboarding flow is fantastic! The step-by-step guidance is clear and I was able to set up my account in less than 5 minutes. The explanations for each setting are really helpful, and the progress indicator at the top shows me exactly where I am in the process. Great work on this!

Expected analysis:

  • Sentiment: positive
  • Category: other or feedback
  • Priority: low (it's praise, not urgent action)

Test 2 - Bug report (the negative example from earlier):

I've been using the dashboard for a week now and I keep running into the same problem. When I click "Export Report," the button doesn't clearly show what's happening. There's no loading spinner or confirmation message, so I can't tell if my click registered or if it's processing. I've clicked it 3 times thinking it didn't work, and now I have 3 export jobs running. This is really frustrating and wastes my time.

Expected analysis:

  • Sentiment: negative
  • Category: bug
  • Priority: high (blocks user workflow)

Test 3 - Feature request:

It would be nice to have keyboard shortcuts for common actions. I'm a power user and navigating with the mouse slows me down.

Expected analysis:

  • Sentiment: neutral
  • Category: feature_request
  • Priority: medium (nice to have improvement)

Check your results:

After each test, check your email to verify:

  • ✅ The structured fields appear correctly
  • ✅ Sentiment matches the tone of feedback
  • ✅ Priority makes sense for the issue severity
  • ✅ Summary captures the main point

If the AI gets it wrong:

  • Try a more capable paid model (with $10 credit)
  • Refine your system message with more specific examples
  • Add more detailed rules for each category

Outcome: You've verified your AI Agent correctly analyzes different feedback types.

I tested the AI Agent with different feedback types

What's next

You've built an intelligent feedback processing system in n8n:

  • ✅ n8n workflow with Chat Trigger → AI Agent → Gmail
  • ✅ AI Tools Agent powered by OpenRouter that analyzes sentiment
  • ✅ Structured JSON output with sentiment, category, and priority
  • ✅ Email notifications with enriched feedback analysis

What you learned:

  • How to set up n8n workflows with visual nodes
  • How AI Tools Agents process and analyze text
  • Why OpenRouter gives you access to many AI models
  • Why structured data matters for automation

Continue to: 5.3 Mobile Responsive

In the next lesson, you'll:

  • Ensure your feedback form works beautifully on mobile devices
  • Test across different screen sizes
  • Fix any responsive design issues

Then in lesson 5.4 (Webhook Integration), you'll:

  • Replace the Chat Trigger with a Webhook that your app can call
  • Connect your feedback form to the n8n webhook
  • Create an API endpoint to receive the structured feedback back
  • Store everything in Neon
  • Test the complete end-to-end flow

Your feedback system is becoming production-ready!