/n8n-tutorials

How to pass conversation history to Anthropic Claude reliably in n8n?

Learn how to reliably pass conversation history to Anthropic Claude in n8n with clear steps for stable, context-aware automations.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to pass conversation history to Anthropic Claude reliably in n8n?

To pass conversation history to Claude reliably in n8n, you should explicitly build and control the conversation array yourself (usually inside a Function node) and then send that array as the messages field to the Anthropic API node or an HTTP Request node. Do not rely on “item chaining” or storing entire history in a single string field unless you intentionally format it that way. Keep the history small, store it in a predictable structure, and trim it before sending to Claude. This avoids runaway payload sizes, unexpected formatting, and memory/time‑limit problems in real n8n workflows.

 

Why this works

 

Claude expects a structured message array like:

  • { role: "user", content: "..." }
  • { role: "assistant", content: "..." }

n8n workflows don’t maintain “conversation state” automatically; each node only receives the current item JSON. So you must maintain the entire chat history yourself (usually an array inside a field like chatHistory). Each turn, you append the new message, trim the array if needed, and then send that full array to Claude.

 

Core pattern you can reuse in real production

 

The safest, most production-proof approach is:

  • Store your running conversation history in a predictable array (inside a Function node or external DB).
  • Append the current user message.
  • Optionally trim old messages (to avoid hitting Claude token limits).
  • Pass that cleaned array to the Claude node.
  • Append Claude’s reply to the history and store it again.

 

// Function node: Build/Update conversation history
// Incoming: userMessage (string), chatHistory (array) or empty
// Output: { messages: [...historyIncludingNewUserMsg] }

const userMessage = $json.userMessage;
let history = $json.chatHistory || [];   // Load previous history if exists

// Append the new user message
history.push({
  role: "user",
  content: userMessage
});

// Optional trimming for safety
// e.g., keep only last 10 messages
history = history.slice(-10);

// Return for next node (Claude API)
return [
  {
    json: {
      messages: history
    }
  }
];

 

Sending it to Claude from n8n

 

You can use the official Anthropic node or a generic HTTP Request node. The critical part is sending the messages array as-is.

Here’s the payload structure when using HTTP Request:

{
  "model": "claude-3-5-sonnet-latest",
  "max_tokens": 1024,
  "messages": {{ $json.messages }}
}

In the Anthropic node, you put the same array into the Messages field using an expression like:

// Inside field:
// {{$json["messages"]}}

 

Capturing Claude’s reply and storing history

 

Claude returns something like:

{
  "content": [
    { "type": "text", "text": "Hello! ..." }
  ]
}

So you need another Function node right after Claude:

// Function node: Append Claude's reply to history and store
const history = $json.messages;       // The same array we sent
const reply = $json.content[0].text;  // Claude’s actual text

history.push({
  role: "assistant",
  content: reply
});

return [
  {
    json: {
      chatHistory: history,
      assistantReply: reply
    }
  }
];

 

Important production‑level considerations

 

  • Limit the history size. Token costs and API request sizes grow fast. Slice the array regularly.
  • Consider storing history externally (Postgres, Redis, or n8n’s built-in Data Store). This avoids losing context if the workflow restarts.
  • Avoid building history as one giant string. Claude performs better and more predictably with structured role messages.
  • Watch for n8n memory/time limits if your history grows too large or your execution is long-running.
  • Always validate what you pass to the API. A malformed array (e.g., strings instead of objects) will cause cryptic Anthropic errors.

 

Final practical explanation

 

n8n does not automatically remember anything. To pass conversation history to Claude, you manually store the history array, append to it each turn, trim it, and send that whole array to Claude. This is the same pattern used in real production chatbots running on n8n today, and it’s stable, predictable, and easy to maintain.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022