How to add memory to Claude chatbot
Quick answer
To add memory to a
Claude chatbot, maintain the conversation history by appending previous user and assistant messages to the messages parameter in each API call. Use the anthropic Python SDK to send the full chat history with the messages list and the system prompt to preserve context across turns.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the anthropic Python SDK and set your API key as an environment variable.
pip install anthropic>=0.20 Step by step
Use the anthropic.Anthropic client to create a chat completion with memory by passing the full conversation history in the messages parameter. Append each new user and assistant message to this list to maintain context.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Initialize conversation history
conversation = []
# System prompt defining assistant behavior
system_prompt = "You are a helpful assistant."
# Function to send a message and update memory
def chat_with_memory(user_input):
global conversation
# Append user message
conversation.append({"role": "user", "content": user_input})
# Call Claude with full conversation history
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=500,
system=system_prompt,
messages=conversation
)
assistant_reply = response.content[0].text
# Append assistant message
conversation.append({"role": "assistant", "content": assistant_reply})
return assistant_reply
# Example usage
print(chat_with_memory("Hello, who won the World Series in 2020?"))
print(chat_with_memory("Can you remind me what you just said?")) output
The Los Angeles Dodgers won the World Series in 2020. The Los Angeles Dodgers won the World Series in 2020.
Common variations
You can implement asynchronous calls using asyncio with the Anthropic SDK or use different Claude models like claude-3-5-haiku-20241022 for varied performance. Also, consider truncating or summarizing long histories to stay within token limits.
import asyncio
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
conversation = []
system_prompt = "You are a helpful assistant."
async def chat_with_memory_async(user_input):
global conversation
conversation.append({"role": "user", "content": user_input})
response = await client.messages.acreate(
model="claude-3-5-haiku-20241022",
max_tokens=500,
system=system_prompt,
messages=conversation
)
assistant_reply = response.content[0].text
conversation.append({"role": "assistant", "content": assistant_reply})
return assistant_reply
# Example async usage
async def main():
reply1 = await chat_with_memory_async("Hello!")
print(reply1)
reply2 = await chat_with_memory_async("What did I just say?")
print(reply2)
asyncio.run(main()) output
Hello! How can I assist you today? You just said: Hello!
Troubleshooting
- If you hit token limits, truncate or summarize older messages to keep the conversation within model constraints.
- If responses seem out of context, ensure the full conversation history is passed correctly in the
messageslist. - Check your
ANTHROPIC_API_KEYenvironment variable is set and valid to avoid authentication errors.
Key Takeaways
- Maintain conversation history in a list of messages to add memory to Claude chatbot.
- Pass the full message history with each API call using the
messagesparameter andsystemprompt. - Use async calls or different Claude models for flexibility and performance.
- Manage token limits by truncating or summarizing conversation history.
- Always set and verify your
ANTHROPIC_API_KEYenvironment variable.