Skip to content
TheAgent Ecosystem
AI Agents

Give Your n8n AI Agent Memory (Postgres Chat Memory, Step by Step)

Why your bot forgets everything, what memory actually costs, and how to wire it right

Muhammad Qasim HammadAI-assisted10 min read1,958 words

AI-drafted, reviewed by Muhammad Qasim Hammad on June 17, 2026. See our AI disclosure.

n8n + AI agents: Stop Your n8n Agent Forgetting Everything
Table of contents
  1. What does memory actually do for an n8n AI agent?
  2. Which memory node should you use? (Simple vs Postgres vs Redis)
  3. How much does memory add to your token bill?
  4. How do you set the Session Key correctly?
  5. How do you add memory in n8n, step by step?
  6. Step 1: Open your AI Agent workflow
  7. Step 2: Attach Postgres Chat Memory
  8. Step 3: Set the Session Key
  9. Step 4: Set Context Window Length
  10. Step 5: Test with two related messages
  11. Step 6: Monitor token usage
  12. Where can this go wrong?
  13. What should you set up this weekend?

Your n8n AI agent answers every message as if it has never spoken to the user before, because the underlying model is completely stateless between calls. Adding n8n AI agent memory fixes this by storing the recent conversation and automatically prepending it to each new request, so the agent can follow a real thread.

The symptom is frustrating: a customer asks about their order, the agent answers, then asks a follow-up and the agent asks for the order number again. Every turn starts from scratch.

Before you drag a node onto the canvas, understand what memory does and what it costs.

What does memory actually do for an n8n AI agent?#

Memory in an n8n AI agent stores recent chat turns in a backend, then re-sends them to the model on every new call. The n8n docs describe the stateless model clearly: the LLM has no built-in recall, so n8n injects the context manually, giving the appearance of continuity without any special model capability.

Two parameters control everything: Session Key (which conversation this is) and Context Window Length (how many prior turns to re-send). Get both right and the agent remembers. Get either one wrong and the agent either forgets or leaks one user's conversation into another's.

Memory helps multi-turn tasks: a support chat that refers back to earlier messages, a lead-gen conversation that builds on what the prospect already said, or a Telegram bot tracking a thread across dozens of messages. It does nothing for one-shot tasks like "classify this email" or "extract this JSON." If you are running those, skip memory entirely.

Stat cards showing 1,400 input tokens at window 4, 2,600 at window 10, $0 storage on free tier, and Simple Memory lost on restartKey figures for sizing and budgeting n8n AI agent memory.

Which memory node should you use? (Simple vs Postgres vs Redis)#

Postgres Chat Memory is the right default for production: it writes to an external database and survives restarts, redeploys, and multi-worker setups. Simple Memory stores history inside the workflow process with zero setup, but the history disappears the moment n8n restarts. Redis Chat Memory is the right call when you already run Redis and need low-latency reads at scale.

BackendSetupSurvives restartScales across workersBest for
Simple MemoryNoneNoNoLocal testing only
Postgres Chat MemoryPostgres credentialsYesYesProduction default
Redis Chat MemoryRedis credentialsYesYesHigh-scale / existing Redis
MongoDB Chat MemoryMongoDB credentialsYesYesExisting MongoDB stack

My default for any agent I ship is Postgres Chat Memory on a Supabase free-tier database: $0 storage cost, full SQL inspection of stored conversations, and it survives the restarts that happen every time I update a workflow. n8n self-hosted (Community edition) costs $0 via the fair-code license; n8n Cloud Starter is €20/month if you prefer managed hosting.

Comparison table of n8n memory backends Simple, Postgres, Redis and MongoDB across setup, restart survival, scaling and best useChoose your backend based on durability and scale needs.

How much does memory add to your token bill?#

Every call with memory active re-sends your system prompt, the last N turns, and the new message, so a longer window means more input tokens each time. At Claude Haiku 4.5 prices of $1.00 input / $5.00 output per 1M tokens (as of June 2026), the arithmetic below is straightforward to reproduce.

