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.The general syntax for adding storage to an Agent looks like:
storage.py
Copy
Ask AI
from phi.agent import Agentfrom phi.model.openai import OpenAIChatfrom phi.tools.duckduckgo import DuckDuckGofrom phi.storage.agent.postgres import PgAgentStorageagent = Agent( model=OpenAIChat(id="gpt-4o"), storage=PgAgentStorage(table_name="agent_sessions", db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"), tools=[DuckDuckGo()], 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?")
The following databases are supported as a storage backend: