Maintain Conversation History in n8n with Claude Code Session IDs | Alpha | PandaiTech

Maintain Conversation History in n8n with Claude Code Session IDs

Implement persistent memory in n8n workflows by generating UUIDs and using Claude Code's session flags to resume conversations across multiple execution steps.

Learning Timeline
Key Insights

Context Retention Logic

Without the `-r` flag and Session ID, questions like 'Why is one of them down?' will fail because the AI treats every request as a brand new conversation. The Session ID links the interactions.
Prompts

Initial Status Query

Target: Claude Code
how many access points are up right now

Contextual Follow-up

Target: Claude Code
Why is one of them down?
Step by Step

Generating a Session UUID in n8n

  1. Click the '+' button to add a new node.
  2. Search for and select the 'Code' node.
  3. Ensure the language is set to 'JavaScript' in the node settings.
  4. Input a JavaScript function to generate a random UUID (Universally Unique Identifier).
  5. Execute the node to generate the ID variable.

Initiating a Persistent AI Session

  1. Open the configuration for the node executing the Claude Code command.
  2. In the command input field, type your initial prompt (e.g., 'how many access points are up right now').
  3. Append the flag `--session-id` to the end of the command string.
  4. Drag and drop the UUID variable from the previous Code node into the command field after the flag.
  5. Ensure there is a space between the flag and the variable.
  6. Click 'Execute Node' to run the initial query.

Resuming Context in Follow-up Steps

  1. Create or configure a new command node for the follow-up interaction.
  2. Enter a context-dependent prompt (e.g., 'Why is one of them down?').
  3. Append the `-r` (resume) flag to the command string.
  4. Map the *same* UUID variable from the initial Code node after the `-r` flag.
  5. Click 'Execute Node' to process the prompt using the history from the previous step.

More from Build & Deploy Autonomous AI Agents

View All