Assume 500 tokens for the system prompt, 200 tokens per turn (user + assistant), and 100 tokens for the new message. Output is roughly 300 tokens per call.

Context Window LengthInput tokens per callCost per 1,000 calls (Haiku 4.5)Trade-off
4 turns~1,400$2.90Lean; may lose thread on long chats
10 turns~2,600$4.10More coherent; 85% more input tokens

The $2.90 figure: 1.4M input tokens x $1.00 + 0.3M output tokens x $5.00 = $1.40 + $1.50 = $2.90. The $4.10 figure: 2.6M x $1.00 + 0.3M x $5.00 = $2.60 + $1.50 = $4.10. All arithmetic is checkable by hand.

Postgres storage on a free tier is effectively $0. The cost of memory is tokens, not disk. Prompt caching helps with the fixed system prompt, but the growing history is what actually drives the bill up. Start at Context Window Length 4 and raise it only when the agent clearly loses the thread.

For a deeper look at capping the token bill, see the Claude API cost control workflow guide.

Pros and cons of raising n8n Context Window Length: more coherence versus higher token cost and bigger payloadsWeigh coherence gains against token cost before raising the window.

How do you set the Session Key correctly?#

The Session Key tells n8n which bucket of stored history belongs to which user. When you use the built-in Chat Trigger, the sessionId is passed automatically and you can leave the Session Key field on its default. Outside that context, you must map it yourself.

For a Telegram bot, set the Session Key to the incoming message.chat.id. For a WhatsApp agent via Twilio, use the From phone number. For a Webhook, pull whatever stable per-user identifier arrives in the payload.

The Simple Memory docs call this out directly: multiple memory nodes share one instance unless given different session IDs. One wrong setting and your support bot will answer User B with User A's order details.

How do you add memory in n8n, step by step?#

Adding memory to an existing AI Agent node takes about five minutes. Open your workflow, click the AI Agent node, find the Memory slot in the sub-node panel, and connect a memory node. The five steps below map directly to the howToSteps schema so you can follow along in the UI.

Six-step process to add Postgres Chat Memory to an n8n AI Agent: open node, attach memory, set Session Key, window length, test, monitorSix steps from a forgetful agent to a memory-enabled one.

Step 1: Open your AI Agent workflow#

Open the workflow that contains your AI Agent node. If you are building from scratch, add an AI Agent node and connect it to a Chat Trigger.

Step 2: Attach Postgres Chat Memory#

In the AI Agent node, click the Memory sub-node slot. Choose Postgres Chat Memory. Add your Postgres credentials. A Supabase free-tier project gives you a connection string in under two minutes. For a quick test, choose Simple Memory instead, but plan to swap it before going live.

Step 3: Set the Session Key#

Inside the Postgres Chat Memory node, set Session Key. If you are using the Chat Trigger, the {{ $json.sessionId }} expression works automatically. For Telegram or WhatsApp, map it to the per-user chat ID. For a custom Webhook, use whatever stable user identifier arrives in the payload.

Step 4: Set Context Window Length#

Set Context Window Length to 4. This re-sends the last 4 user+assistant pairs with every call, which is enough for most support and lead-gen flows. Raise it only if the agent loses the thread during a real conversation, and watch the input token counts in the execution log as you tune it.

Send a first message, then a follow-up that refers back to it. For the RAG support bot I run, I send "I want to upgrade my plan" followed by "What does that change about my invoice?" A working memory setup answers the second question correctly. If the agent asks for context again, check that the Session Key is populated.

Step 6: Monitor token usage#

Open the execution log, click into the AI Agent node, and read the input token count. Compare runs at window 4 vs window 10. Use the cost table above to decide whether the extra coherence is worth the extra spend per 1,000 calls.

Where can this go wrong?#

Most memory failures fall into four categories, and none of them are bugs in n8n. They come down to your Session Key, your window length, and what you store. Get those three right and memory is reliable; get them wrong and the agent leaks conversations, runs up tokens, or forgets mid-task.

