Share Memory between Agents

This example demonstrates how to share memory between Agents.

This means that memories created by one Agent, will be available to the other Agents.

Code

1from kern.agent import Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIResponses
4from kern.tools.hackernews import HackerNewsTools
5from rich.pretty import pprint
6
7db = SqliteDb(db_file="agents.db")
8
9john_doe_id = "john_doe@example.com"
10
11chat_agent = Agent(
12 model=OpenAIResponses(id="gpt-5.2"),
13 description="You are a helpful assistant that can chat with users",
14 db=db,
15 update_memory_on_run=True,
16)
17
18chat_agent.print_response(
19 "My name is John Doe and I like to hike in the mountains on weekends.",
20 stream=True,
21 user_id=john_doe_id,
22)
23
24chat_agent.print_response("What are my hobbies?", stream=True, user_id=john_doe_id)
25
26
27research_agent = Agent(
28 model=OpenAIResponses(id="gpt-5.2"),
29 description="You are a research assistant that can help users with their research questions",
30 tools=[HackerNewsTools()],
31 db=db,
32 update_memory_on_run=True,
33)
34
35research_agent.print_response(
36 "I love reading about AI. What are the top stories on Hacker News about AI?",
37 stream=True,
38 user_id=john_doe_id,
39)
40
41memories = research_agent.get_user_memories(user_id=john_doe_id)
42print("Memories about John Doe:")
43pprint(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 openai rich

Export your OpenAI API key

1export OPENAI_API_KEY=your_openai_api_key_here

Run Example

1python agents_share_memory.py