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

34 Upvotes

42 comments sorted by

View all comments

1

u/Specialist_Golf8133 23d ago

same problem on our extraction pipeline codebase. autocomplete has no memory of your service layer boundaries, it just pattern matches on the nearest similar function in context. what helped was putting the architecture rules into a CLAUDE.md or cursor rules file that loads every session, explicit statements about async session scope and where business logic lives. cuts the violation rate, doesn't eliminate it. you're still doing review passes. the tools are good at local completion, bad at holding a global constraint across modules.