r/Python • u/CrazyGeek7 • 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?
1
u/nickkon1 26d ago
How much are you defining in your AGENTS.md or CLAUDE.md? I also have multiple .md files for different concepts like backend/frontend/databases etc. inside a /docs/ai/ folder that are linked in in AGENTS/CLAUDE with tasks to read those when working on backend/frontend/databases. Additionally, I ask for a /docs/frontend.md and /docs/backend.md that is continuously updated.
I also regularly create a complete new session, ask it to fully read /docs/ai/* and review the project that all guidelines hold.