Error in my memory nodes

Describe the problem/error/question

when my ai agent uses any tool to do anything the memory node gives me an error that is the key field is undefined btw i am not using a chat trigger because my trigger is a webhook and my workflow is a chatbot on instagram. so summary is when i send a message that doesnt require using any tools the memory node works fine but when it does use a tool the key field become undefined btw i am putting the timestamp in the key field and i also tried putting the sender id that the webhook sends but still the same error and this error happened to me in both postgres memory node and in the simple memory.

What is the error message (if any)? Key parameter is empty

Provide a key to use as session ID in the ‘Key’ parameter or use the ‘Connected Chat Trigger Node’ option to use the session ID from your Chat Trigger

Please share your workflow

Share the output returned by the last node

error

Information on your n8n setup

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

Welcome @amazingzizo to our community! I’m Jay and I am a n8n verified creator.

The problem is that inside tool execution, the memory node loses the session context because it can’t access the parent workflow’s node data the same way. Instead of referencing a node in the Key field, hardcode a stable session ID from your webhook payload using {{ $('Webhook').first().json.entry[0].messaging[0].sender.id }} - adjust the path to match your actual Instagram payload structure. Using .first() is important here because during sub-tool execution the current item context may be different. If that still fails, check your webhook payload structure in the Webhook node output to confirm the exact path to the sender ID.

thank u for replying to me but unfortunately it didn’t work and now it even gives me undefined every time not just when using the tools

@amazingzizo when Memory runs INSIDE the AI Agent’s tool execution, $(‘Webhook’) doesnt resolve because the agent’s input is whats in scope, not the workflow context. fix is to extract the session ID in a Set node BEFORE the agent runs, then reference it via $json. drop this Set node between ur Webhook and AI Agent:

then change ur Memory Key field to just: {{ $json.sessionId }}

the agent forwards its input to the memory tool, so sessionId stays accessible inside tool execution.

it worked thank u very much i appreciate the help thx

Your welcome! Feel free to mark one of the posts as the solution, and have a great day!