Brains with Recall: Memory 🧠
Give your agents the ability to remember user preferences, context, and habits across conversations.
Imagine a support assistant that remembers your shipping address from last month, or a coding partner that knows you prefer tabs over spaces.
That's the power of Memory in Kern! 🧠
When relevant facts appear in conversation, an agent with Memory extracts them, stores them in a database, and recalls them when they become relevant again. The agent dynamically learns about each user over time.
💡 Memory vs. Session History
It's easy to get these two confused:
- Session History (Short-term continuity) 💬: Stores raw messages so the model remembers what you just said in the current thread ("What did we just talk about?").
- Memory (Long-term recall) 🧠: Stores extracted facts about the user across days, weeks, and different sessions ("What are Sarah's food preferences?").
🛠️ Bootstrapping Memory
To give your agent a memory, connect a database and turn on update_memory_on_run=True:
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.models.openai import OpenAIChat45# 1. Setup your database to store memories 💾6db = SqliteDb(db_file="tmp/agent_data.db")78# 2. Spawn the agent with memory enabled9agent = Agent(10 model=OpenAIChat(id="llama3.2:3b", base_url="http://localhost:11434/v1"),11 db=db,12 update_memory_on_run=True, # Extractor runs automatically! 🧠13 markdown=True,14)1516# Run 1: Tell the agent a preference17agent.print_response("Hey! My name is John and I write code in Python.", user_id="user_42")1819# Run 2: Test if the agent remembers!20agent.print_response("What language should I use to write this script?", user_id="user_42")🔄 Two Memory Topologies
Depending on how much control you want to give the model, Kern supports two memory strategies:
1. Automatic Memory (update_memory_on_run=True)
After each run completes, Kern runs a background extraction task to identify user facts, update preferences, and save them. This is predictable and works perfectly for standard assistant bots.
2. Agentic Memory (enable_agentic_memory=True)
The agent is equipped with memory management tools. It decides when to create, search, update, or delete memories on its own based on the flow of conversation. (Note: These two options are mutually exclusive; Agentic memory takes precedence if both are set.)
💾 Where do memories live?
Memories are stored in your agent's database. By default, they are saved in a table named kern_memories (or a collection of the same name for MongoDB). Kern creates this table automatically on its first run.
You can customize the table name when initializing the database:
1from kern.db.postgres import PostgresDb23db = PostgresDb(4 db_url="postgresql://user:pass@localhost:5432/my_db",5 memory_table="user_insights_table", # Custom table name 🏷️6)🔍 Manual Retrieval
You can query stored memories programmatically (e.g. to display a user profile in your frontend application):
1# Fetch raw memories for user_422user_memories = agent.get_user_memories(user_id="user_42")3for memory in user_memories:4 print(f"[{memory.topics}] {memory.memory}")📊 Memory Schema Reference
Each row in kern_memories stores the following parameters:
| Field | Type | Description |
|---|---|---|
memory_id | str | Unique identifier for this memory snippet. |
memory | str | The extracted fact (e.g., "User prefers Python"). |
topics | list | Categorized tags (e.g., ["programming_language"]). |
user_id | str | Association ID for the user. |
agent_id | str | The specific agent that learned this memory. |
updated_at | int | Timestamp of the last modification. |