Skip to content

Chapter 3: Building Your First AI Agent Workflow (Chatbot/Mail Agent Demo)

Video: Watch this chapter on YouTube (12:48)

Overview

This chapter provides a comprehensive hands-on walkthrough of building an AI agent workflow in n8n. The workflow creates an email assistant that can understand natural language requests, draft emails, and send them through Gmail—all powered by OpenAI's GPT models.

Detailed Summary

Setting Up the Workflow

Step 1: Creating the Trigger Node

The workflow begins with a trigger node. For this demo, the On Chat Message trigger is selected:

  1. Click the plus button in the top right corner
  2. Select "On chat message" trigger node
  3. A chat terminal appears where you can interact with the workflow
  4. Type "hello" to populate initial data

Identifying trigger nodes: Look for the lightning icon on the left side of the node.

Step 2: Understanding Trigger Node Output

Opening the chat message trigger node reveals: - Session ID: Identifies the chat session - Action: Shows "send message" since we're sending through chat input - Chat Input: Contains the actual message typed (e.g., "hello")

Data can be viewed in three formats: - JSON (underlying format) - Table (rows and columns) - Schema (structured view for easy drag-and-drop)

Adding the AI Agent Node

Step 3: Connecting the AI Agent

  1. Click the plus button and select "AI Agent"
  2. The output from the chat trigger automatically populates as input
  3. The prompt/user message field shows json.chatInput

Configuring the LLM

The first element to attach is the LLM (Large Language Model):

  1. Under the LLM section, select "OpenAI Chat Model"
  2. Choose or create OpenAI credentials:
  3. Click "Create new credential"
  4. Enter your OpenAI API key (obtained from OpenAI or CodeCloud Keyspace)
  5. Optionally add organization ID
  6. Click Save
  7. Select the model (e.g., GPT-4o Mini for most use cases)

Configuring Memory

Why memory matters:

Without memory: - User: "Hello, my name is Mark" - AI: "Hello Mark, how can I assist you today?" - User: "What is my name?" - AI: "I don't have access to personal information..." ❌

With memory: - Same conversation, but AI responds: "Your name is Mark" ✅

Setting up Simple Memory: 1. Under Memory, select "Simple Memory" 2. Leave "Connected chat trigger node" as the session key 3. Set context window length (default: 5 interactions) 4. The yellow highlight indicates changes requiring re-execution

Configuring the Gmail Tool

The third element is the email tool:

  1. Under Tools, add "Gmail" node
  2. Connect Gmail credentials:
  3. n8n Cloud: Sign in with Google (OAuth2 recommended)
  4. Self-hosted: Use service account method
  5. Configure Gmail settings:
  6. Tool Description: Set automatically (AI decides when to use)
  7. Resource: Message
  8. Operation: Send
  9. Recipient: Let model define
  10. Subject: Let model define
  11. Email Type: Text (or HTML for rich formatting)
  12. Message: Let model define

Configuring the AI Agent Prompts

System Message Setup

  1. Go to Options → Add Option → System Message
  2. Enter system prompt defining the agent's role:
You are a helpful email assistant which helps craft effective and succinct email based on user's instructions. You also help with sending the email by using the attached email tool when asked.

Pro tip: For production, use ChatGPT or another LLM to prompt engineer an effective system prompt.

User Message Setup

The user message should be dynamic (expression) not fixed: - Fixed: Same static input every run - Expression: Dynamic, changes based on user input

To configure: 1. Toggle to "Expression" 2. Drag the chatInput variable into the field 3. The value updates based on actual user input

Testing the Email Agent

Conversation Flow Example:

  1. User: "Hello, my name is Mark"
  2. AI accesses OpenAI and Simple Memory
  3. Gmail tool NOT used (no email request)
  4. Response: "Hello again Mark, what would you like to do today?"

  5. User: "I would like to send an email to my boss at marconi@codecloud.com about the upcoming marketing meeting on 26th of August 2025"

  6. AI asks for more details: "What would you like the subject and message to be?"

  7. User: "Please come up with it yourself"

  8. AI drafts email with subject, greeting, and body
  9. AI asks for confirmation before sending

  10. User: "My boss's name is Moonshot"

  11. AI updates the draft with correct name

  12. User: "Yes, please go ahead and send it"

  13. Gmail tool is activated
  14. Email is sent successfully

Customizing Gmail Options

Removing n8n Attribution

By default, emails include "This email was sent automatically by n8n". To remove: 1. Open Gmail node 2. Go to Options → Add Option 3. Select "Append n8n Attribution" 4. Toggle OFF

Node Control Features

Execute Step Button

  • Runs only the selected node
  • Useful for testing changes without running entire workflow
  • Saves API tokens

Deactivate Button

  • Disables a node temporarily
  • Workflow skips this node during execution
  • Useful for complex branched workflows

Delete Button

  • Removes the node from workflow

Pinning Data

The Pin feature is essential for development:

  1. Open node and click "Pin" in top right
  2. Data becomes fixed for that node
  3. Every demo/trial run uses pinned data
  4. Prevents API token consumption during testing
  5. Unpin before pushing to production

Publishing the Workflow

Activating for Production

  1. Toggle "Activate Workflow" at the top
  2. Warning message: "You can now make calls to your production chat URL"
  3. Click "Got it"
  4. Workflow now runs automatically on triggers

Making Chat Publicly Available

  1. Open chat trigger node
  2. Toggle "Make chat publicly available"
  3. Copy the generated URL
  4. Open URL in browser to access public chat interface
  5. Configure optional settings:
  6. Authentication requirements
  7. Custom initial messages

Viewing Execution Logs

Access execution history through the "Executions" button: - Beaker icon: Test executions (development) - No beaker: Production executions - Shows date, time, and execution type - "Copy to editor" imports production data for troubleshooting


Key Takeaways

  1. Three essential AI agent components: Every AI agent needs an LLM (brain), memory (context), and tools (capabilities).

  2. Memory enables context: Without memory, the AI treats each message as a new conversation. With memory, it maintains conversation continuity.

  3. AI decides tool usage: When tools are attached with "set automatically" description, the AI intelligently decides when to use them based on user requests.

  4. System vs User prompts: System prompts define the agent's role and behavior; user prompts carry the actual request.

  5. Fixed vs Expression: Use expressions for dynamic content that changes with user input; fixed values for constants.

  6. Pin data during development: Pinning prevents unnecessary API calls and costs while testing.

  7. Execute step for iteration: Test individual nodes without running the entire workflow.

  8. Production requires activation: Toggle the workflow active for it to respond to real triggers.

  9. Public chat access: n8n can generate public URLs for external users to interact with your AI agent.

  10. Execution logs aid troubleshooting: Copy production data to editor for debugging issues.

Conclusion

Building the first AI agent workflow demonstrates the power and simplicity of n8n. The email assistant created in this chapter showcases how natural language can be transformed into automated actions—receiving a conversational request, understanding the intent, drafting appropriate content, and executing the send action through Gmail. The workflow combines the intelligence of GPT models with the practical utility of email automation, all configured through n8n's visual interface without writing code. This foundation prepares learners for more complex workflows involving multiple tools, conditional logic, and advanced AI capabilities in subsequent chapters.