πŸ“ Tutorials
Β· 4 min read

How to Use Claude Sonnet 5 with Aider: Setup and Config


Aider is one of the most popular open-source AI pair-programming tools, and it works cleanly with Claude Sonnet 5. With Sonnet 5 landing close to Opus 4.8 at a fraction of the price, it is an excellent default model for Aider. This guide gets you running in a few minutes.

New to the tool? Start with our Aider complete guide.

Step 1: Install Aider

python -m pip install aider-install
aider-install

Or with pipx:

pipx install aider-chat

Step 2: Add your Anthropic API key

Store the key in an environment variable rather than passing it on the command line:

export ANTHROPIC_API_KEY="your-key-here"

For good key hygiene, see secure AI API keys.

Step 3: Launch Aider with Sonnet 5

Point Aider at the model string claude-sonnet-5:

aider --model claude-sonnet-5

That is it. Aider will use Sonnet 5 for both code edits and chat. You can confirm the active model at the top of the Aider session.

Step 4: Use a strong-plus-cheap model split (optional)

Aider lets you set a separate model for edits versus higher-level reasoning. A cost-effective pattern is to run Sonnet 5 as your main model and reserve Opus 4.8 for the hardest architecture decisions:

aider --model claude-sonnet-5 --architect

In architect mode, you can have a stronger model plan and a cheaper model apply edits. Tune this to your budget.

Step 5: Manage the 1M context window

Sonnet 5 supports a one million token context window, so Aider can hold large parts of your repo in context. Use /add to include only the files relevant to the task. Adding the whole repo is possible but costs input tokens on every message. For patterns, see context window management.

Cost tips

  • Add only the files you need with /add, and drop them with /drop when done.
  • Use Aider’s built-in token and cost reporting to watch spend per message.
  • Remember Sonnet 5’s new tokenizer can raise effective token counts by up to 1.35 times. See pricing explained.
  • Use /clear to reset context between unrelated tasks.

A typical Aider session with Sonnet 5

Once you launch with aider --model claude-sonnet-5, the workflow is conversational. Add the files you want to work on, describe the change you want, and let Sonnet 5 propose edits as a diff that Aider applies and commits to git. A realistic sequence looks like this:

aider --model claude-sonnet-5
/add src/auth/session.py tests/test_session.py
> Add refresh-token rotation to the session manager and update the tests.

Sonnet 5 will read both files, implement the change, update the tests, and Aider will show you the diff before committing. Because Sonnet 5 is built to check its own work, it often catches edge cases in the tests it writes. Review the diff, then accept. If something is off, just describe the correction and it iterates.

When to escalate to Opus 4.8

Sonnet 5 handles the large majority of Aider work well. For the hardest tasks, deep multi-file refactors, subtle concurrency bugs, or architecture decisions, switch to the flagship for that session:

aider --model claude-opus-4-8

This two-model habit keeps your spend low while giving you the flagship when it genuinely matters. See Sonnet 5 vs Opus 4.8 for the decision rule and migrate from Opus 4.8 to Sonnet 5 if you are moving spend down.

Troubleshooting

  • Authentication errors. Confirm ANTHROPIC_API_KEY is exported in the same shell that runs Aider.
  • Model not found. Update Aider to a current version so it recognizes claude-sonnet-5.
  • Unexpectedly high cost. Sonnet 5’s new tokenizer can raise effective token counts by up to 1.35 times. Use /drop to remove files you no longer need and check Aider’s token report. See pricing explained.
  • Edits touch the wrong files. Be explicit about which files to change, and add only those with /add.

Frequently asked questions

What model string does Aider use for Sonnet 5? claude-sonnet-5, passed with --model.

Can I use Sonnet 5 and Opus 4.8 together in Aider? Yes. Use architect mode to pair a stronger planning model with a cheaper editing model.

Does Aider support the full 1M context window? Yes, though you control how many files enter context with /add and /drop.

Is Sonnet 5 a good default for Aider? Yes. It offers near-flagship coding quality at well under half the price of Opus 4.8.

Can I use Sonnet 5 with Aider through OpenRouter instead of the native API? Yes. Point Aider at an OpenRouter-compatible configuration and use the anthropic/claude-sonnet-5 model string. This gives you provider fallback at the cost of a small router fee. See the Sonnet 5 OpenRouter setup.

How do I keep Aider costs down with Sonnet 5? Add only the files you need with /add, drop them when finished, use /clear between unrelated tasks, and watch Aider’s built-in token report. Remember the new tokenizer can raise effective token counts, so trimming context matters more than it used to. See token efficiency.

Is Sonnet 5 better than Sonnet 4.6 in Aider? Yes. It is more reliable on multi-step edits and better at catching its own mistakes, which means fewer wasted iterations. See Sonnet 5 vs Sonnet 4.6.

Does Aider show me the cost of each Sonnet 5 request? Yes. Aider reports token usage and estimated cost per message, which makes it easy to spot expensive interactions and adjust how much context you include.

Can I use Sonnet 5 in Aider for non-Python projects? Absolutely. Aider and Sonnet 5 work across languages and stacks. Add the relevant files with /add regardless of language, and Sonnet 5 will edit them in place.

The bottom line

Running Sonnet 5 in Aider is a one-flag change: aider --model claude-sonnet-5. Manage context with /add and /drop, watch the token meter, and consider an architect split with Opus 4.8 for hard tasks. For the model details, read the Sonnet 5 complete guide.