Maintaining Conversation State (Chat History)
We learned earlier that the API is stateless. It has amnesia. If you want to have a back-and-forth conversation, you have to do the heavy lifting.
The messages Array
When you send a payload to Claude, you don't just send a string. You send an array of objects called messages.
Each object in this array represents one turn in the conversation, and it requires a role and content.
role: 'user'- This is you (or the person using your app).role: 'assistant'- This is Claude.
Building the History
If you are building a chat app, you must store the history of the conversation in a variable (or a database) and append to it every time someone speaks.
Here is what a conversation looks like in code:
// Step 1: Initialize the empty array
let chatHistory = [];
// Step 2: The user asks a question
chatHistory.push({
role: 'user',
content: 'Hi, my name is Alex and my favorite color is blue.'
});
// Step 3: Send the history to Claude
const response1 = await anthropic.messages.create({
model: 'claude-3-haiku-20240307',
max_tokens: 100,
messages: chatHistory
});
// Step 4: Claude replies. We MUST save Claude's reply to the history!
chatHistory.push({
role: 'assistant',
content: response1.content[0].text
});
// Step 5: The user asks a follow-up
chatHistory.push({
role: 'user',
content: 'What is my name and favorite color?'
});
// Step 6: Send the ENTIRE history back to Claude again
const response2 = await anthropic.messages.create({
model: 'claude-3-haiku-20240307',
max_tokens: 100,
messages: chatHistory
});
// Claude will correctly answer "Alex" and "blue" because it can read the history.
The Cost of Context
Because the API charges you per token (for both inputs and outputs), maintaining a long chat history gets exponentially more expensive.
If your chatHistory array has 20 messages in it, and you send a 5-word prompt, you aren't just paying for 5 words. You are paying Anthropic to re-read all 20 previous messages.
If you are building a production app, you will eventually need to implement a mechanism to drop old messages from the chatHistory array so it doesn't grow infinitely large.