Bring Your Own FastAPI App

Integrate your own FastAPI app with AgentOS.

AgentOS is built on FastAPI, which means you can integrate your existing FastAPI application, routes, middleware, dependencies, and deployment entrypoints with AgentOS.

Save this complete example as app.py. Later Python blocks are alternative configuration fragments using its existing support_agent and imports.

from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.os import AgentOS
from fastapi import FastAPI

app = FastAPI(title="Product API")


@app.get("/account/{account_id}")
async def get_account(account_id: str):
    return {"account_id": account_id, "status": "active"}


support_agent = Agent(
    id="support-agent",
    model=OpenAIResponses(id="gpt-5.4"),
)

agent_os = AgentOS(
    agents=[support_agent],
    base_app=app,
)

app = agent_os.get_app()

if __name__ == "__main__":
    agent_os.serve(app="app:app", reload=True)

The returned app serves both routes:

RouteOwner
GET /account/{account_id}Product API
POST /agents/support-agent/runsAgentOS

Handle Route Conflicts

Set on_route_conflict when the base application already defines a path and method used by AgentOS.

ValueBehaviorUse when
"preserve_agentos"AgentOS replaces the conflicting base-app routeAgentOS should own its standard API paths
"preserve_base_app"AgentOS skips the conflicting routeThe product route must keep its existing behavior
"error"Application construction raises a ValueErrorEvery conflict should block deployment

"preserve_agentos" is the default.

agent_os = AgentOS(
    agents=[support_agent],
    base_app=app,
    on_route_conflict="error",
)

See Override Routes for conflict examples and matching behavior.

Keep Existing Application Behavior

AgentOS prepares the supplied FastAPI app in place:

Existing behaviorAgentOS behavior
Routes and routersPreserved unless they conflict with an AgentOS route
MiddlewareRetained on the combined application
FastAPI dependenciesContinue to apply to the routes that declare them
LifespanCombined with the lifespan passed to AgentOS
CORSExisting CORS middleware is updated with AgentOS origins, or AgentOS adds CORS middleware

Use AgentOS authorization for runtime access control. Keep product-specific FastAPI dependencies on the custom routes that need them.

Add Middleware

Add FastAPI or Starlette middleware to the base app before calling get_app():

from starlette.middleware import Middleware
from starlette.middleware.trustedhost import TrustedHostMiddleware

app = FastAPI(
    middleware=[
        Middleware(
            TrustedHostMiddleware,
            allowed_hosts=["api.example.com"],
        )
    ]
)

agent_os = AgentOS(
    agents=[support_agent],
    base_app=app,
)
app = agent_os.get_app()

See AgentOS Middleware for JWT validation, request context, logging, and custom middleware.

Combine Lifespans

Pass a lifespan to AgentOS when the runtime needs its own startup and shutdown work. AgentOS wraps the lifespan already configured on the base app.

from contextlib import asynccontextmanager


@asynccontextmanager
async def agent_os_lifespan(app):
    app.state.runtime_status = "ready"
    yield
    app.state.runtime_status = "stopped"


agent_os = AgentOS(
    agents=[support_agent],
    base_app=app,
    lifespan=agent_os_lifespan,
)

app = agent_os.get_app()

See Custom Lifespan for a complete example.

Run the Combined App

Install the AgentOS and FastAPI CLI dependencies:

uv pip install -U "agno[os]" openai "fastapi[standard]"
export OPENAI_API_KEY="your_openai_api_key"
fastapi dev app.py
fastapi run app.py --host 0.0.0.0 --port 8000
uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4

Next Steps

TaskGuide
Resolve path and method conflictsOverride Routes
Add authentication and request middlewareAgentOS Middleware
Configure runtime authorizationAuthorization
Run startup and shutdown logicCustom Lifespan
Review base_app parametersAgentOS class reference