Writing custom middleware
Notebook· 04.03· 150–180 min· Lesson 35 of 129
The notebook opens in your lab, in a new tab. If your server is stopped, it starts first (a few seconds).
By the end you can
- Write middleware in both styles, decorator (
@wrap_model_call,@before_model, …) and class (AgentMiddleware), and choose between them. - Build three real middleware by hand: a request logger, a role-based tool filter, and a token-budget guard with custom state.
- Use the tested versions in
mastery.middleware(AuditLogMiddleware,RoleToolFilterMiddleware,TokenBudgetMiddleware). - Test middleware without a real model, and keep a failing cell reproducible (the module's debugging skill).
Before you start
Lessons 04.01–04.02.
