Aider is the best terminal AI coding tool I've used. But by default it sends every diff through your OpenAI or Anthropic key, which gets expensive fast on real refactors — a single 100-file repo map can torch a few dollars before Aider even reads your prompt.
This post shows how to run Aider against any LLM provider — Ollama for free local runs, OpenRouter for mixed-provider routing, AWS Bedrock for the enterprise plate — through a single OpenAI-compatible endpoint, with prompt caching and MCP Code Mode layered on top to slash the bill further. I'll use puts GPT-4o and Claude 3.5 Sonnet at the top. But you don't need a $3-per-million-tokens model to rename a variable. You need it for the architecture decisions.
Lynkr's tier routing splits the work by prompt complexity:
| Aider call type | Routes to | Cost |
|---|---|---|
| Repo map summarization | qwen2.5-coder:7b (Ollama, local) | $0 |
| File edits, single-function diffs | gemini-flash-1.5 (OpenRouter) | ~$0.075/M |
| Architecture / multi-file refactors | claude-3.5-sonnet (Anthropic) | $3/M |
# .env additions
TIER_SIMPLE=ollama:qwen2.5-coder:7b
TIER_MEDIUM=openrouter:google/gemini-flash-1.5
TIER_COMPLEX=openrouter:anthropic/claude-3.5-sonnet
TIER_REASONING=openrouter:anthropic/claude-opus-4
Then point Aider at --model lynkr-auto and Lynkr scores each prompt before picking the tier.
Stacking the three levers
Each lever on its own is meaningful. Stacked, they compound:
Caching alone: ~70% input-token cut on a stable session
+ Tier routing: another ~40% by pushing routine calls to Flash/Ollama
+ MCP Code Mode (if you compose with other MCP tools): another 5–15x on tool-plumbing tokens
In my own Aider workflow — heavy refactors against a 200k-LOC monorepo — this combination has dropped a session that used to cost ~$8 in Claude calls down to under $1.50. Not because Claude got cheaper. Because most of the work is now happening on cached prefixes, free local models, or in-sandbox code execution.
Configuration walkthrough
Step 1 — Install and start Lynkr
npx lynkr@latest
First run creates a .env file. Minimal config:
MODEL_PROVIDER=anthropic
ANTHROPIC_API_KEY=sk-ant-...
PROMPT_CACHE=true
PORT=8081
For full local + free:
MODEL_PROVIDER=ollama
OLLAMA_ENDPOINT=http://localhost:11434
OLLAMA_MODEL=qwen2.5-coder:latest
PORT=8081
Then ollama pull qwen2.5-coder:latest.
Step 2 — Point Aider at the gateway
export OPENAI_API_BASE=http://localhost:8081/v1
export OPENAI_API_KEY=dummy
Drop those in your shell rc file.
Step 3 — Pick a model (or let Lynkr pick)
# Direct pass-through
aider --model openai/gpt-4o
# Or let Lynkr tier-route
aider --model lynkr-auto
Step 4 — Verify
curl -s http://localhost:8081/v1/models | python3 -m json.tool | head
Start Lynkr with LOG_LEVEL=info and watch the cache-hit lines on your second Aider request — that's where the savings show up.
Aider-specific gotchas
Weak model for commits / summarization. Aider uses a cheaper model for non-code calls; default is gpt-4o-mini. Override to a free local one:
aider --model openai/gpt-4o --weak-model ollama/qwen2.5-coder:7b
Long context. Local Ollama models will OOM on 200k+ token repo maps. Either set --map-tokens 0, or route long-context calls to Gemini Flash 1M-token contexts via the TIER_REASONING line above.
Streaming. Aider expects streaming responses. Lynkr streams by default. If you're on a non-streaming Databricks endpoint, set STREAM_PASSTHROUGH=false and Lynkr buffers + simulates.
Cache hit rate. Prompt caching only fires when the prefix is byte-identical across requests. If your repo map changes (you edit a /added file), the cache for that block invalidates and rebuilds. Lynkr logs cache-hit ratios per session — watch them; if hit rate is below 60% something in your workflow is busting the prefix.
Quickref
| Aider env var | Lynkr role |
|---|---|
OPENAI_API_BASE=http://localhost:8081/v1 | Where Lynkr listens |
OPENAI_API_KEY=dummy | Required by Aider, ignored by Lynkr |
--model openai/gpt-4o | Forwarded as-is to the configured upstream |
--model lynkr-auto | Triggers Lynkr's complexity-based tier routing |
--weak-model ollama/qwen2.5-coder:7b | Free local model for commit messages |
TL;DR
The default Aider setup pays full price for the same repo-map bytes on every turn. The fix isn't "use a cheaper model" — it's:
Cache the repetitive prefix (prompt caching).
Collapse tool plumbing into one call (MCP Code Mode).
Match model size to task complexity (tier routing).
Stacked, those three levers have taken my Aider sessions from ~$8 to ~$1.50 without changing how I work. Lynkr is one gateway that does all three; it's Apache 2.0, single Node binary, drop-in OpenAI base URL.
Aider's GitHub: — star to follow next integration writeups (OpenHands, Vercel AI SDK, Open Interpreter queued).
SOCIAL SHARE CARD GENERATOR