Agentic Memory

This example shows you how to use persistent memory with an Agent.

During each run the Agent can create/update/delete user memories.

To enable this, set enable_agentic_memory=True in the Agent config.

Code

1from kern.agent import Agent
2from kern.db.postgres import PostgresDb
3from kern.models.openai import OpenAIResponses
4from rich.pretty import pprint
5
6db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
7
8db = PostgresDb(db_url=db_url)
9
10
11john_doe_id = "john_doe@example.com"
12
13agent = Agent(
14 model=OpenAIResponses(id="gpt-5.2"),
15 db=db,
16 enable_agentic_memory=True,
17)
18
19agent.print_response(
20 "My name is John Doe and I like to hike in the mountains on weekends.",
21 stream=True,
22 user_id=john_doe_id,
23)
24
25agent.print_response("What are my hobbies?", stream=True, user_id=john_doe_id)
26
27memories = agent.get_user_memories(user_id=john_doe_id)
28print("Memories about John Doe:")
29pprint(memories)
30
31
32agent.print_response(
33 "Remove all existing memories of me.",
34 stream=True,
35 user_id=john_doe_id,
36)
37
38memories = agent.get_user_memories(user_id=john_doe_id)
39print("Memories about John Doe:")
40pprint(memories)
41
42agent.print_response(
43 "My name is John Doe and I like to paint.", stream=True, user_id=john_doe_id
44)
45
46memories = agent.get_user_memories(user_id=john_doe_id)
47print("Memories about John Doe:")
48pprint(memories)
49
50
51agent.print_response(
52 "I don't paint anymore, i draw instead.", stream=True, user_id=john_doe_id
53)
54
55memories = agent.get_user_memories(user_id=john_doe_id)
56
57print("Memories about John Doe:")
58pprint(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 agentic_memory.py