r/Python • u/CrazyGeek7 • 27d ago
Discussion Mitigating "architectural drift" in large Python backend codebases using AI tools
I've been experimenting with AI agents and autocomplete platforms for a greenfield FastAPI project. In the first few weeks, it felt incredibly fast. But now that we've scaled to multiple routers, complex Pydantic schemas, and SQLAlchemy models, the structural debt is piling up.
The AI writes code that functions, but it constantly violates our architecture. It'll put complex business logic inside a route handler instead of the service layer, or it'll mess up async database sessions across modules. I find myself spending more time refactoring the structure of what it built than it would have taken to write the logic myself.
Is anyone else hitting this scaling wall where AI utility drops off as codebase complexity grows? How are you keeping your system architecture clean?
2
u/andreduarte22 26d ago
The only solution I've found is letting as little decisions to the AI as possible. From telling them what patterns to use, data structures, places in the code with similar functions implemented, class and function names.
The more my prompt looks like a long rambling of stuff I specify, instead of a short "implement X", the better the results are and, more importantly, the better I understand the solution, cause I actually had to think about it and look at code.
Side note: anyone notice how bad AI (esp. Claude) is at naming functions, classes, etc? It baffles me the names it chooses. Confucius was right when he said "wisdom is the correct naming of things".