A wrong or empty Session Key either merges all users into one shared conversation or makes the agent forget every previous message. A window that is too large is expensive: at a Context Window Length of 20, you are re-sending 4,500+ input tokens per call, which adds up on a busy day. A window that is too small (1 or 2 turns) loses context on any multi-step task.

The fourth category is one most tutorials skip: stored conversations contain PII. Whatever a user types gets written to your Postgres or Redis store. Apply access controls, set a retention policy, and treat that table like any other database of customer data. The lead-gen agent post and the Google Sheets as database guide both cover the data-hygiene habits worth building early.

Memory also does nothing for one-shot tasks. If your agent classifies a single input and returns, every token in the history window is wasted. Only add memory when the task genuinely requires the agent to refer back to earlier turns.

Decision flow: skip memory for one-shot tasks, Simple Memory for testing, Postgres Chat Memory for production multi-turn agentsUse this flow to pick the right memory backend before you build.

What should you set up this weekend?#

If you have a working n8n agent that forgets everything, you can fix it in one session. Add Postgres Chat Memory to the AI Agent node, connect a Supabase free-tier database, set the Session Key to the per-user ID your trigger already provides, and set Context Window Length to 4. Test with two related messages.

For a production setup, pair this with the self-host n8n on a VPS guide to put the Postgres instance and n8n on the same server, and bookmark the Claude API cost control workflow to add a token-budget guard before the window grows. Memory is not magic. It is a prepend operation with a price tag. Wire it right and keep the window tight.

Frequently asked questions

What is memory in an n8n AI agent?
It is stored chat history that n8n prepends to every new request, so the model can refer to earlier messages. The model itself is stateless; n8n does the storing and the re-sending.
What is the difference between Simple Memory and Postgres Chat Memory?
Simple Memory holds history in the workflow process and loses it on restart. Postgres Chat Memory writes to an external database, so it survives restarts and works across multiple n8n workers.
Does adding memory cost more?
Yes. Every call re-sends the stored history. A 10-turn window sends roughly 2,600 input tokens per call vs 1,400 for a 4-turn window on Claude Haiku 4.5 (as of June 2026, $1.00 per 1M input tokens).
What is the Session Key in n8n memory?
It is the identifier that separates one user's conversation from another's. The Chat Trigger sets it automatically. For Telegram or Webhooks you must set it manually to a stable per-user value like the chat ID.
How many messages should the context window keep?
Start at 4 turns. Raise it only if the agent clearly loses the thread. Every extra turn adds roughly 200 input tokens per call, so keep the window as short as still works.
Does my agent lose memory when n8n restarts?
Only if you use Simple Memory. Postgres Chat Memory, Redis Chat Memory, and MongoDB Chat Memory are external stores, so they survive an n8n restart or redeploy.

Sources

Primary references and vendor documentation used while drafting and reviewing this article.

  1. n8n Docs: Understand Memory in AI Agents
  2. n8n Docs: Simple Memory (Window Buffer) Node
  3. n8n Docs: Postgres Chat Memory Node
  4. n8n Docs: Redis Chat Memory Node
  5. n8n Docs: AI Agent Node
  6. Anthropic Claude Pricing (June 2026)
  7. n8n Pricing
  8. n8n Sustainable Use License

Written by

Muhammad Qasim Hammad
Muhammad Qasim Hammad
AI agents & automationFounder · Cart Gaze LLCPMP-certified PM

Muhammad Qasim Hammad is an AI agent and automation expert and the founder of Cart Gaze LLC (cartgaze.com). He builds product for the love of it: when an idea lands, a working prototype is usually running within hours, built with the same AI agents and automations he sells. He puts his own output at roughly 20× what it was before agents, and the Agentic OS behind this site is the working proof, documented in public with the tools he actually ran and what they really cost.

AI & Automation Services

Want a pipeline like this running in your business?

I'm Qasim — I design and ship AI agents and n8n automations for solo operators and small teams. Tell me what's eating your team's week, and I'll scope a fix.

Related reading