Learn how to reliably pass conversation history to Anthropic Claude in n8n by structuring API requests, managing message arrays, and maintaining context for seamless multi-turn interactions.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
To pass conversation history to Anthropic Claude reliably in n8n, you need to structure your requests properly by maintaining and including previous messages in each new API call. This ensures context continuity as your workflow progresses, enabling Claude to reference earlier parts of the conversation.
Understanding the Anthropic Claude API Message Structure
Before diving into the n8n implementation, it's important to understand how Claude's API handles conversations. Claude uses a structured message format where each message has a "role" (either "user" or "assistant") and "content". To maintain conversation history, you need to append new messages to the existing array of messages with each request.
Step 1: Setting Up Your n8n Workflow
First, create a new workflow in n8n:
Step 2: Add a Trigger Node
Add a node that will trigger your conversation:
Step 3: Set Up the Initial Claude Request
Add the HTTP Request node to make your first call to Claude:
{
"model": "claude-3-opus-20240229",
"max\_tokens": 1000,
"messages": [
{
"role": "user",
"content": "Hello, Claude. This is our first interaction."
}
]
}
Step 4: Store the Conversation History
After the initial request, add a Set node to store the conversation:
[
{
"role": "user",
"content": "Hello, Claude. This is our first interaction."
},
{
"role": "assistant",
"content": "={{ $node["Initial Claude Request"].json.content }}"
}
]
This stores both the initial user message and Claude's response in an array.
Step 5: Create a Function to Handle Follow-up Messages
Now, create a path for handling follow-up messages:
// Get the existing conversation history
const conversationHistory = $input.item.json.conversationHistory || [];
// Get the new user message (this would come from your input mechanism)
// For this example, we'll hardcode it, but in a real scenario, this would come from user input
const newUserMessage = {
"role": "user",
"content": "Tell me more about this conversation feature."
};
// Add the new message to conversation history
const updatedHistory = [...conversationHistory, newUserMessage];
// Prepare the request body for Claude
const requestBody = {
"model": "claude-3-opus-20240229",
"max\_tokens": 1000,
"messages": updatedHistory
};
// Return the updated history and request body
return {
conversationHistory: updatedHistory,
requestBody: requestBody
};
Step 6: Make the Follow-up Request to Claude
Add another HTTP Request node to send the follow-up message:
Step 7: Update the Conversation History
After receiving Claude's follow-up response, update the conversation history:
={{
// Get the previous conversation history and add Claude's latest response
[
...$node["Prepare Follow-up Request"].json.conversationHistory,
{
"role": "assistant",
"content": $node["Follow-up Claude Request"].json.content
}
]
}}
Step 8: Creating a Loop for Continuous Conversation
To create a loop that allows for multiple back-and-forth exchanges:
Alternatively, you can use webhook responses to create an interactive chatbot:
Step 9: Implement Dynamic User Input
For a real conversation, you'll want to accept dynamic user input rather than hardcoded messages:
// Get the existing conversation history
const conversationHistory = $input.item.json.conversationHistory || [];
// Get the new user message from the webhook payload or other input
const newUserMessage = {
"role": "user",
"content": $input.item.json.userMessage || "Default fallback message"
};
// Add the new message to conversation history
const updatedHistory = [...conversationHistory, newUserMessage];
// Prepare the request body for Claude
const requestBody = {
"model": "claude-3-opus-20240229",
"max\_tokens": 1000,
"messages": updatedHistory
};
// Return the updated history and request body
return {
conversationHistory: updatedHistory,
requestBody: requestBody
};
Step 10: Handling Conversation Storage and State Management
For maintaining conversations across multiple sessions or workflow runs:
This approach lets you maintain conversations even if the workflow is restarted or across different sessions.
Step 11: Optimizing for Token Usage
As conversations grow longer, you may need to manage token usage:
// Get the current conversation history
const conversationHistory = $input.item.json.conversationHistory || [];
// If the history is longer than a certain number of messages, trim it
// Keep the most recent N messages (e.g., 10)
const maxMessages = 10;
const trimmedHistory = conversationHistory.length > maxMessages
? conversationHistory.slice(conversationHistory.length - maxMessages)
: conversationHistory;
// Return the trimmed history
return {
conversationHistory: trimmedHistory
};
Step 12: Error Handling and Retries
Add error handling to make your conversation flow more robust:
Complete Workflow Example
Here's a streamlined example of the complete workflow for a webhook-based conversation:
This structure creates a reliable way to maintain conversation history with Claude through n8n workflows.
Final Tips for Reliable Claude Conversations in n8n
By following these steps, you'll create a robust system for maintaining conversation history with Claude in your n8n workflows, enabling contextual and coherent exchanges across multiple interactions.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.