Implementation Guide — Memory Examples
This directory originally contained Python example scripts that were removed from this archive.
An AI assistant can recreate them by following the instructions below.
conversation_memory.py
Purpose: Show how to maintain multi-turn conversation history with Claude.
What to implement:
- Basic message list: Maintain a
messages = []list and append{"role": "user", ...}/{"role": "assistant", ...}each turn. - Sliding window: When
len(messages) > N, drop the oldest user+assistant pair to stay within token limits. - Summarization compression: When the window is full, call Claude once to summarize the conversation so far, then replace older messages with
{"role": "user", "content": "Conversation summary: ..."}. - Build a simple REPL (input loop) to demonstrate live multi-turn chat.
How to run: python conversation_memory.py
Dependencies: anthropic
external_memory.py
Purpose: Implement long-term agent memory backed by a vector store.
What to implement:
- Use
chromadb(in-memory mode) or a plain JSON file as the memory store. - Save memory: After each assistant turn, embed the response and upsert it into the store with metadata
{turn, timestamp, topic}. - Retrieve memory: Before each user turn, embed the user’s message, retrieve the top-3 most relevant past memories, and inject them into the system prompt as “Relevant past context”.
- Demonstrate the agent “remembering” something mentioned several turns ago.
How to run: python external_memory.py
Dependencies: anthropic, chromadb, sentence-transformers