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) + CalculatorPart 1: LLM Chain vs AI Agent
| LLM Chain | AI Agent | |
|---|---|---|
| How it works | One prompt in, one answer out | Thinks, picks tools, loops until done |
| Path | Fixed. You decide every step | Decided by the model |
| Memory | No | Yes, with a memory node |
| Tools | No | Yes |
| Cost and predictability | Cheaper, very predictable | More flexible, less predictable |
Part 2: Chat Trigger + AI Agent
- New workflow → add When chat message received.
- Add an AI Agent node after it.
- In the Chat Model slot choose Google Gemini and your existing credential (free key from Google AI Studio).
- 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.
- In the Memory slot add Simple Memory.
- Session ID: with the Chat Trigger, n8n fills it in automatically (one memory per chat session).
- Context window length: how many past messages it keeps. Small is faster and cheaper, bigger means longer memory.
Ask again. Now it remembers.
Part 4: Give it a job (system message)
AI Agent → Options → System Message:
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
- In the Tool slot add Google Sheets Tool.
- Operation: Get Row(s). Select the leads spreadsheet from Episode 1.
- 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.
- 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
- Read: your message + system message + memory.
- Decide: answer directly, or call a tool?
- Act: call the tool, read the result.
- Loop: need more? Call another tool (for example Leads Sheet, then Calculator).
- 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
| Problem | Fix |
|---|---|
| Agent never uses the sheet | Improve the tool description and mention the tool by name in the system message |
| Agent makes up leads | Add "Never invent leads" and lower the temperature |
| Forgets the conversation | Check Simple Memory is connected and the session ID is set |
| Wrong dates ("this week") | Add today's date with {{ $now.toISODate() }} |
Practice
- Add a second Google Sheets tool that appends a note to a lead.
- Ask "What percentage of our leads are hot?" and check the logs to see both tools being used.
- 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