n8n AI Agent Memory: Setup & Fixes
Updated 2026-11-11
By default, an n8n AI Agent node has no memory: each execution starts fresh. You fix that by attaching a memory sub-node. The most common bug is not the memory node itself but the session key. Pair memory with channel guides such as Connect an AI agent to Slack, WhatsApp, or HubSpot in n8n so each user keeps their own thread.
Prerequisites
- n8n (Cloud or self-hosted), a recent 1.x version
- An AI Agent workflow with a chat model attached (OpenAI, Anthropic, etc.)
Steps
1. Pick a memory type
| Node | Stores data in | Use it when |
|---|---|---|
| Simple Memory (formerly Window Buffer Memory) | n8n process memory | Testing, single-instance setups. Lost on restart, not shared in queue mode (n8n warns against using Simple Memory when the instance runs in queue mode). |
| Postgres Chat Memory | Postgres table | Production, survives restarts |
| Redis Chat Memory | Redis | Production, fast, optional TTL |
There are also memory nodes for other stores (e.g. MongoDB, Zep, Motorhead) depending on your n8n version.
2. Attach the memory node
- Open the AI Agent node’s canvas. Below it are connectors for Chat Model, Memory and Tool.
- Click + under Memory and choose Simple Memory (or Postgres Chat Memory).
- Set Context Window Length, the number of past interactions to keep (e.g.
10).
3. Set the session key (the important part)
The session key decides whose conversation is loaded.
- With the Chat Trigger (n8n’s built-in chat), the default Connected Chat Trigger Node uses
sessionIdautomatically. - With any other trigger (Telegram, WhatsApp, Webhook, Slack), switch Session ID to Define below and use an expression that is unique per user, for example:
Telegram: {{ $('Telegram Trigger').item.json.message.chat.id }}
Webhook: {{ $json.body.user_id }}
Slack: {{ $('Slack Trigger').item.json.channel }}
Without this, every user shares one memory, or memory is empty on each run.
4. Use Postgres for production
- Add a Postgres Chat Memory node instead of Simple Memory.
- Create a Postgres credential.
- Leave Table Name at the default (
n8n_chat_histories) and n8n creates it on first run. - Use the same session key expression as above.
5. Test
Open the chat panel, say “My name is Sam”, then ask “What’s my name?” In the execution log, the memory node should show the earlier messages being loaded.
Common errors (n8n AI agent memory not working)
Agent forgets everything between messages: the session key is different on each run (e.g. uses an execution ID) or empty. Check the memory node’s input in the execution log.
No session ID found: you’re not using the Chat Trigger but left Session ID on Connected Chat Trigger Node. Switch to Define below.
Memory works in the editor, not in production (queue mode): Simple Memory lives in one worker’s RAM. Use Postgres or Redis memory.
Memory leaks between users: the session key is a constant. Make it per user or per chat.
Context gets too long and costs spike: lower Context Window Length or cap agent steps the way Fix: AI agent stuck in a loop describes for code-first agents.
FAQ
Why does my n8n AI agent forget the previous message?
The session key is empty or changes on every run. If the trigger is not the Chat Trigger, set Session ID to Define below and use an expression that stays the same for one user.
Which memory node survives a restart?
Postgres Chat Memory or Redis Chat Memory. Simple Memory lives in the n8n process. It is lost on restart and it is not shared in queue mode.
What table does Postgres Chat Memory use?
The default table name is n8n_chat_histories. n8n creates it on the first run.
Related guides
- Connect an AI agent to HubSpot with n8n and Connect an AI agent to Stripe with n8n
- Connect an AI agent to Slack and Connect an AI agent to WhatsApp
- Build an AI agent that books meetings (n8n scheduling pattern)
- Build an agent that runs on a schedule (scheduled digests with memory)
- What is an AI agent? (memory in the agent loop)
- OpenAI Agents SDK: function tools (code-first alternative)
- Connect an AI agent to Telegram
- Fix: AI agent stuck in a loop
- Connect an AI agent to Gmail