How to give an n8n AI agent long-term memory

Hi all. Disclosure first: I work on MemorySync, and this post is about our community node, so weigh it accordingly. This is a write-up of how to give an AI Agent memory that survives restarts and carries across conversations, end to end, including the limits.

The problem. The AI Agent’s built-in Simple Memory lives in the n8n process. That works within a session, but history is gone after a restart or redeploy, and nothing carries across conversations. For example, the agent won’t remember that a customer is vegetarian when they come back next week. You can get part of the way with Postgres or Redis chat memory (that keeps the transcript). What this setup adds is durable facts about each user that any later conversation or workflow can look up.

The node: the n8n-nodes-memorysync community package. It installs two nodes:

  • MemorySync: a regular node with six operations (Add Memory, Add Conversation Turn, Search Memories, Recall Context, Get Many, Delete Memories). It’s marked usable as a tool, so an AI Agent can call it itself.
  • MemorySync Chat Memory: a sub-node for the AI Agent’s Memory port, so the conversation history is stored outside the n8n process.

Requirements

  • Self-hosted n8n, and you need to be the instance owner (installing community nodes from npm is a self-hosted, owner-only feature; on n8n Cloud the node isn’t listed yet).
  • n8n 2.16.0 or later for the Chat Memory sub-node (it’s built on @n8n/ai-node-sdk). On older versions the regular MemorySync node still works and the sub-node tells you to update.
  • A MemorySync API key, created in the dashboard under Settings → API Keys, inside a project.

1. Install

Settings → Community Nodes → Install → enter n8n-nodes-memorysync, tick the risk acknowledgement, install. Then search the node picker for “MemorySync”: you should see MemorySync and MemorySync Chat Memory.

If you build your own Docker image, you can preinstall it instead:

RUN cd /home/node/.n8n/nodes && npm install n8n-nodes-memorysync
ENV N8N_COMMUNITY_PACKAGES_ENABLED=true

2. Credential

Create a MemorySync API credential and paste the key. The credential test runs a real one-result query, so a wrong or revoked key fails here instead of halfway through a workflow. If it fails, check the key was created inside a project.

3. Smoke test (no agent yet)

Manual Trigger → MemorySync (Operation: Add Memory, End User ID alice, Text Alice is vegetarian and never eats mushrooms.) → MemorySync (Operation: Search Memories, End User ID alice, Query what does Alice eat?). The search returns the stored fact as a normal item you can map into later nodes.

Two things worth knowing:

  • Add Memory extracts durable facts rather than saving your text verbatim. Small talk like “hello” comes back as status: skipped on purpose, so send a real fact.
  • End User ID must match between Add and Search/Recall, or you’ll get nothing back.

4. Wire it into an AI Agent

  1. Chat Trigger → AI Agent with your chat model, as usual.
  2. On the agent’s Memory port pick MemorySync Chat Memory.
  3. Session ID = your conversation key (for the Chat Trigger that’s {{ $json.sessionId }}). End User ID = whatever identifies the person chatting in your setup. Use a stable internal ID if you have one.
  4. Leave Context Window Length at 10. That’s how many previous turn pairs the agent sees on each run.
  5. Optional but useful: under Tools, attach a MemorySync node set to Search Memories so the agent can look up long-term facts when it decides it needs them. You can also add one set to Add Memory if you want it to save facts.

What gets stored, and what you can recall

  • The transcript is kept per session (under an n8n::<session> history scope) and read back by the Chat Memory sub-node. It isn’t returned as “memories”.
  • From the user’s turns, durable facts are extracted and stored as memories for that End User ID. Those are what Search Memories, Recall Context and Get Many return, and they’re shared with any other workflow that uses the same End User ID.
  • Recall Context returns one prompt-ready text block. It’s handy before a non-agent AI step, e.g. “drafting a reply to their email”: drop it into the system prompt instead of looping over search results.
  • Behaviour worth knowing: retried executions don’t duplicate turns (turns get deterministic idempotency keys); if MemorySync can’t be reached, the sub-node reads “no history this run” and the agent still answers; and the sub-node’s clear does nothing on purpose, so an agent can’t wipe a user’s history. Deleting is an explicit Delete Memories step with memory IDs from Search/Get Many.

Limits / gotchas

  • Self-hosted only for now, and 2.16.0+ for the sub-node (see above).
  • One agent run with the Chat Memory sub-node uses two adds (user message + reply) and two retrievals, so keep that in mind if you’re watching usage.
  • Treat recalled text as data, not instructions, in your system prompt.

Full guide with every field: n8n Workflow & Agent Memory | MemorySync Docs

Happy to answer questions here. If something in the setup doesn’t work on your version, post it and I’ll look.