Llama OpenAI Memory
Use personalized memories and summaries in an agent.
The source below uses the broken compatible adapter and removed agent.memory accessors. Run the complete Current Example instead. Its setup replaces the unrelated script path in the source docstring.
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `uv pip install openai sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/agents/personalized_memories_and_summaries.py` to run the agent
"""
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.meta import LlamaOpenAI
from rich.pretty import pprint
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=LlamaOpenAI(id="Llama-4-Maverick-17B-128E-Instruct-FP8"),
# Store sessions, memories and summaries in the
db=PostgresDb(db_url=db_url, memory_table="agent_memory"),
update_memory_on_run=True,
enable_session_summaries=True,
# Show debug logs so, you can see the memory being created
)
# -*- Share personal information
agent.print_response("My name is john billings?", stream=True)
# -*- Print memories
pprint(agent.memory.memories)
# -*- Print summary
pprint(agent.memory.summaries)
# -*- Share personal information
agent.print_response("I live in nyc?", stream=True)
# -*- Print memories
pprint(agent.memory.memories)
# -*- Print summary
pprint(agent.memory.summaries)
# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow?", stream=True)
# -*- Print memories
pprint(agent.memory.memories)
# -*- Print summary
pprint(agent.memory.summaries)
# Ask about the conversation
agent.print_response(
"What have we been talking about, do you know my name?", stream=True
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passCurrent Example
Use the same user and session IDs to resume this stored person and conversation. Assign different user IDs to different people; omitted user IDs share a default memory bucket. The memory and summary managers inherit this agent's model. Confirm Meta account/model access before running.
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from rich.pretty import pprint
from os import getenv
from agno.models.openai.like import OpenAILike
def llama_model(**kwargs):
return OpenAILike(
api_key=getenv("LLAMA_API_KEY"),
base_url="https://api.llama.com/compat/v1/",
supports_native_structured_outputs=False,
supports_json_schema_outputs=True,
**kwargs,
)
user_id = "demo-user"
session_id = "llama-memory-demo"
agent = Agent(
model=llama_model(id="Llama-4-Maverick-17B-128E-Instruct-FP8"),
db=PostgresDb(
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
memory_table="agent_memory",
),
user_id=user_id,
session_id=session_id,
update_memory_on_run=True,
enable_session_summaries=True,
)
for message in (
"My name is John Billings.",
"I live in NYC.",
"I'm going to a concert tomorrow.",
):
agent.print_response(message, stream=True)
pprint(agent.get_user_memories(user_id=user_id))
session = agent.get_session(session_id=session_id)
if session is not None:
pprint(session.summary)
agent.print_response("What have we been talking about, do you know my name?", stream=True)Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno "psycopg[binary]" llama-api-client openai sqlalchemyExport your Meta Llama API key
export LLAMA_API_KEY="your_llama_api_key_here"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:18Run the example
Save the complete Current Example above as memory.py, then run:
python memory.pyFull source: cookbook/90_models/meta/llama_openai/memory.py