Chatbot CANT access Chat Memory History

Hi everyone,

I’m attaching my workflow scenario for context. I want to achieve a setup where the entire chat history is passed to the AI when generating responses.
Currently, every time I send a new message through the WhatsApp module, it starts a new conversation, ignoring the previous context of the chat. I want to change that so the AI can access and respond based on the full chat history. Any tips or guidance would be greatly appreciated!

Thanks!

Here is the workflow. Only the Chat Memory is important.

  • n8n version: 1.69.2
  • Database (default: SQLite): QLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own, main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): self-hosted in google cloud
  • Operating system: Windows10
1 Like

I’m looking for an answer to the same question.

You should be able to change your postgress chat memory to do this.

Change it from Session ID: Connected Chat Trigger Node to Session ID: Define below
Then have some logic between the trigger and agent to find/generate an id key.

Something to note here is it wouldn’t grab anything before you implimented that logic


If possible, you could also fetch the previous messages with the WhatsApp api between the trigger and agent and pass that in, but an not sure if that operation is available.

1 Like

would have to set an ID for the manual chat trigger as well though right? Wouldnt know how to do that

Do you mean for testing purposes?

If you want to use the n8n chat trigger, you just need to add that logic into whatever ID creation/fetching system you make.

The way the chat memory tool works is by accessing a field {{ $json.sessionId }}
So you can access that field before it gets to the agent node

1 Like

@liam Well ultimately what I want to do is store the entire chat that has come from one number (not per session meaning as soon as some time passes or what not) just like within a ChatGPT chat where the entire chat is given as context until the token window is maxed out.

I am not familiar with the WhatsApp api, but look and see if they have an api method for fetching an entire conversation and use the http node to do that.

Otherwise, you will need to store it as the conversation happens one message at a time with your own logic whenever you receive a message or send a message

Why don’t you use phone number as the session ID for consistency?

1 Like

where would I put the phone number as ID input then? The SQL has no placeholder

The Postgres Chat Memory node by default expects sessionId to contain the ID. You can either switch to “Define below” option and point to the WhatsApp property that has the number (no idea if it does as I never used WhatsApp API either) or use Set node in between WhatsApp and AI Agent insuring sessionId is present there and contains the phone number in additions to another property that holds the text to be passed to AI Agent.

1 Like

I now selected define below and used json.id and after each of the 3 blocks used a set variables block to define the id. The first image is from the chat input within N8N. THe last 2 are from the schedule trigger - I need that one otherwise if I don’t use the Supabase account for 2 weeks it deactivates on the free plan - this in theory is a cool work around.

I only fear that there will be issues since it waits for the inputs of multiple paths here or is this not a problem? Has been in the future.

Trigger on Chat input
Screenshot 2025-01-29 092558


not sure what you mean here, can you elaborate please?

As long as you have the sessionId defined, it shouldn’t be an issue I believe

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.