Team with Agentic Memory

This example demonstrates how to use agentic memory with a team. Unlike simple memory storage, agentic memory allows the AI to actively create, update, and delete user memories during each run based on the conversation context, providing intelligent memory management.

Code

1"""
2This example shows you how to use persistent memory with an Agent.
3
4During each run the Agent can create/update/delete user memories.
5
6To enable this, set `enable_agentic_memory=True` in the Agent config.
7"""
8
9from kern.agent import Agent
10from kern.db.postgres import PostgresDb
11from kern.memory import MemoryManager # noqa: F401
12from kern.models.openai import OpenAIResponses
13from kern.team import Team
14
15db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
16db = PostgresDb(db_url=db_url)
17
18john_doe_id = "john_doe@example.com"
19
20agent = Agent(
21 model=OpenAIResponses(id="gpt-5.2"),
22)
23
24team = Team(
25 model=OpenAIResponses(id="gpt-5.2"),
26 members=[agent],
27 db=db,
28 enable_agentic_memory=True,
29)
30
31team.print_response(
32 "My name is John Doe and I like to hike in the mountains on weekends.",
33 stream=True,
34 user_id=john_doe_id,
35)
36
37team.print_response("What are my hobbies?", stream=True, user_id=john_doe_id)
38
39# More examples:
40# agent.print_response(
41# "Remove all existing memories of me.",
42# stream=True,
43# user_id=john_doe_id,
44# )
45
46# agent.print_response(
47# "My name is John Doe and I like to paint.", stream=True, user_id=john_doe_id
48# )
49
50# agent.print_response(
51# "I don't pain anymore, i draw instead.", stream=True, user_id=john_doe_id
52# )

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 openai psycopg sqlalchemy

Set up PostgreSQL database

Start PostgreSQL with pgvector and update the connection string in the code as needed.

Set environment variables

1export OPENAI_API_KEY=your_openai_api_key_here

Run the example

1python team_with_agentic_memory.py