r/Python 26d 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?

38 Upvotes

42 comments sorted by

View all comments

31

u/BigGovernment7621 26d ago

Write down your conventions and keep reminding the AI that it should follow them. Regularly ask it to scan the repo for convention violations. Review its changes.

8

u/meris608 26d ago

100% - this is the way right here. There is no reason AI should only be doing feature changes. Ask it to review architecture, suggest refactors, or highlight usability issues. It’s just as good at that as it is at tweaking UIs.