Custom Memory Manager

This example shows how you can configure the Memory Manager.

We also set custom system prompts for the memory manager. You can either override the entire system prompt or add additional instructions which is added to the end of the system prompt.

Code

1from kern.agent import Agent
2from kern.db.postgres import PostgresDb
3from kern.memory import MemoryManager
4from kern.models.openai import OpenAIResponses
5from rich.pretty import pprint
6
7db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
8
9db = PostgresDb(db_url=db_url)
10
11# You can also override the entire `system_message` for the memory manager
12memory_manager = MemoryManager(
13 model=OpenAIResponses(id="gpt-5.2"),
14 additional_instructions="""
15 IMPORTANT: Don't store any memories about the user's name. Just say "The User" instead of referencing the user's name.
16 """,
17 db=db,
18)
19
20john_doe_id = "john_doe@example.com"
21
22agent = Agent(
23 model=OpenAIResponses(id="gpt-5.2"),
24 db=db,
25 memory_manager=memory_manager,
26 update_memory_on_run=True,
27 user_id=john_doe_id,
28)
29
30agent.print_response(
31 "My name is John Doe and I like to swim and play soccer.", stream=True
32)
33
34agent.print_response("I dont like to swim", stream=True)
35
36
37memories = agent.get_user_memories(user_id=john_doe_id)
38
39print("John Doe's memories:")
40pprint(memories)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U kern-ai

Run Example

1python custom_memory_manager.py