Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.agno.com/llms.txt

Use this file to discover all available pages before exploring further.

A callable that receives a RequestContext and returns a fresh Agent with tenant-specific instructions. The factory runs on every run-scoped request (POST /runs, POST /continue), alongside a normal prototype agent in the same AgentOS.
"""Basic Agent Factory -- per-tenant agent construction.

Demonstrates the simplest factory pattern: a callable that receives a
RequestContext and returns a fresh Agent with tenant-specific instructions.
"""

from agno.agent import Agent, AgentFactory
from agno.db.postgres import PostgresDb
from agno.factory import RequestContext
from agno.models.openai import OpenAIResponses
from agno.os import AgentOS

# ---------------------------------------------------------------------------
# Database
# ---------------------------------------------------------------------------

db = PostgresDb(
    id="factory-demo-db",
    db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
)

# ---------------------------------------------------------------------------
# Factory: build a per-tenant agent
# ---------------------------------------------------------------------------


def build_tenant_agent(ctx: RequestContext) -> Agent:
    """Called on every request. Returns a fresh Agent for the calling tenant."""
    user_id = ctx.user_id or "anonymous"
    return Agent(
        model=OpenAIResponses(id="gpt-5.4"),
        db=db,
        instructions=f"You are a helpful assistant for tenant {user_id}. Be concise.",
        add_datetime_to_context=True,
        markdown=True,
    )


tenant_factory = AgentFactory(
    db=db,
    id="tenant-agent",
    name="Per-tenant assistant",
    description="Builds a personalized agent per tenant on each request",
    factory=build_tenant_agent,
)

# ---------------------------------------------------------------------------
# A normal (prototype) agent alongside the factory
# ---------------------------------------------------------------------------

static_agent = Agent(
    id="support-agent",
    name="Support Agent",
    model=OpenAIResponses(id="gpt-5.4"),
    db=db,
    instructions="You are a general support agent. Be concise.",
    markdown=True,
)

# ---------------------------------------------------------------------------
# AgentOS -- factories and prototypes coexist
# ---------------------------------------------------------------------------

agent_os = AgentOS(
    id="factory-basic-demo",
    description="Demo: basic agent factory alongside a static agent",
    agents=[static_agent, tenant_factory],
)
app = agent_os.get_app()

if __name__ == "__main__":
    agent_os.serve(app="01_basic_factory:app", port=7777, reload=True)

Run the Example

# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno

# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate

# Start Postgres for session storage
./cookbook/scripts/run_pgvector.sh

python cookbook/05_agent_os/factories/agent/01_basic_factory.py