How can I insert a message list JSON into the agent and make it as the context?

Describe the problem/error/question

How can I insert a message list JSON into the agent and make it as the context?

I just want to pass it to the agent as a temporary message context.

hey @blowsnow Welcome to the n8n community!

I think if you just use the simple memory option and connect it to the ai agent, it will retain the context of the conversation you are having with the ai agent.

Let me know if this helps or if i’m misunderstanding the problem.

@blowsnow To pass a message list JSON as temporary context to an AI Agent in n8n, you have a few options:

Option 1: Use the Chat Memory Node

The Window Buffer Memory node lets you inject a conversation history:

  1. Add a Window Buffer Memory node before your AI Agent

  2. In the memory node, use the “Session ID” to create unique conversation contexts

  3. Pass your message list JSON through an Edit Fields node to format it as:

    {
      "chatHistory": [
        {"role": "user", "content": "Previous message"},
        {"role": "assistant", "content": "Previous response"}
      ]
    }
    
    
  4. Connect this to your AI Agent node

Option 2: Include in System Message

Add your message context directly in the AI Agent’s system prompt:

  1. In your AI Agent node, go to Options → System Message

  2. Use an expression to inject your JSON:

    Here is the conversation context:
    {{ $json.messageHistory }}
    
    Use this context to inform your responses.
    
    

Option 3: Use a Tool with Context

Create a custom tool that provides the context:

  1. Add a Code Tool to your agent

  2. Have it return your message list when called

  3. The agent can reference this context as needed

Hope this helps!

1 Like

Hi @blowsnow Welcome!
You can do something like this:

This can help you give JSON as a context to your AI Agent, hope this helps.

1 Like

I think Chat Memory Manager node is what you are looking for:

You could try adding a Chat Memory Manager node with an If node before the agent node, depending on your logic..