Skip to content
Lesson 2 of 840 min video

Build an AI Agent in n8n with Memory + Tools

Turn the lead workflow into Lead Desk, a chat agent that remembers the conversation, follows a system message and uses Google Sheets and a calculator as tools.

Series: Road to AI Automation · Episode 2 · Level: Beginner → Intermediate · Time: about 40 minutes to follow along

Before this: Episode 1. We reuse its Google Sheet of leads.

What you will build

Lead Desk, an AI agent you can chat with. It:

  • remembers the conversation (memory),
  • follows clear rules (system message),
  • reads your real leads from Google Sheets and does math (tools).

You will be able to ask "Which hot leads do we have? Draft a follow-up for her." and the agent works out the steps itself.

Chat Trigger → AI Agent
                 ├─ Chat Model:  Google Gemini
                 ├─ Memory:      Simple Memory
                 └─ Tools:       Leads Sheet (Google Sheets) + Calculator

Part 1: LLM Chain vs AI Agent

LLM ChainAI Agent
How it worksOne prompt in, one answer outThinks, picks tools, loops until done
PathFixed. You decide every stepDecided by the model
MemoryNoYes, with a memory node
ToolsNoYes
Cost and predictabilityCheaper, very predictableMore flexible, less predictable
Rule of thumb: same steps every time → chain. Steps depend on what the user asks → agent.

Part 2: Chat Trigger + AI Agent

  1. New workflow → add When chat message received.
  2. Add an AI Agent node after it.
  3. In the Chat Model slot choose Google Gemini and your existing credential (free key from Google AI Studio).
  4. Click Open chat and say hi.

It replies, but it has no memory, no rules and no access to your data yet.

Part 3: Memory

Test without memory: say "My name is Hassan", then ask "What's my name?". It forgets.

  1. In the Memory slot add Simple Memory.
  2. Session ID: with the Chat Trigger, n8n fills it in automatically (one memory per chat session).
  3. Context window length: how many past messages it keeps. Small is faster and cheaper, bigger means longer memory.

Ask again. Now it remembers.

Remember the session ID idea. In Episode 3 (Telegram) we set it ourselves so each user gets private memory.

Part 4: Give it a job (system message)

AI Agent → Options → System Message:

prompt
You are Lead Desk, an assistant for an AI agency.

- Use the "Leads Sheet" tool for any question about leads.
- Use the calculator for any math.
- Never invent leads. If unsure, say so.
- Keep replies short and friendly.

Today is {{ $now.toISODate() }}

Why each part matters:

  • Role: the agent knows who it is.
  • When to use each tool: agents use tools much more reliably when told when.
  • "Never invent": reduces made-up answers.
  • Today's date: language models don't know the date. The expression adds it.

Part 5: Tools

  1. In the Tool slot add Google Sheets Tool.
  2. Operation: Get Row(s). Select the leads spreadsheet from Episode 1.
  3. Give it a clear name and description, for example:
Name: Leads Sheet
Description: Reads all leads with name, email, category, reason and reply.
Use this to answer any question about leads.
  1. Add the Calculator tool.

Test in the chat:

You:   Which hot leads do we have?
Agent: [uses Leads Sheet] One hot lead: Sara Khan, she wants a chatbot for 3 clinics.
You:   Draft a follow-up email for her.
Agent: Hi Sara, thanks again for reaching out about your clinics chatbot...

You never repeated "Sara". Memory knew who you meant.

Part 6: How the agent thinks

  1. Read: your message + system message + memory.
  2. Decide: answer directly, or call a tool?
  3. Act: call the tool, read the result.
  4. Loop: need more? Call another tool (for example Leads Sheet, then Calculator).
  5. Reply: answer, and save the turn to memory.

After a run, open the agent's logs to see every tool call. This is the first place to look when an agent misbehaves.

Pro tips

  • Tool descriptions are prompts. Vague descriptions are the top reason agents ignore or misuse tools.
  • Fewer tools. Start with 2 to 4.
  • Max iterations (agent options) caps loops so a confused agent can't burn your quota.
  • Lower temperature for factual business tasks.
  • Test tricky questions, like a lead that doesn't exist. A good agent says it can't find it.

Troubleshooting

ProblemFix
Agent never uses the sheetImprove the tool description and mention the tool by name in the system message
Agent makes up leadsAdd "Never invent leads" and lower the temperature
Forgets the conversationCheck Simple Memory is connected and the session ID is set
Wrong dates ("this week")Add today's date with {{ $now.toISODate() }}

Practice

  1. Add a second Google Sheets tool that appends a note to a lead.
  2. Ask "What percentage of our leads are hot?" and check the logs to see both tools being used.
  3. Give the agent a stricter tone (formal, max 3 sentences) and compare answers.

Next episode: put this agent on Telegram as a live bot that anyone can message, with private memory for each user.

Key takeaways

  • Use a chain when the steps are always the same, an agent when they depend on the question
  • Simple Memory keeps the conversation; the session ID decides whose memory it is
  • A system message gives the agent a role, rules and when to use each tool
  • Tool names and descriptions are prompts: clear ones make the agent use tools reliably