Agent with Memory

Enable user memories and session summaries for a Llama agent backed by PostgresDb.

Code

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.meta import Llama
from rich.pretty import pprint

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

agent = Agent(
    model=Llama(id="Llama-4-Maverick-17B-128E-Instruct-FP8"),
    user_id="test_user",
    session_id="test_session",
    # Pass the database to the Agent
    db=db,
    # Enable user memories
    update_memory_on_run=True,
    # Enable session summaries
    enable_session_summaries=True,
)

# -*- Share personal information
agent.print_response("My name is John Billings", stream=True)

# -*- Print memories and session summary
if agent.db:
    pprint(agent.get_user_memories(user_id="test_user"))
    session = agent.get_session(session_id="test_session")
    pprint(session.summary if session else "No session saved yet")

# -*- Share personal information
agent.print_response("I live in NYC", stream=True)
# -*- Print memories and session summary
if agent.db:
    pprint(agent.get_user_memories(user_id="test_user"))
    session = agent.get_session(session_id="test_session")
    pprint(session.summary if session else "No session saved yet")

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

These examples require access to the Meta Llama API, an API key, and the chosen model enabled for your account. Check the model catalog while signed in, and replace the example ID if your account offers a different compatible model. Installing llama-api-client alone does not grant API access.

The memory and summary managers use the configured Llama model. These example IDs identify one user and conversation; use your application's identities in production. A failed run may leave no saved session or summary.

Usage

Set up your virtual environment

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

Set your LLAMA API key

export LLAMA_API_KEY=YOUR_API_KEY

Install dependencies

uv pip install sqlalchemy "psycopg[binary]" pgvector llama-api-client agno

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