Short-Term Conversation Memory › The Solution: Conversation History
Michael BrenndoerferJuly 1, 20252 min read
AI Agent HandbookAll chapters Part 6 of 15Memory and Retrieval - How Agents Remember
Short-Term Conversation Memory › The Solution: Conversation HistoryOne section · Sections in this chapter3 / 15 ↓
Sections in this chapter15
The fix is straightforward: keep track of the conversation and send it with each new message. Instead of just sending the latest question, you send the entire dialogue history.
Here's what that looks like:
In[4]:
Codedef ask_with_memory(conversation_history: list, question: str) -> tuple:
"""
Ask a question with full conversation context.
Args:
conversation_history: List of previous messages
question: The new question to ask
Returns:
Tuple of (answer, updated_history)
"""
# Add the new question to history
conversation_history.append({
"role": "user",
"content": question
})
# Send entire conversation to the model
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=conversation_history
)
# Extract the answer
answer = response.content[0].text
# Add the answer to history
conversation_history.append({
"role": "assistant",
"content": answer
})
return answer, conversation_historyNow let's try the same conversation with memory:
In[5]:
Code## Start with empty history
history = []
## First question
answer1, history = ask_with_memory(history, "What's the capital of France?")
print("Q1:", answer1)
## Output: "The capital of France is Paris."
## Follow-up question - now with context!
answer2, history = ask_with_memory(history, "What's the population?")
print("Q2:", answer2)
## Output: "Paris has a population of approximately 2.1 million people
## within the city proper, and about 12 million in the greater
## metropolitan area."Out[5]:
ConsoleQ1: The capital of France is Paris.
Q2: The population of Paris is approximately 2.2 million people within the city limits (as of recent estimates). However, the Paris metropolitan area (Île-de-France region) has a much larger population of around 12-13 million people, making it one of the largest metropolitan areas in Europe.
The agent understood "the population" refers to Paris because it remembered the previous exchange. This is the foundation of conversational AI: maintaining context across turns.