Skip to main content
AgentOS supports any FastAPI/Starlette middleware. You can create custom middleware for logging, rate limiting, monitoring, security, and more.

Creating Custom Middleware

Custom middleware in AgentOS follows the FastAPI/Starlette middleware pattern using BaseHTTPMiddleware:

Common Use Cases

These are some examples demonstrating how to add some common custom middlewares:

Adding Middleware to AgentOS

1

Create AgentOS App

custom_middleware.py
2

Add Custom Middleware

3

Serve your AgentOS

See the following full examples:

Error Handling

Handle exceptions in middleware:
It is important to note that the error response should be a JSONResponse.

Developer Resources