Complete AgentOS setup with custom FastAPI app and router
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.anthropic import Claude
from agno.os import AgentOS
from agno.tools.duckduckgo import DuckDuckGoTools
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
# Setup the database
db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")
# Setup basic agents, teams and workflows
web_research_agent = Agent(
name="Basic Agent",
model=Claude(id="claude-sonnet-4-0"),
db=db,
tools=[DuckDuckGoTools()],
add_history_to_context=True,
num_history_runs=3,
add_datetime_to_context=True,
markdown=True,
)
# Custom FastAPI app
app: FastAPI = FastAPI(
title="Custom FastAPI App",
version="1.0.0",
)
# Add Middlewares
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add your own routes
@app.post("/customers")
async def get_customers():
return [
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com",
},
{
"id": 2,
"name": "Jane Doe",
"email": "jane.doe@example.com",
},
]
# Setup our AgentOS app by passing your FastAPI app
agent_os = AgentOS(
description="Example app with custom routers",
agents=[web_research_agent],
fastapi_app=app,
)
# Alternatively, add all routes from AgentOS app to the current app
# for route in agent_os.get_routes():
# app.router.routes.append(route)
app = agent_os.get_app()
if __name__ == "__main__":
"""Run our AgentOS.
You can see the docs at:
http://localhost:7777/docs
"""
agent_os.serve(app="custom_fastapi_app:app", reload=True)
Create a virtual environment
Terminal
and create a python virtual environment.python3 -m venv .venv
source .venv/bin/activate
Set Environment Variables
export ANTHROPIC_API_KEY=your_anthropic_api_key
export DATABASE_URL=postgresql+psycopg://ai:ai@localhost:5532/ai # Optional
Install libraries
pip install -U agno anthropic fastapi["standard"] uvicorn sqlalchemy pgvector psycopg
Setup PostgreSQL Database
# Using Docker
docker run -d \
--name agno-postgres \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-p 5532:5432 \
pgvector/pgvector:pg17
Run Example with FastAPI CLI
fastapi run cookbook/agent_os/custom_fastapi_app.py
Run Example with Python
python cookbook/agent_os/custom_routers.py