Your own AI Assistant with Aider and OpenRouter
This article will guide you through the complete process of connecting OpenRouter with Aider, an amazing open source terminal based AI assistant that you can integrate in your daily workflow.
This post is a part of the Cost Efficient AI Assistants series on Kubito. Make sure you check out the other posts in this series in the menu above.
We’ve seen many different AI assistants emerge in the last few years, VSCode forks like Cursor, GitHub Copilot running GPT-4o, VSCode extensions like Continue, and many more. What I found really impressive is Aider. It’s terminal based, much smarter and informative than the other solutions I’ve tried, and filled with features. Oh, and it connects with almost any local or external model!
Following up on the previous Open WebUI with OpenRouter discussion, we’ll reuse the same API Key we’ve created in OpenRouter to use Aider directly in the terminal. Let’s begin!
Installing Aider
To install Aider, follow the installation guide. Take note of the supported Python versions, because if you have an older/newer version, it won’t install.
For example, this is how I installed it on a Mac M3 Pro:
|
|
As you can see, I forced pipx
to use a lower version, as my system’s default one is 3.13
. To make sure everything went well, run the help command and while there, see the available options, there are a lot of them:
|
|
Connecting to OpenRouter
Now, see the docs for connecting to LLMs if you want more info. For OpenRouter, you can simply do the following:
|
|
MacOS Keychain for API Key (optional)
On Mac, I didn’t want to expose my OpenRouter API key, not even in the shell history, so I used the system keychain:
|
|
After that, I added the following to .zshrc
:
|
|
Now, Aider can access the environment variable securely after you do source .zshrc
or reopen the terminal.
Using Aider
Now you’re ready to use Aider. I personally use VSCode’s integrated terminal with its panel to the right side, much easier than switching between windows when using Aider, so you can do that as well if you want. Let’s start Aider with Claude 3.5 Sonnet and some flags that I’ve found helpful.
|
|
To explain the commands, I want dark mode because my terminal has a dark background, pretty output will give you better text output, the cache prompts feature will be utilized when a model supports it, I disabled auto commits because I want to control when I commit code, and I have chosen the model. When using OpenRouter models, you have to prefix them with openrouter/
. To see the available models, use the --list-models
flag or check the path on the OpenRouter web page.
Once that is done, you will see Aider entering a subshell where you can use commands to interact with your code. Always run the aider
command while in the repository you’re working on, and use /help
to see every available command, or check the Aider docs to see them all.
Aider recommends that you add the files you will be working on to keep the LLM “focused”, because many files at once will confuse it and it will provide bad results. You can add files with /add <filename>
, and it has a nice autocomplete with tab. Once you add files, you can simply ask what you want to be done to the code, and Aider will interact with the model and perform the changes. If you’re happy with the changes, and auto commit is disabled, just do /commit
and it will create a nice commit message and commit the code for you. Pretty neat!
Aider also has different modes. My favorite ones are /ask
, which will allow you to ask questions about the code without performing changes, and /architect
mode which will enter the model in a state where you can work together with it to create and design.
You can also change the model on the fly with the /model
command!
Make sure to check every tip in the official docs, they are very helpful if you want to understand how to use it better.
Model Warnings
Some models that are newer won’t have information about their token cost, so Aider won’t be able to tell you how much you’ve spent. This and other situations are called model warnings, and you can see the official docs on how to troubleshoot.
For example, when Qwen 2.5 Coder 32B and DeepSeek 2.5 came out, they didn’t have information from OpenRouter. To fix this, I simply added the following to $HOME/.aider.model.metadata.json
:
|
|
As mentioned, you can see the docs to fix this, and you can check the cost of the models on OpenRouter itself.
Conclusion
By using the same OpenRouter key for both Open WebUI and Aider, you have the flexibility and cost efficiency to switch between different models and get the best model for the task you are working on. This makes it the best setup I could create for myself to stop using ChatGPT directly, and I hope it will help you get a better idea on what you can create yourself. For now, I am very happy with this solution, especially knowing that I am not paying a fixed price for something I am not utilizing all the time, and knowing that I control how and when the models are used. See you in the next guide, and have fun setting your personal AI assistants!
If you find this post helpful, please consider supporting the blog. Your contributions help sustain the development and sharing of great content. Your support is greatly appreciated!
Buy Me a Coffee