Agents use storage to persist sessions by storing them in a database.

Agents come with built-in memory but it only lasts while the session is active. To continue conversations across sessions, we store Agent sessions in a database, like PostgreSQL.

Storage is a necessary component when building user facing AI products as any production application will require users to be able to “continue” their conversation with the Agent.

Storage works with both agents, teams, and workflows. The storage system in Agno is versatile and can be used with both individual agents and complex workflows. This allows for persistent conversations and cached results across sessions.

Learn more about using storage with agents, workflows and teams to build powerful multi-agent systems with state management.

The general syntax for adding storage to an Agent looks like:

storage.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.storage.postgres import PostgresStorage

agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    storage=PostgresStorage(table_name="agent_sessions", db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
    tools=[DuckDuckGoTools()],
    show_tool_calls=True,
    add_history_to_messages=True,
)
agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")
agent.print_response("Which country are we speaking about?")

Example

1

Run Postgres

Install docker desktop and run Postgres on port 5532 using:

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  agno/pgvector:16
2

Create an Agent with Storage

Create a file agent_with_storage.py with the following contents

import typer
from typing import Optional, List
from agno.agent import Agent
from agno.storage.postgres import PostgresStorage
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.pgvector import PgVector, SearchType

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=PgVector(table_name="recipes", db_url=db_url, search_type=SearchType.hybrid),
)
storage = PostgresStorage(table_name="pdf_agent", db_url=db_url)

def pdf_agent(new: bool = False, user: str = "user"):
    session_id: Optional[str] = None

    if not new:
        existing_sessions: List[str] = storage.get_all_session_ids(user)
        if len(existing_sessions) > 0:
            session_id = existing_sessions[0]

    agent = Agent(
        session_id=session_id,
        user_id=user,
        knowledge=knowledge_base,
        storage=storage,
        # Show tool calls in the response
        show_tool_calls=True,
        # Enable the agent to read the chat history
        read_chat_history=True,
        # We can also automatically add the chat history to the messages sent to the model
        # But giving the model the chat history is not always useful, so we give it a tool instead
        # to only use when needed.
        # add_history_to_messages=True,
        # Number of historical responses to add to the messages.
        # num_history_responses=3,
    )
    if session_id is None:
        session_id = agent.session_id
        print(f"Started Session: {session_id}\n")
    else:
        print(f"Continuing Session: {session_id}\n")

    # Runs the agent as a cli app
    agent.cli_app(markdown=True)


if __name__ == "__main__":
    # Load the knowledge base: Comment after first run
    knowledge_base.load(upsert=True)

    typer.run(pdf_agent)
3

Run the agent

Install libraries

pip install -U agno openai pgvector pypdf "psycopg[binary]" sqlalchemy

Run the agent

python agent_with_storage.py

Now the agent continues across sessions. Ask a question:

How do I make pad thai?

Then message bye to exit, start the app again and ask:

What was my last message?
4

Start a new run

Run the agent_with_storage.py file with the --new flag to start a new run.

python agent_with_storage.py --new

Schema Upgrades

The relational SQL-based storage classes have fixed schemas. As new Agno features are released, the schemas might need to be updated.

Upgrades can either be done manually or automatically.

Automatic Upgrades

Automatic upgrades are done when the auto_upgrade_schema parameter is set to True in the storage class constructor.
You only need to set this once for an agent run and the schema would be upgraded.

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
storage = PostgresStorage(table_name="agent_sessions", db_url=db_url, auto_upgrade_schema=True)

Manual Upgrades

Manual schema upgrades can be done by calling the upgrade_schema method on the storage class.

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
storage = PostgresStorage(table_name="agent_sessions", db_url=db_url)  
storage.upgrade_schema()

Params

ParameterTypeDefaultDescription
storageOptional[AgentStorage]NoneStorage mechanism for the agent.

Developer Resources