Skip to main content
This example demonstrates how to enable automatic session summaries that help maintain conversation context across longer interactions by summarizing previous conversations.

Code

03_session_summary.py
"""
This example shows how to use the session summary to store the conversation summary.
"""

from agno.agent.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.openai import OpenAIChat
from agno.session.summary import SessionSummaryManager  # noqa: F401

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

db = PostgresDb(db_url=db_url, session_table="sessions")

# Method 1: Set enable_session_summaries to True

agent = Agent(
    model=OpenAIChat(id="gpt-5-mini"),
    db=db,
    enable_session_summaries=True,
    session_id="session_summary",
)

agent.print_response("Hi my name is John and I live in New York")
agent.print_response("I like to play basketball and hike in the mountains")

# Method 2: Set session_summary_manager

# session_summary_manager = SessionSummaryManager(model=OpenAIChat(id="gpt-5-mini"))

# agent = Agent(
#     model=OpenAIChat(id="gpt-5-mini"),
#     db=db,
#     session_id="session_summary",
#     session_summary_manager=session_summary_manager,
# )

# agent.print_response("Hi my name is John and I live in New York")
# agent.print_response("I like to play basketball and hike in the mountains")

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
2

Install libraries

pip install -U agno openai psycopg2-binary
3

Setup PostgreSQL

# Make sure PostgreSQL is running
# Update connection string in the code as needed
4

Export your OpenAI API key

  export OPENAI_API_KEY="your_openai_api_key_here"
5

Create a Python file

Create a Python file and add the above code.
touch 03_session_summary.py
6

Run Agent

python 03_session_summary.py
7

Find All Cookbooks

Explore all the available cookbooks in the Agno repository. Click the link below to view the code on GitHub:Agno Cookbooks on GitHub
I