AI writes unarchitectes code if you don't have a system for developing and maintaining a consistent architecture over time. In human coding, a lot of architecture decisions tend to only live in human brains, communicated through ephemeral means (text chats, voice calls, etc) and are not concretely documented. Since LLMs don't really have a way to manage long running architectural decisions (memory systems can only get so far, like it won't capture architectural decisions if you never talk about architectural decisions), you have to (have your agent) capture architecture design within the repository.
I personally like using openspec for this, because it makes the model iteratively ask you architectual decisions, and then manages creating and accessing the documents as needed.