Memory Tools
The MemoryTools toolkit enables Agents to manage user memories through create, update, and delete operations. This toolkit integrates with a provided database where memories are stored.
The toolkit implements a "Think → Operate → Analyze" cycle that allows an Agent to:
- Think through memory management requirements and plan operations
- Execute memory operations (add, update, delete) on the database
- Analyze the results to ensure operations completed successfully and meet requirements
This approach gives Agents the ability to persistently store, retrieve, and manage user information, preferences, and context across conversations.
The toolkit includes the following tools:
think: A scratchpad for planning memory operations, brainstorming content, and refining approaches. These thoughts remain internal to the Agent and are not shown to users.get_memories: Gets a list of memories for the current user from the database.add_memory: Creates new memories in the database with specified content and optional topics.update_memory: Modifies existing memories by memory ID, allowing updates to content and topics.delete_memory: Removes memories from the database by memory ID.analyze: Evaluates whether memory operations completed successfully and produced the expected results.
Example
Here's an example of how to use the MemoryTools toolkit:
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.models.openai import OpenAIResponses4from kern.tools.memory import MemoryTools56# Create a database connection7db = SqliteDb(8 db_file="tmp/memory.db"9)1011memory_tools = MemoryTools(12 db=db,13)1415agent = Agent(16 model=OpenAIResponses(id="gpt-5.2"),17 tools=[memory_tools],18 markdown=True,19)2021agent.print_response(22 "My name is John Doe and I like to hike in the mountains on weekends. "23 "I like to travel to new places and experience different cultures. "24 "I am planning to travel to Africa in December. ",25 user_id="john_doe@example.com",26 stream=True27)2829# This won't use the session history, but instead will use the memory tools to get the memories30agent.print_response("What have you remembered about me?", stream=True, user_id="john_doe@example.com")Here is how you can configure the toolkit:
1from kern.tools.memory import MemoryTools23memory_tools = MemoryTools(4 db=my_database,5 enable_think=True, # Enable the think tool (true by default)6 enable_get_memories=True, # Enable the get_memories tool (true by default)7 enable_add_memory=True, # Enable the add_memory tool (true by default)8 enable_update_memory=True, # Enable the update_memory tool (true by default)9 enable_delete_memory=True, # Enable the delete_memory tool (true by default)10 enable_analyze=True, # Enable the analyze tool (true by default)11 add_instructions=True, # Add default instructions12 instructions=None, # Optional custom instructions13 add_few_shot=True, # Add few-shot examples14 few_shot_examples=None, # Optional custom few-shot examples15)