Session Summary

This example shows how to use the session summary to store the conversation summary.

1"""
2Session Summary
3=============================
4
5This example shows how to use the session summary to store the conversation summary.
6"""
7
8from kern.agent.agent import Agent
9from kern.db.postgres import PostgresDb
10from kern.models.openai import OpenAIResponses
11from kern.session.summary import SessionSummaryManager # noqa: F401
12
13db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
14
15db = PostgresDb(db_url=db_url, session_table="sessions")
16
17# Method 1: Set enable_session_summaries to True
18
19# ---------------------------------------------------------------------------
20# Create Agent
21# ---------------------------------------------------------------------------
22agent = Agent(
23 model=OpenAIResponses(id="gpt-5-mini"),
24 db=db,
25 enable_session_summaries=True,
26 session_id="session_123",
27)
28
29# ---------------------------------------------------------------------------
30# Run Agent
31# ---------------------------------------------------------------------------
32if __name__ == "__main__":
33 agent.print_response("Hi my name is John and I live in New York")
34 agent.print_response("I like to play basketball and hike in the mountains")
35
36 print(agent.get_session_summary(session_id="session_123"))
37
38 # Method 2: Set session_summary_manager
39
40 # session_summary_manager = SessionSummaryManager(model=OpenAIResponses(id="gpt-5-mini"))
41
42 # agent = Agent(
43 # model=OpenAIResponses(id="gpt-5-mini"),
44 # db=db,
45 # session_id="session_summary",
46 # session_summary_manager=session_summary_manager,
47 # )
48
49 # agent.print_response("Hi my name is John and I live in New York")
50 # agent.print_response("I like to play basketball and hike in the mountains")

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 session_summary.py