Load Agent from Database
Fetch a stored agent from PostgresDb with get_agent_by_id and run it, with get_agents shown for listing all.
Demonstrates loading an agent from the database by ID and running it.
"""
Load Agent from Database
========================
Demonstrates loading an agent from the database by ID and running it.
"""
from agno.agent.agent import get_agent_by_id, get_agents # noqa: F401
from agno.db.postgres import PostgresDb
# ---------------------------------------------------------------------------
# Create Database Client
# ---------------------------------------------------------------------------
db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")
# ---------------------------------------------------------------------------
# Run Agent Load Example
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent = get_agent_by_id(db=db, id="agno-agent")
if agent:
agent.print_response("How many people live in Canada?")
else:
print("Agent not found")
# You can also get all agents from the database
# agents = get_agents(db=db)
# for agent in agents:
# print(agent)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]" openai sqlalchemyExport your API keys
export OPENAI_API_KEY="your_openai_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:18Clone Agno
Clone the pinned Agno source and run the remaining commands from its root:
git clone https://github.com/agno-agi/agno.git
cd agno
git checkout v3.0.4Save the agent
Persist the agno-agent record before loading it:
python cookbook/93_components/save_agent.pyRun the example
Run the example from the repository root:
python cookbook/93_components/get_agent.pyFull source: cookbook/93_components/get_agent.py