Persist data across node executions

Describe the problem/error/question

How do I persist data across node executions?

I’m building a simple chatbot node that connects to a chat trigger. I need to store previous messages to send to the AI every execution.

I don’t want to use Code nodes or any other nodes. I want it all in one node.

I’ve tried saving to this.getWorkflowStaticData but nothing ever saves.

Please share your workflow

Share the output returned by the last node

who are you
I’m Claude, an AI assistant created by Anthropic. I’m here to help with a wide variety of tasks like answering questions, helping with analysis and research, creative writing, math and coding problems, or just having a conversation. Is there something specific I can help you with today?
are you sure
I don’t have context for what you’re asking me to be sure about, since this appears to be the start of our conversation. Could you clarify what specific topic or claim you’d like me to address? I’m happy to discuss whatever you have in mind and share how confident I am about different aspects of it.

Information on your n8n setup

  • n8n version: 1.117.3
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: WSL

If I understood you correctly, you just need a memory(refer to image). Use Simple Memory to store previous interactions within the workflow. For production you have to use Postgres or Redis.

Note: Chat trigger has sessionIdparameter, which you can use as memory key as follows: {{ $json.sessionId }} or {{ $('When chat message received').item.json.sessionId }}. Simple Memory already uses it in default. For other memories, you have to point it out manually.

Thanks for the reply Mookie.

I’ve used the AI Agent before but that’s not exactly what I’m trying to do. Let me try to explain.

I’m building custom nodes for my company. We offer AI hosting. I want this node to preserve chat history by itself. Use it like a simple chatbot. Just a couple messages until it is refreshed.

You mentioned sessionId. Is there a way to access the chat trigger session data from inside the execute function of my custom node? I want to retrieve the session history and send it to the AI manually so it can generate the next response.

Ah, didn’t realize your were creating a custom node :sweat_smile:

I just checked the nodes base of n8n. They mention memory?: BaseChatMemory more than once in functions. Also here is a code snippet for loading chat history, from the same file:

async function loadChatHistory(
	memory: BaseChatMemory,
	model: BaseChatModel,
	maxTokensFromMemory?: number,
): Promise<BaseMessage[]> {
	const memoryVariables = await memory.loadMemoryVariables({});
	let chatHistory = memoryVariables['chat_history'] as BaseMessage[];

	if (maxTokensFromMemory) {
		chatHistory = await trimMessages(chatHistory, {
			strategy: 'last',
			maxTokens: maxTokensFromMemory,
			tokenCounter: model,
			includeSystem: true,
			startOn: 'human',
			allowPartial: true,
		});
	}

	return chatHistory;
}

You can view the complete file from here.