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:
- Click the plus button in the top right corner
- Select "On chat message" trigger node
- A chat terminal appears where you can interact with the workflow
- 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¶
- Click the plus button and select "AI Agent"
- The output from the chat trigger automatically populates as input
- The prompt/user message field shows
json.chatInput
Configuring the LLM¶
The first element to attach is the LLM (Large Language Model):
- Under the LLM section, select "OpenAI Chat Model"
- Choose or create OpenAI credentials:
- Click "Create new credential"
- Enter your OpenAI API key (obtained from OpenAI or CodeCloud Keyspace)
- Optionally add organization ID
- Click Save
- 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:
- Under Tools, add "Gmail" node
- Connect Gmail credentials:
- n8n Cloud: Sign in with Google (OAuth2 recommended)
- Self-hosted: Use service account method
- Configure Gmail settings:
- Tool Description: Set automatically (AI decides when to use)
- Resource: Message
- Operation: Send
- Recipient: Let model define
- Subject: Let model define
- Email Type: Text (or HTML for rich formatting)
- Message: Let model define
Configuring the AI Agent Prompts¶
System Message Setup¶
- Go to Options → Add Option → System Message
- 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:¶
- User: "Hello, my name is Mark"
- AI accesses OpenAI and Simple Memory
- Gmail tool NOT used (no email request)
-
Response: "Hello again Mark, what would you like to do today?"
-
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"
-
AI asks for more details: "What would you like the subject and message to be?"
-
User: "Please come up with it yourself"
- AI drafts email with subject, greeting, and body
-
AI asks for confirmation before sending
-
User: "My boss's name is Moonshot"
-
AI updates the draft with correct name
-
User: "Yes, please go ahead and send it"
- Gmail tool is activated
- 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:
- Open node and click "Pin" in top right
- Data becomes fixed for that node
- Every demo/trial run uses pinned data
- Prevents API token consumption during testing
- Unpin before pushing to production
Publishing the Workflow¶
Activating for Production¶
- Toggle "Activate Workflow" at the top
- Warning message: "You can now make calls to your production chat URL"
- Click "Got it"
- Workflow now runs automatically on triggers
Making Chat Publicly Available¶
- Open chat trigger node
- Toggle "Make chat publicly available"
- Copy the generated URL
- Open URL in browser to access public chat interface
- Configure optional settings:
- Authentication requirements
- 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¶
-
Three essential AI agent components: Every AI agent needs an LLM (brain), memory (context), and tools (capabilities).
-
Memory enables context: Without memory, the AI treats each message as a new conversation. With memory, it maintains conversation continuity.
-
AI decides tool usage: When tools are attached with "set automatically" description, the AI intelligently decides when to use them based on user requests.
-
System vs User prompts: System prompts define the agent's role and behavior; user prompts carry the actual request.
-
Fixed vs Expression: Use expressions for dynamic content that changes with user input; fixed values for constants.
-
Pin data during development: Pinning prevents unnecessary API calls and costs while testing.
-
Execute step for iteration: Test individual nodes without running the entire workflow.
-
Production requires activation: Toggle the workflow active for it to respond to real triggers.
-
Public chat access: n8n can generate public URLs for external users to interact with your AI agent.
-
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.