Persistent Session

Persistent Session Example.

1"""
2Persistent Session
3=============================
4
5Persistent Session Example.
6"""
7
8from kern.agent.agent import Agent
9from kern.db.postgres import PostgresDb
10from kern.models.openai import OpenAIResponses
11
12db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
13
14db = PostgresDb(db_url=db_url, session_table="sessions")
15
16# ---------------------------------------------------------------------------
17# Create Agent
18# ---------------------------------------------------------------------------
19agent = Agent(
20 model=OpenAIResponses(id="gpt-5-mini"),
21 db=db,
22 session_id="session_storage",
23 add_history_to_context=True,
24)
25
26# ---------------------------------------------------------------------------
27# Run Agent
28# ---------------------------------------------------------------------------
29if __name__ == "__main__":
30 agent.print_response("Tell me a new interesting fact about space")

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/05_state_and_session
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python persistent_session.py