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 Agent2from kern.db.sqlite import SqliteDb3from kern.models.openai import OpenAIResponses4from kern.tools.hackernews import HackerNewsTools5from rich.pretty import pprint67db = SqliteDb(db_file="agents.db")89john_doe_id = "john_doe@example.com"1011chat_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)1718chat_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)2324chat_agent.print_response("What are my hobbies?", stream=True, user_id=john_doe_id)252627research_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)3435research_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)4041memories = 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.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openai richExport your OpenAI API key
1export OPENAI_API_KEY=your_openai_api_key_hereRun Example
1python agents_share_memory.py