Agent with Memory
Persist memories and session summaries for a vLLM agent with PostgresDb.
Code
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.vllm import VLLM
from rich.pretty import pprint
# Change this if your Postgres container is running elsewhere
DB_URL = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=VLLM(id="Qwen/Qwen2.5-7B-Instruct"),
db=PostgresDb(db_url=DB_URL),
user_id="test_user",
session_id="test_session",
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")
pprint(session.summary if session else "No stored session is available.")
# -*- 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")
pprint(session.summary if session else "No stored session is available.")
# -*- 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")
pprint(session.summary if session else "No stored session is available.")
# Ask about the conversation
agent.print_response(
"What have we been talking about, do you know my name?", stream=True
)The reads below each run use the same user and session IDs configured on the agent. Summary generation can fail, so a stored session can have no summary. Both memory and summary managers use the configured vLLM model; memory updates require a tool-capable model and template. The server below pairs Qwen2.5 with the Hermes parser.
Usage
Install vLLM in a server environment using the installation guide for your hardware. The GPU commands below require a supported Linux accelerator environment. The Agno client can run in a separate Python environment with agno, openai and the example's other dependencies.
Run vllm serve in a dedicated server terminal with its environment active and keep it running. In a client terminal, activate the environment for your saved Python file, export VLLM_API_KEY, and run the agent. The examples use port 8000; for a server on another host, set the matching base_url on VLLM. A server started without --api-key accepts a placeholder key, but Agno still requires a nonempty VLLM_API_KEY.
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateRun 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:18Install dependencies
uv pip install -U agno openai sqlalchemy "psycopg[binary]" pgvectorStart vLLM server
vllm serve Qwen/Qwen2.5-7B-Instruct \
--dtype float32 \
--enable-auto-tool-choice \
--tool-call-parser hermesSet your API key
export VLLM_API_KEY=xxxRun Agent
Save the code above as memory.py, then run:
python memory.py