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 Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIChat
4
5# 1. Setup your database to store memories 💾
6db = SqliteDb(db_file="tmp/agent_data.db")
7
8# 2. Spawn the agent with memory enabled
9agent = 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)
15
16# Run 1: Tell the agent a preference
17agent.print_response("Hey! My name is John and I write code in Python.", user_id="user_42")
18
19# 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 PostgresDb
2
3db = 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_42
2user_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:

FieldTypeDescription
memory_idstrUnique identifier for this memory snippet.
memorystrThe extracted fact (e.g., "User prefers Python").
topicslistCategorized tags (e.g., ["programming_language"]).
user_idstrAssociation ID for the user.
agent_idstrThe specific agent that learned this memory.
updated_atintTimestamp of the last modification.

📚 Learn More