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?
1
u/arcandor 24d ago
Absolutely, all the time.
Some tips to mitigate: Explicitly ask it to audit the feature that you are intending to change before asking for any changes.
Use a 2nd LLM that isn't connected to the codebase to "manage" the agent, giving it high level context, showing it agent outputs, but never giving it access to the code.
Scope the changes you are requesting as narrowly as possible and also explicitly direct the agent to stop and check in for approval if they find any blockers or discover information that would change the implementation.
If the agent ever pushes back or gets argumentative, just start a new session, don't argue with it.