Skip to main content
This guide walks you through setting up an agent with Learning Machine.

Prerequisites

  • Python 3.9+
  • A database (PostgreSQL recommended for production)
  • OpenAI API key (or another model provider)

Setup

1

Set up your virtual environment

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

Install dependencies

uv pip install -U agno openai sqlalchemy psycopg
3

Run PostgreSQL with PgVector

Install Docker Desktop and run:
docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:16
4

Set your API key

export OPENAI_API_KEY=sk-***

Your First Learning Agent

Create a file called learning_agent.py:
learning_agent.py
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.openai import OpenAIResponses

# Connect to your database
db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")

# Create an agent with learning enabled
agent = Agent(
    model=OpenAIResponses(id="gpt-5.2"),
    db=db,
    learning=True,
    markdown=True,
)

# Session 1: Share some information
print("=== Session 1 ===")
agent.print_response(
    "Hi! I'm Sarah, I work at Acme Corp as a data scientist. I prefer detailed explanations.",
    user_id="[email protected]",
    session_id="session_1",
)

# Session 2: The agent remembers
print("\n=== Session 2 ===")
agent.print_response(
    "What do you know about me?",
    user_id="[email protected]",
    session_id="session_2",
)
Run it:
python learning_agent.py
The agent will remember Sarah’s name, company, role, and preferences across sessions.

What Just Happened?

With learning=True, the agent:
  1. Extracted profile info from Session 1 (name: Sarah, company: Acme Corp, role: data scientist)
  2. Stored a preference (prefers detailed explanations)
  3. Recalled this context in Session 2 to answer accurately

Customizing What Gets Learned

Control which stores are enabled:
from agno.learn import LearningMachine

agent = Agent(
    model=OpenAIResponses(id="gpt-5.2"),
    db=db,
    learning=LearningMachine(
        user_profile=True,      # Structured fields (name, preferences)
        user_memory=True,       # Unstructured observations
        session_context=True,   # Session summary and goals
        entity_memory=False,    # Knowledge about external entities
        learned_knowledge=False # Reusable insights (requires Knowledge base)
    ),
)

Using SQLite (Development)

For quick development without PostgreSQL:
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses

db = SqliteDb(db_file="learning.db")

agent = Agent(
    model=OpenAIResponses(id="gpt-5.2"),
    db=db,
    learning=True,
)

Next Steps