I have been scaffolding a project lately and while the dev speed was great the performance was garbage I was seeing 250ms plus response times for simple authenticated routes I realized the initial code was full of middleware bloat
To fix it I used Blackbox AI to refactor the logic and find the bottlenecks Most LLMs suggest the easy way like BaseHTTPMiddleware but Blackbox helped me implement more performant patterns like Pure ASGI middleware It is a bit more code but it talks directly to the server and shaved 20ms off every single request
I also moved my Auth logic from global middleware into a FastAPI Dependency This was huge it stopped the app from hitting the database on every single public request or docs page load Lastly I tweaked GZip to only trigger for payloads over 1kb so it does not waste CPU on tiny JSON responses
The result My P95 response times dropped from 280ms to 85ms on my EC2 instance Lesson learned AI is great for the what but you still need to be the how when it comes to speed If your routes feel sluggish look at your middleware stack first it is usually the silent killer
Anyone else notice AI generated code leaning too hard on BaseHTTPMiddleware Curious if there are other hidden overheads I should look out for
TLDR Swapped BaseHTTPMiddleware for pure ASGI using Blackbox AI moved auth to Depends and stopped GZipping tiny responses Cut latency by 70 percent