Skip to main content
Custom Starlette middleware can wrap the FastAPI app returned by AgentOS.get_app(). add_middleware is last-in, first-out: the logging middleware added last is the outer layer and sees each request before the rate limiter added first.
custom_middleware.py

Run the Example

1

Set up your virtual environment

2

Install dependencies

3

Export your OpenAI API key

4

Run the example

Save the code above as custom_middleware.py, then run:
Full source: cookbook/05_agent_os/06_customize/custom_middleware.py