Agent with Memory

Store user memories and session summaries for a Perplexity agent in Postgres.

Sonar's built-in web search is separate from Agno function tools. The current Perplexity adapter does not forward Agno tool definitions. Use eager knowledge retrieval with add_knowledge_to_context=True and search_knowledge=False, and an explicit tool-capable MemoryManager model for memory extraction.

The memory manager uses an explicit tool-capable OpenAI model to write memories. The user and session IDs connect the runs to the records read below; replace these example IDs with your application's identities. Summary generation can still use the Perplexity model.

Code

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.perplexity import Perplexity
from agno.models.openai import OpenAIChat
from agno.memory import MemoryManager
from rich.pretty import pprint

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
db = PostgresDb(db_url=db_url)

agent = Agent(
    model=Perplexity(id="sonar-pro"),
    # Store the memories and summary in a database
    db=db,
    user_id="test_user",
    session_id="test_session",
    memory_manager=MemoryManager(model=OpenAIChat(id="gpt-5.4-mini"), db=db),
    update_memory_on_run=True,
    enable_session_summaries=True,
)

# -*- Share personal information
agent.print_response("My name is john billings", stream=True)
# -*- Print memories and summary
if agent.db:
    pprint(agent.get_user_memories(user_id="test_user"))
    session = agent.get_session(session_id="test_session")
    if session is not None and session.summary is not None:
        pprint(session.summary)

# -*- Share personal information
agent.print_response("I live in nyc", stream=True)
# -*- Print memories and summary
if agent.db:
    pprint(agent.get_user_memories(user_id="test_user"))
    session = agent.get_session(session_id="test_session")
    if session is not None and session.summary is not None:
        pprint(session.summary)

# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow", stream=True)
# -*- Print memories and summary
if agent.db:
    pprint(agent.get_user_memories(user_id="test_user"))
    session = agent.get_session(session_id="test_session")
    if session is not None and session.summary is not None:
        pprint(session.summary)

# Ask about the conversation
agent.print_response(
    "What have we been talking about, do you know my name?", stream=True
)

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Set your API keys

Perplexity handles chat. OpenAI handles memory extraction with function tools.

export PERPLEXITY_API_KEY="YOUR_PERPLEXITY_API_KEY"
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"

Install dependencies

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

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18

Run Agent

Save the code above as memory.py, then run:

python memory.py