Aider - Architect Mode!

Learn how to use Aider-Architect for Enhanced AI-assisted coding

Elevating Your Codebase with Aider’s Architect Mode

In our last post, we explored Aider's Code Mode, showcasing its ability to scope LLM interactions to specific files for efficient debugging and incremental changes. Today, we’re taking things up a notch with Architect Mode, a powerful feature that allows Aider to perform advanced, large-scale code refactoring while adhering to robust engineering principles like SRP (Single Responsibility Principle) and DRY (Don’t Repeat Yourself).

If you’re managing a sprawling codebase, Architect Mode might just be your new best friend. Let’s dive into what makes this mode unique and how to use it effectively.

🎥 Watch the full demo on our YouTube Channel to see Architect Mode in action!


What is Architect Mode?

Architect Mode employs a two-model approach:

  1. Architect Model: Uses a larger, more compute-intensive model like OpenAI’s 01 Preview to reason about and plan high-level refactoring.
  2. Editor Model: Executes the proposed changes using a smaller, faster, and cost-efficient model such as Claude’s Sonnet.

This mode excels at managing complex tasks that span multiple files, such as reorganizing modules, extracting abstractions, or improving testability.

Key Benefit: By decoupling the reasoning phase from the editing phase, Architect Mode provides state-of-the-art refactoring while keeping costs in check.


Getting Started with Architect Mode

1. Setting Up Your Models

Architect Mode requires you to specify both an architect and an editor model. Use the following command to configure:

aider --o1-preview --architect --editor-model sonnet --no-auto-commits

This setup ensures the architect model does the heavy lifting while the editor model handles code changes efficiently.

  1. Choosing Your Scope

Architect Mode is ideal for larger refactorings. For instance, in the Meeting Buddy project, the home.py file had grown to nearly 500 lines, half of which dealt with non-UI logic like WebSocket communication. Architect Mode allowed us to:

  • Extract a WebSocketClient abstraction.
  • Encapsulate message queue processing.
  • Separate configuration loading.
  • Improve error handling and test coverage.
  • Write unit tests for the refactored components and more!
  1. Adding Files to Context

Start by identifying the files relevant to your refactor:

/add home.py
/add service.py
/add utils.py
/add create_meeting_notes.py
/add config.py

Adding these files ensures Aider has the necessary context for its recommendations.

How Architect Mode Works

Step 1: Ask the Architect

Provide a high-level request for improvement. For example:

The home.py file has grown large and unwieldy. Please refactor it to follow good engineering practices such as SRP, DRY, encapsulation, and testability.

The architect model will analyze the code and suggest a plan. For example:

  • Abstract the WebSocket client into a separate module.
  • Encapsulate stream handling logic.
  • Push queue processing into a dedicated class.
  • Improve function and variable naming with docstrings.

Step 2: Execute Changes

Once the architect model proposes a plan, the editor model will make incremental changes, producing Git diffs for review. For instance:

  • Code handling WebSocket logic was moved from home.py into a new websocket_client.py module.
  • Queue processing logic was encapsulated in a new MessageProcessor class.

Step 3: Iterative Refinement

Large refactorings often involve multiple iterations. Aider will process changes in smaller chunks to ensure clarity and correctness. If additional files are required, you can /add them to the context mid-session.

Real-Life Challenges and Solutions

  1. Managing Circular Dependencies

During the refactor, a circular dependency emerged between service.py and create_meeting_notes.py. Architect Mode suggested extracting shared functionality into a new summarizer.py module, resolving the issue cleanly.

  1. Test Generation

Architect Mode automatically generated new unit tests for the refactored components, including:

  • WebSocket connection handling.
  • Message processing logic.
  • Configuration loading.

While some tests needed refinement, the generated tests provided a solid foundation, saving significant development time.

  1. Cost and Efficiency

Using large models like 01 Preview increases compute costs. In our session, Architect Mode incurred approximately $2 in API costs—a worthwhile tradeoff for the time saved in complex refactoring. To minimize costs:

  • Use Architect Mode selectively for large changes.
  • Rely on Code Mode for smaller, targeted improvements.

Best Practices for Using Architect Mode

  1. Be Specific When Needed: Broad asks like “improve engineering practices” can result in sweeping changes. For narrower changes, provide more focused prompts.
  2. Review Diff Outputs: Always validate the diffs generated by Aider before applying them to ensure they align with your expectations.
  3. Iterate Gradually: For significant changes, let Aider process edits in smaller increments, especially when multiple files are involved.

In Conclusion

Architect Mode is a game-changer for developers tackling large-scale refactoring. It combines the power of advanced reasoning with efficient editing, enabling you to produce cleaner, more maintainable code with minimal effort. While it may require fine-tuning and some manual oversight, the productivity gains far outweigh the costs.

If you’re ready to elevate your development workflow, give Architect Mode a try. Check out the official documentation for more details: Architect Mode Overview.

Want to See It in Action?

Watch the full demo on the Spillwave YouTube Channel, where we refactor a real project step by step using Architect Mode. Don’t forget to subscribe for more AI-powered development tips!