AI agents

n8n AI Agent with Postgres chat memory

A Chat Trigger, an AI Agent on Claude Sonnet, and Postgres Chat Memory keyed on the incoming session id so conversations survive an n8n restart.

Trigger
Chat Trigger, the n8n chat window
Nodes
4
Requires
Anthropic, Postgres
Category
AI agents
From the course
AI Agents in n8n
Published
2026-09-10

Node types used

  • Chat Trigger
  • AI Agent
  • Anthropic Chat Model
  • Postgres Chat Memory
Workflow diagram: n8n AI Agent with Postgres chat memoryai_languageModelai_memoryWhen chat message rec…When chat message receivedAI AgentAnthropic Chat ModelPostgres Chat Memory
TriggerAI

What it does

The first agent everyone builds in n8n uses Simple Memory, which lives in the n8n process. Restart the container and every conversation is gone. Worse, in the default configuration all callers share one memory, so two people chatting at once read each other messages. Both problems are one sub-node swap.

A Chat Trigger opens the built-in chat window. An AI Agent node handles the turn, with a system message that tells it to answer in at most four sentences unless asked for more and to say plainly when it does not know something rather than guessing at names, dates, prices or URLs. An Anthropic Chat Model sub-node supplies the model, set to Claude Sonnet 4.6. A Postgres Chat Memory sub-node stores the history in a table called chat_support_agent, with the session id taken from the incoming request rather than a fixed value, and a context window of five turns.

It has no tools and no retrieval. The agent can only answer from the model and the last five turns of the conversation, which is the point: it is the memory wiring on its own, before anything else is added.

Node by node

  1. 1

    When chat message received A Chat Trigger that opens the n8n chat window and passes each message, along with its session id, into the agent.

  2. 2

    AI Agent Runs the turn with promptType auto, so the chat message is taken as the prompt. Its system message caps answers at four sentences unless more is asked for and instructs it to admit what it does not know instead of inventing names, dates, prices or URLs.

  3. 3

    Anthropic Chat Model Attached on the ai_languageModel connection. Set to claude-sonnet-4-6 with default options.

  4. 4

    Postgres Chat Memory Attached on the ai_memory connection. Writes to the table chat_support_agent, takes sessionIdType fromInput so each chat session gets its own thread, and replays a context window of five previous turns into each request.

Setup after import

  1. 1

    Create an Anthropic credential and select it on Anthropic Chat Model.

  2. 2

    Create a Postgres credential and select it on Postgres Chat Memory. In current n8n versions the node creates its own table on first use. If the first run complains that chat_support_agent is missing, create the table or change the table name on the node to one that exists.

  3. 3

    Open the chat window from the Chat Trigger node and send a message, then restart n8n and send another. The agent should still know what you said.

  4. 4

    Change the model on Anthropic Chat Model if you would rather use a different one. It is one dropdown and nothing else in the workflow depends on it.

  5. 5

    Raise or lower the context window of five if your conversations need more or less history. Every extra turn is sent with every request and is paid for on every request.

Limits

  • No tools, no retrieval, no structured output.

  • A context window of five turns means the agent forgets anything said earlier in a long conversation.

  • Every session id gets its own thread, so anyone who can reach the chat endpoint can start one.

  • Nothing logs cost or token usage.

Download the workflow

The 4 node export as n8n reads it. Credential ids, personal values and real endpoints are replaced with placeholders, so nothing here can reach an account that is not yours. No email, no account.

Download n8n-ai-agent-postgres-chat-memory.json

Get help with this in the community

If the import fails, if a node errors on the first run, or if you want to point this at a service it does not cover yet, post it in the free House of Loops community. Say which template it is and paste the error. Shannon Atkinson answers, and the answer stays there for the next person who hits the same thing.

Ask in the community