Skip to main content
Each middleware wraps your application to intercept requests and responses, enabling you to implement cross-cutting concerns like authentication, logging, and rate limiting. AgentOS supports any FastAPI/Starlette middleware. You can create custom middleware for logging, rate limiting, monitoring, security, and more.

Creating Custom Middleware

Middleware in AgentOS follows the FastAPI/Starlette pattern using BaseHTTPMiddleware. See the following common middleware examples:

Error Handling

Handle exceptions in middleware:
Error responses must be returned as JSONResponse objects to ensure proper serialization and HTTP status codes.

Adding Middleware to AgentOS

1

Create AgentOS App

custom_middleware.py
2

Add Custom Middleware

3

Serve your AgentOS

Developer Resources

Custom Middleware

Rate limiting and request logging middleware implementation.

Custom FastAPI + JWT

Custom FastAPI app with JWT middleware and AgentOS integration.

FastAPI Middleware

Official FastAPI middleware documentation and examples.